JustScriptIt
Technical User
I am writing a script to check the existence of a folder.
After declaring Option Explicit and using the command-line
I still cannot figure out why the script says that a folder exists even though I can see it does not
This is the snippit of code
And just in case, below is the code in it's entirety
After declaring Option Explicit and using the command-line
Code:
cscript //x script.vbs
I still cannot figure out why the script says that a folder exists even though I can see it does not
This is the snippit of code
Code:
' Can we connect to WMI?
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strIP & "\root\cimv2")
If (Err.Number <> 0) Then
HandleError()
objErrorFile.WriteLine("Server " & strIP & ", Resolved Name: " & strName & " cannot be read")
Else
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = " & strPath)
If colFolders.Count > 0 Then
objFile.WriteLine(strIP & "," & strName & "," & "YES")
Else
objFile.WriteLine(strIP & "," & strName & "," & "NO")
End If
End If
And just in case, below is the code in it's entirety
Code:
Option Explicit
''''''''''Declare Variables''''''''''
Dim objShell, objScriptExec, objStdOut
Dim objInputFSO, objOutputFSO, objFile, objErrorOutputFSO, objErrorFile
Dim strInputFile, strData, arrLines, strLine, strIP, strName
Dim strCommand, strPingResult, bNoPing, arrayPingResult, strCheck, arrayPCLine
Dim objReg, strKeyPath, strValueName, strValue, strPath
Dim objWMIService, colFolders, strDrive, strArray, strRel, strGUP, strFolder
''''''''''Declare Constants''''''''''
const ForAppending = 8
const strOutputFile = "output_server_gup_status.csv"
const strErrOutputFile = "error_log_server_gup_status.txt"
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
''''''''''Declare Sub Routines''''''''''
Sub HandleError()
On Error Goto 0
Err.Clear
bErrorFound = True
End Sub
''''''''''Declare Functions''''''''''
Function Ping(strIP)
bNoPing = False
Set objShell = CreateObject("WScript.Shell")
strCommand = "ping.exe -n 1 -a " & strIP
Set objScriptExec = objShell.Exec(strCommand)
strPingResult = objScriptExec.StdOut.ReadAll
Set objStdOut = objScriptExec.StdOut
If InStr(strPingResult, "Reply from") Then
arrayPingResult = split(strPingResult, vbcrlf)
arrayPCLine = split(arrayPingResult(1), " ")
strName = arrayPCLine(1)
Else
strName = "NULL"
objErrorFile.WriteLine(strPingResult)
objErrorFile.WriteLine(vbcrlf)
bNoPing = True
End If
End Function
''''''''''Begin Execution''''''''''
'Create an Input File System Object
Set objInputFSO = CreateObject("Scripting.FileSystemObject")
'Name of the input text file
strInputFile = InputBox("What is the name of file with list of IP Addresses?")
'Open the text file - strData now contains the whole file
strData = objInputFSO.OpenTextFile(strInputFile,1).ReadAll
'Split the text file into lines
arrLines = Split(strData,vbCrLf)
'Create an Output File System Object
Set objOutputFSO = CreateObject("Scripting.FileSystemObject")
'Create Output File
Set objFile = objOutputFSO.CreateTextFile(strOutputFile)
'Create an Output File System Object for Recording Error
Set objErrorOutputFSO = CreateObject("Scripting.FileSystemObject")
'Create Output File
Set objErrorFile = objOutputFSO.CreateTextFile(strErrOutputFile)
objFile.WriteLine("IP Address,Computer, Is Server a GUP, Last Time Server Acted as GUP?")
'Step through the lines
For Each strLine in arrLines
strIP = strLine
Ping(strIP)
'*****If we can ping computer*****
If bNoPing = False Then
' Can we connect to registry?
On Error Resume Next
Set objReg=GetObject( _
"winmgmts:\\" &_
strIP & "\root\default:StdRegProv")
If (Err.Number <> 0) Then
HandleError()
objErrorFile.WriteLine("Cannot connect to registry of " & strIP & ", Resolved Name: " & strName)
Else
'Find the SEP Installation path
strKeyPath = "SOFTWARE\Symantec\Symantec Endpoint Protection\AV"
strValueName = "Home Directory"
objReg.GetStringValue _
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
strPath = strValue & "SharedUpdates"
' Can we connect to WMI?
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strIP & "\root\cimv2")
If (Err.Number <> 0) Then
HandleError()
objErrorFile.WriteLine("Server " & strIP & ", Resolved Name: " & strName & " cannot be read")
Else
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = " & strPath)
If colFolders.Count > 0 Then
objFile.WriteLine(strIP & "," & strName & "," & "YES")
Else
objFile.WriteLine(strIP & "," & strName & "," & "NO")
End If
End If
End If
End If
Next
Wscript.echo "Output is located at output_server_gup_status.csv"
Wscript.echo "Error log is located at error_log_server_gup_status.txt"
'Cleanup
Set objInputFSO=Nothing
Set objOutputFSO=Nothing