Script to search for multiple files on multiple servers remotely
Posted: Thu Dec 20, 2012 7:37 am
Hi
For reason when I run the follwoing code some incorrect file paths are returned when I loop through the file names and servers.
Output:
Altiris0010CG02 Ping Successful...
Altiris0010CG02ExpressImagesWindows7W7110108W7110108.002 = True
Altiris0010CG02ExpressImagesWindows7W7110108W7110108.003 = True
Altiris0010CG02ExpressImagesWindows7W7110108W7110108.img = True
Altiris0010CG02ExpressImagesXP110102XP110102.002 = False
Altiris0010CG02ExpressImagesXP110102XP110102.003 = False
Altiris0010CG02ExpressImagesXP110102XP110102.img = False
Altiris0010CG02ExpressImagesXP110102XP110102.txt = False
Altiris0010CG02ExpressImagesXP110102XP110101.txt = False
Altiris0010CG02ExpressImagesXP110102XP110102.txt = False
Altiris0010CG02ExpressImagesXP110102XP110103.txt = False
Altiris0010CG02ExpressImagesXP110102XP110104.txt = False
Altiris0010CG02ExpressImagesXP110102XP110105.txt = False
Altiris0010CG02ExpressImagesXP110102XP110106.txt = False
Altiris0010CG02ExpressImagesXP110102XP110107.txt = False
Altiris0010CG02ExpressImages = False
Altiris0010CG02ExpressImages = False
Altiris0010CG02ExpressImages = False
Altiris0010CG02ExpressImages = False
Code:
'Run Script in Cscript mode only
'------------------------------------------------------------------------------------------------------
Sub forceCScriptExecution
Dim Arg, Str
If Not LCase( Right( WScript.FullName, 12 ) ) = "cscript.exe" Then
For Each Arg In WScript.Arguments
If InStr( Arg, " " ) Then Arg = """" & Arg & """"
Str = Str & " " & Arg
Next
CreateObject( "WScript.Shell" ).Run "cscript //nologo """ & WScript.ScriptFullName & """" & Str
WScript.Quit
End If
End Sub
forceCScriptExecution
'------------------------------------------------------------------------------------------------------
Function Ping(Target)
Dim results
On Error Resume Next
Set shell = CreateObject("WScript.Shell")
' Send 1 echo request, waiting 2 seconds for result
Set exec = shell.Exec("ping -n 6 -w 2000 " & Target)
results = LCase(exec.StdOut.ReadAll)
Ping = (InStr(results, "reply from") > 0)
End Function
'Usage: If Ping("192.168.1.100") Then
' Do something to access the resource
'End If
'---------------------------------------------------------------------------------
Function FileToArray(ByVal strFile, ByVal blnUNICODE)
Const FOR_READING = 1
Dim objFSO, objTS, strContents
' BEGIN CALLOUT A
FileToArray = Split("")
' END CALLOUT A
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFile) Then
On Error Resume Next
Set objTS = objFSO.OpenTextFile(strFile, FOR_READING, False, blnUNICODE)
If Err = 0 Then
strContents = objTS.ReadAll
objTS.Close
' BEGIN CALLOUT B
FileToArray = Split(strContents, vbNewLine)
' END CALLOUT B
End If
End If
End Function
'---------------------------------------------------------------------------------
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Error Handling
'---------------------------------------------------------------------------------
If objFSO.FileExists("Files.txt") Then
Else
wscript.echo "Could not find files.txt...ending script"
wacript.quit
End If
If objFSO.FileExists("Servers.txt") Then
Else
wscript.echo "Could not find Servers.txt...ending script"
wacript.quit
End If
'----------------------------------------------------------------------------------
If objFSO.FileExists("Results.txt") Then
objFSO.DeleteFile "Results.txt"
End If
Set objInputFile = objFSO.OpenTextFile("servers.txt",1)
Set objOutputFile = objFSO.OpenTextFile("Results.txt",8,True)
'Set drive and Path here...
'-----------------------------------------------------------------------------------
strDrive = "D"
strPath = "ExpressImages"
'-----------------------------------------------------------------------------------
Do until objInputFile.AtEndofStream
strcomputer = objInputFile.ReadLine
'----------------------------------------------------------------------------------
If Ping(strComputer) Then
objOutputFile.WriteLine(strComputer & " " & "Ping Successful...")
Else
objOutputFile.WriteLine(strComputer & " " & "Could not be reached...")
End If
'---------------------------------------------------------------------------------
For Each strLine In FileToArray("files.txt", False)
'strCompleteFile = "" & strComputer & "" & strDrive & "$" & strPath & strLine
strCompleteFile = "" & strComputer & "" & strPath & strLine
wscript.echo "Searching for:" & " " & strCompleteFile
wscript.echo "Writing Output:"
If objFSO.FileExists(strCompleteFile) Then
Data = strCompleteFile & " " & "= True"
objOutputFile.WriteLine(Data)
Else
Data = strCompleteFile & " " & "= False"
objOutputFile.WriteLine(Data)
End If
Next
Loop
'----------------------------------------------------------------
objOutputFile.Close
objInputFile.Close
Wscript.quit
Please helpto fix this code or show me a better way how to accomplish this task.
Thanks
For reason when I run the follwoing code some incorrect file paths are returned when I loop through the file names and servers.
Output:
Altiris0010CG02 Ping Successful...
Altiris0010CG02ExpressImagesWindows7W7110108W7110108.002 = True
Altiris0010CG02ExpressImagesWindows7W7110108W7110108.003 = True
Altiris0010CG02ExpressImagesWindows7W7110108W7110108.img = True
Altiris0010CG02ExpressImagesXP110102XP110102.002 = False
Altiris0010CG02ExpressImagesXP110102XP110102.003 = False
Altiris0010CG02ExpressImagesXP110102XP110102.img = False
Altiris0010CG02ExpressImagesXP110102XP110102.txt = False
Altiris0010CG02ExpressImagesXP110102XP110101.txt = False
Altiris0010CG02ExpressImagesXP110102XP110102.txt = False
Altiris0010CG02ExpressImagesXP110102XP110103.txt = False
Altiris0010CG02ExpressImagesXP110102XP110104.txt = False
Altiris0010CG02ExpressImagesXP110102XP110105.txt = False
Altiris0010CG02ExpressImagesXP110102XP110106.txt = False
Altiris0010CG02ExpressImagesXP110102XP110107.txt = False
Altiris0010CG02ExpressImages = False
Altiris0010CG02ExpressImages = False
Altiris0010CG02ExpressImages = False
Altiris0010CG02ExpressImages = False
Code:
'Run Script in Cscript mode only
'------------------------------------------------------------------------------------------------------
Sub forceCScriptExecution
Dim Arg, Str
If Not LCase( Right( WScript.FullName, 12 ) ) = "cscript.exe" Then
For Each Arg In WScript.Arguments
If InStr( Arg, " " ) Then Arg = """" & Arg & """"
Str = Str & " " & Arg
Next
CreateObject( "WScript.Shell" ).Run "cscript //nologo """ & WScript.ScriptFullName & """" & Str
WScript.Quit
End If
End Sub
forceCScriptExecution
'------------------------------------------------------------------------------------------------------
Function Ping(Target)
Dim results
On Error Resume Next
Set shell = CreateObject("WScript.Shell")
' Send 1 echo request, waiting 2 seconds for result
Set exec = shell.Exec("ping -n 6 -w 2000 " & Target)
results = LCase(exec.StdOut.ReadAll)
Ping = (InStr(results, "reply from") > 0)
End Function
'Usage: If Ping("192.168.1.100") Then
' Do something to access the resource
'End If
'---------------------------------------------------------------------------------
Function FileToArray(ByVal strFile, ByVal blnUNICODE)
Const FOR_READING = 1
Dim objFSO, objTS, strContents
' BEGIN CALLOUT A
FileToArray = Split("")
' END CALLOUT A
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFile) Then
On Error Resume Next
Set objTS = objFSO.OpenTextFile(strFile, FOR_READING, False, blnUNICODE)
If Err = 0 Then
strContents = objTS.ReadAll
objTS.Close
' BEGIN CALLOUT B
FileToArray = Split(strContents, vbNewLine)
' END CALLOUT B
End If
End If
End Function
'---------------------------------------------------------------------------------
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Error Handling
'---------------------------------------------------------------------------------
If objFSO.FileExists("Files.txt") Then
Else
wscript.echo "Could not find files.txt...ending script"
wacript.quit
End If
If objFSO.FileExists("Servers.txt") Then
Else
wscript.echo "Could not find Servers.txt...ending script"
wacript.quit
End If
'----------------------------------------------------------------------------------
If objFSO.FileExists("Results.txt") Then
objFSO.DeleteFile "Results.txt"
End If
Set objInputFile = objFSO.OpenTextFile("servers.txt",1)
Set objOutputFile = objFSO.OpenTextFile("Results.txt",8,True)
'Set drive and Path here...
'-----------------------------------------------------------------------------------
strDrive = "D"
strPath = "ExpressImages"
'-----------------------------------------------------------------------------------
Do until objInputFile.AtEndofStream
strcomputer = objInputFile.ReadLine
'----------------------------------------------------------------------------------
If Ping(strComputer) Then
objOutputFile.WriteLine(strComputer & " " & "Ping Successful...")
Else
objOutputFile.WriteLine(strComputer & " " & "Could not be reached...")
End If
'---------------------------------------------------------------------------------
For Each strLine In FileToArray("files.txt", False)
'strCompleteFile = "" & strComputer & "" & strDrive & "$" & strPath & strLine
strCompleteFile = "" & strComputer & "" & strPath & strLine
wscript.echo "Searching for:" & " " & strCompleteFile
wscript.echo "Writing Output:"
If objFSO.FileExists(strCompleteFile) Then
Data = strCompleteFile & " " & "= True"
objOutputFile.WriteLine(Data)
Else
Data = strCompleteFile & " " & "= False"
objOutputFile.WriteLine(Data)
End If
Next
Loop
'----------------------------------------------------------------
objOutputFile.Close
objInputFile.Close
Wscript.quit
Please helpto fix this code or show me a better way how to accomplish this task.
Thanks