This is a compilation of a few code snippets that I have cobbled together to meet my immediate requirements. To run the script and get useful results, the requirements are:
1. Must be run as a user that already has WMI access to remote machines.
2. Input file consists of a series of hosts one per line that will be iterated through
3. Will only check files on the default system drive to my knowledge (suspect minor modification would allow searching other drives)
4. Must change the variable strInputFile strFileName and strFileExt to correspond with source input file and what you are looking for
Happy searching....
' filecheck.vbs
' VBScript program written to check for the file on a list of remote servers
' currently hard coded to exist in text file named servers.txt in same directory
' as this script
'
' This script assumes currently logged on account has access to WMI service
' on remote machines. Each instance of a matching file on the remote system
' results in an output of hostname, filename including full patch to be displayed
'
' Small modifications would be required to search drives other than C:
'
'
' Based off sample script by:
' ----------------------------------------------------------------------
' Copyright (c) 2007-2010 Richard L. Mueller
' Hilltop Lab web site - http://www.rlmueller.net
' Version 1.0 - January 2, 2007
' Version 1.1 - November 6, 2010 - No need to set objects to Nothing.
'
' You have a royalty-free right to use, modify, reproduce, and
' distribute this script file in any way you find useful, provided that
' you agree that the copyright owner above has no warranty, obligations,
' or liability for such use.
Option Explicit
Dim strComputer, locobjWMIService
Dim remobjWMIService
Dim listFile, fso, hname,colItems,objItem, driveItems, driveLetter
Dim strInputFile
Dim strFileName
Dim strFileExt
strInputFile = "servers.txt"
strFileName = "desktop"
strFileExt = "ini"
Set fso = CreateObject("Scripting.FileSystemObject")
Set listFile = fso.OpenTextFile(strInputFile)
do while not listFile.AtEndOfStream
on error resume next
hName = listFile.ReadLine()
' Connect to WMI service on the local computer.
Set locobjWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& ".\root\cimv2")
' Ping remote computer to see if online.
If (PingMachine(strComputer) = True) Then
colItems = ""
remobjWMIService = ""
Set remobjWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=Impersonate}!\\" & _
hname & "\root\cimv2")
' WMI Query to retrieve list of local drives
set driveItems = remobjWMIService.ExecQuery _
(Select * from Win32_LogicalDisk where DriveType = 3")
For Each driveLetter in DriveItems
Wscript.Echo "Drive Letter " & driveLetter.DeviceID & " is Present"
' WMI Query to retrieve list of files
Set colItems = remobjWMIService.ExecQuery _
("Select * from CIM_DataFile where Drive = '" & driveLetter.DeviceID & "' and _
FileName = '" & strFileName & "' and Extension = '" & strFileExt & "'")
' For Each... In Loop (Next at the very end)
For Each objItem in colItems
WScript.Echo hname & "," & objItem.name
Next
Else
Wscript.Echo "Computer " & hname & ", NOT available"
End If
loop
Function PingMachine(ByVal strHost)
' Returns True if strHost can be pinged.
' Variable objWMIService has global scope
' and must be declared in the main program.
Dim colPings, objPing
Set colPings = locobjWMIService.ExecQuery _
("SELECT * FROM Win32_PingStatus " _
& "WHERE Address = '" & strHost & "'")
For Each objPing In colPings
If objPing.StatusCode = 0 Then
' Computer responded to ping.
PingMachine = True
Exit Function
Else
' No reponse.
PingMachine = False
End If
Next
End Function
No comments:
Post a Comment