vbfind
vbfind
(OP)
can someone tell me why this is just showing me the machines that error out, and not the machines that actually have adobe on them.... I'm losing my mind.
Option Explicit
On Error Resume Next
Const F_RDON = 1 ' Read-only
Const NPINGS = 2 ' # of ping attempts
Const TIMEOUT = 750 ' Time out in milliseconds
Dim fso, fd, file ' file references
Dim strComputer ' computer name read from file
Dim isAlive ' does machine respond to ping?
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
'Check for correct number of command-line args
If Wscript.Arguments.Count <> 1 Then
wscript.echo "Usage: cbstart <file>, where <file> is a text file " & _
"with one computer name per line" & vbCRLF
wscript.quit
End If
file = ".\" & Wscript.Arguments.Item(0)
set fso = CreateObject("Scripting.FileSystemObject")
Set fd = fso.OpenTextFile(file, F_RDON)
If Err.Number <> 0 Then
wscript.echo "FATAL: Could not open " & file
wscript.quit
End If
'Extract filename and open the corresponding file. While machine names remain
'in the file, ping the next name and then process if it responds.
Do while Not fd.AtEndOfStream
strComputer = fd.ReadLine
isAlive = canConnect(strComputer,NPINGS,TIMEOUT)
If isAlive = 1 Then
Go strComputer
Else
wscript.echo "WARNING: Could not ping " & strComputer
End If
Loop
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' Change start mode to automatic and start service
Sub Go(strComputer)
On Error Resume Next
Dim objWMIService, colFiles, objFile ' WMI references
Const PATH = "D:\Program Files\Adobe\Acrobat 6.0\Reader\"
Const FILE = "D:\Program Files\Adobe\Acrobat 6.0\Reader\AcroRd32.exe"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
if err.Number <> 0 Then
wscript.echo "WARNING: Could not connect to WMI service on " & strComputer
err.clear
Else
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile" _
& " where Drive='d:' and Path=" & PATH & "")
if err.number <> 0 then
wscript.echo "WARNING:" & err.description & " on " & strComputer
err.clear
else
For Each objFile in colFiles
if objFile.Name = FILE then
if objFile.FileSize = 7671876 then
wscript.echo "INFO: " & strComputer & " has Version 6.0 " & objFile.FileSize
else
wscript.echo "CRITICAL: " & strComputer & " is out of date " & objFile.FileSize
end if
end if
Next
end if
End If
End Sub
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' Ping workstation name and return false if it doesn't respond, true if it does
Function canConnect(strComputer, NPINGS, TIMEOUT)
Dim oShell, oExCmd, pingResult
Set oShell = CreateObject("WScript.Shell")
Set oExCmd = oShell.Exec("ping -n " & NPINGS & " -w " & TIMEOUT & _
" " & strComputer)
pingResult = oExCmd.StdOut.Readall
Select Case InStr(pingResult,"TTL=")
Case 0
canConnect = 0
Case Else
canConnect = 1
End Select
End Function
'------------------------------------------------------------------------------
Option Explicit
On Error Resume Next
Const F_RDON = 1 ' Read-only
Const NPINGS = 2 ' # of ping attempts
Const TIMEOUT = 750 ' Time out in milliseconds
Dim fso, fd, file ' file references
Dim strComputer ' computer name read from file
Dim isAlive ' does machine respond to ping?
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
'Check for correct number of command-line args
If Wscript.Arguments.Count <> 1 Then
wscript.echo "Usage: cbstart <file>, where <file> is a text file " & _
"with one computer name per line" & vbCRLF
wscript.quit
End If
file = ".\" & Wscript.Arguments.Item(0)
set fso = CreateObject("Scripting.FileSystemObject")
Set fd = fso.OpenTextFile(file, F_RDON)
If Err.Number <> 0 Then
wscript.echo "FATAL: Could not open " & file
wscript.quit
End If
'Extract filename and open the corresponding file. While machine names remain
'in the file, ping the next name and then process if it responds.
Do while Not fd.AtEndOfStream
strComputer = fd.ReadLine
isAlive = canConnect(strComputer,NPINGS,TIMEOUT)
If isAlive = 1 Then
Go strComputer
Else
wscript.echo "WARNING: Could not ping " & strComputer
End If
Loop
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' Change start mode to automatic and start service
Sub Go(strComputer)
On Error Resume Next
Dim objWMIService, colFiles, objFile ' WMI references
Const PATH = "D:\Program Files\Adobe\Acrobat 6.0\Reader\"
Const FILE = "D:\Program Files\Adobe\Acrobat 6.0\Reader\AcroRd32.exe"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
if err.Number <> 0 Then
wscript.echo "WARNING: Could not connect to WMI service on " & strComputer
err.clear
Else
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile" _
& " where Drive='d:' and Path=" & PATH & "")
if err.number <> 0 then
wscript.echo "WARNING:" & err.description & " on " & strComputer
err.clear
else
For Each objFile in colFiles
if objFile.Name = FILE then
if objFile.FileSize = 7671876 then
wscript.echo "INFO: " & strComputer & " has Version 6.0 " & objFile.FileSize
else
wscript.echo "CRITICAL: " & strComputer & " is out of date " & objFile.FileSize
end if
end if
Next
end if
End If
End Sub
'------------------------------------------------------------------------------
'------------------------------------------------------------------------------
' Ping workstation name and return false if it doesn't respond, true if it does
Function canConnect(strComputer, NPINGS, TIMEOUT)
Dim oShell, oExCmd, pingResult
Set oShell = CreateObject("WScript.Shell")
Set oExCmd = oShell.Exec("ping -n " & NPINGS & " -w " & TIMEOUT & _
" " & strComputer)
pingResult = oExCmd.StdOut.Readall
Select Case InStr(pingResult,"TTL=")
Case 0
canConnect = 0
Case Else
canConnect = 1
End Select
End Function
'------------------------------------------------------------------------------
Doh!!
RE: vbfind
The problem with your go sub is that the properties are misunderstood in their syntax and contents.
A shortcut to change it is to retrieve the file directly.
Set colFiles = objWMIService.ExecQuery("Select Name, FileSize from CIM_DataFile" _
& " where Name='" & replace(FILE,"\","\\") & "'")
Then the for-next part...
if colFiles.count=0 then
wscript.echo "INFO: " & strComputer & " does not have " & FILE & " at the location."
else
For Each objFile in colFiles
if objFile.FileSize = 7671876 then
wscript.echo "INFO: " & strComputer & " has Version 6.0 " & objFile.FileSize
else
wscript.echo "CRITICAL: " & strComputer & " is out of date " & objFile.FileSize
end if
Next
end if
Messages etc are not perfect but you get the idea.
Main thing is path in the wql statement does not include drive name and the "filename" and backslash is escaped. Name is the full name with full path included. Filename is the bare name without info on the path leading to it. So your colFiles is empty, hence, no message at all.
regards - tsuji
RE: vbfind
thanks again...
Doh!!
RE: vbfind
Sub Go(strComputer)
On Error Resume Next
Dim objWMIService, colFiles, objFile ' WMI references
Const PATH = "D:\Program Files\Adobe\Acrobat 6.0\Reader\"
Const FILE = "AcroRd32.exe"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
if err.Number <> 0 Then
wscript.echo "WARNING: Could not connect to WMI service on " & strComputer
err.clear
Else
'Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile" _
' & " where Drive='d:' and Path=" & PATH & "")
Set colFiles = objWMIService.ExecQuery("Select Name, FileSize from CIM_DataFile" _
& " where Name='" & replace(FILE,"\","\\") & "'")
if err.number <> 0 then
wscript.echo "WARNING:" & err.description & " on " & strComputer
err.clear
else
For Each objFile in colFiles
if objFile.Name = FILE then
if objFile.FileSize <> 7671876 then
wscript.echo "INFO: " & strComputer & " has Version 6.0 " & objFile.FileSize
else
wscript.echo "CRITICAL: " & strComputer & " is out of date " & objFile.FileSize
end if
end if
Next
end if
End If
End Sub
Doh!!
RE: vbfind
I just have a quick look. (I can think about it during weekend.)
I think when you compare file name like
if objFile.name=FILE then
you should do it by first uniformizing the case, like this:
if lcase(objFile.name)=lcase(file)
Whereas, in the wql the compare is not internally with case-insensitive built-in.
- tsuji
RE: vbfind
I meant [1]:
if lcase(objFile.name)=lcase(file) then
and that [2]:
Whereas, in the wql the compare is done internally with case-insensitive built-in.
- tsuji
RE: vbfind
In the revised query string, it is understood if ever anything retrieved, objFile.name=FILE is guaranteed by design. So the testing is just a left-over from the previous query string. (But, if done properly, it would do any harm.)
- tsuji