Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WMI

Status
Not open for further replies.

Ryandran

Technical User
Jan 23, 2011
3
ZA

0Hi,

I am a noob at vbscript and im trying to write a script to read a text file with a list of servers and to check the command used by BGInfo on each server and output the server name and command to a text file but what ever I have tried I cannot get the command to be output. I have tried assigning ObjItem.command to a variable and then outputting it but that didnt work either. Can anyone assist. Thanks.


Option Explicit
Const strInFile = "hostlist.txt"
Const strOutFIle = "results.txt"
Const ForReading = 1

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objInFile, objOutFile
Set objInFile = objFSO.OpenTextFile(strInFile,ForReading, False)
Set objOutFile = objFSO.CreateTextFile(objFSO.GetParentFolderName(Wscript.ScriptFullname) & "\" & strOutFile, True)

Dim objReg, strComputer, strBg
WriteOut "Hostname" & vbTab & "BGInfo Command"
' Step through computer names from input file and check for BGinfo
On Error Resume Next
Do Until objInFile.AtEndOfStream
strComputer = objInFile.Readline


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_StartupCommand Where Caption like '%BGinfo%'", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
WriteOut strComputer & VbTab & objitem.command

Next


Loop

Sub WriteOut(strText)
'Write text to file
objOutFile.WriteLine strText
' Write text to screen if running under cscript
If Instr(1, Wscript.FullName, "cscript",1) Then
Wscript.Echo strText
End If
End Sub
 
Comment out the On Resume Next instruction and say us which error message you get.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi,

I get Variable Undefined ObjItem, I used scriptomatic for the script for the local machine and modified the script to read from a list of servers. It seems to work fine on the local machine.
 
If you use Option Explicit (which I strongly recommend keeping), you have to declare all your variables. Just add this towards the top:
Dim ObjItem
 
Hi, Thanks it is working now. I have addded error handling to the script, can you advise me how to deal with cases were objitem.command does not exist, at the moment I get a permission denied error, which is a bit confusing because I get the same error if my user account cannnot acccess a server. Below is the updated script. Thanks

Option Explicit

Const strInFile = "hostlist.txt"
Const strOutFIle = "results.txt"
Const ForReading = 1

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objInFile, objOutFile
Set objInFile = objFSO.OpenTextFile(strInFile,ForReading, False)
Set objOutFile = objFSO.CreateTextFile(objFSO.GetParentFolderName(Wscript.ScriptFullname) & "\" & strOutFile, True)

Dim objWMIService, strComputer, ColItems, objItem
WriteOut "Hostname" & vbTab & "BGInfo Command"
On Error Resume Next

Do Until objInFile.AtEndOfStream
strComputer = objInFile.Readline
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_StartupCommand Where Caption like '%BGinfo%'")

If Err.number <> 0 then
Writeout StrComputer & VBtab & Err.description
Err.clear

Else
For Each objItem In colItems
WriteOut strComputer & VbTab & objitem.command
Next
End IF
Loop

Sub WriteOut(strText)
'Write text to file
objOutFile.WriteLine strText
' Write text to screen if running under cscript
If Instr(1, Wscript.FullName, "cscript",1) Then
Wscript.Echo strText
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top