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!

Scrpit to collect remote WMI info 1

Status
Not open for further replies.

ncotton

IS-IT--Management
Jan 27, 2006
2,841
GB
I'm looking to retrieve remote WMI information (serial number) from a list of machines in a text file, and output to another text file the machine name and the serial number

The WMI Namespace is root\CIMV2
The WMI class is Win32_BIOS
The WMI object is SerialNumber

Output in a format akin
COMPUTER-001 : DAS123DFS
COMPUTER-002 :
COMPUTER-003 : DAS754SFP

Many regards
Neil
 
All of the things that you need to do have been discussed several times here. These are some search terms to help you get started:

For reading a file:
FileSystemObject
OpenTextFile
ReadLine
ReadAll

To get the information from the machines:
Remote WMI


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I've tried this but can't get it to access remote machines.
Code:
On Error Resume Next
fspec="c:\computers.txt"
set fso=createobject("scripting.filesystemobject")
set ots=fso.opentextfile(fspec)
do while not ots.atendofstream
	strComputer = ots.readline
		Set objWMIService = GetObject("winmgmts:\\" _
		& strComputer& "\root\CIMV2") 
	Set colItems = objWMIService.ExecQuery( _
		"SELECT * FROM Win32_BIOS") 
	For Each objItem in colItems 
		Wscript.Echo "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
	Next
	loop
ots.close
set ots=nothing
set fso=nothing

Neil J Cotton
Technical Consultant
Anix
 
Research the impersonation decorator for WMI calls. Also make sure that you have the required rights on the remote machine.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I've tried it too with impersonation.
 
If you remove On Error Resume Next do you get any errors?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
It's running, however it's just not connecting to the remote machines.

So if I have the local machine in the first entry, then it is fine, then all other instances report the first serial number
 
Currently I have

Code:
On Error Resume Next
comps="c:\computers.txt"
serials="c:\serials.txt"

set fso=createobject("scripting.filesystemobject")
set ots=fso.opentextfile(comps)
set objSerialsFile = fso.OpenTextFile(serials)

do while not ots.atendofstream
	strComputer = ots.readline
		Set objWMIService = GetObject("winmgmts:\\" _
		& strComputer& "\root\CIMV2") 
	Set colItems = objWMIService.ExecQuery( _
		"SELECT * FROM Win32_BIOS") 
	For Each objItem in colItems 
		Wscript.Echo "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
		objSerialsFile.WriteLine "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
	Next
	loop
ots.close
objSerialsFile
set ots=nothing
set fso=nothing
set objSerialsFile=nothing

However the output file is still empty despite the info being grabbed and displayed in the message boxes.
 
>objSerialsFile
[tt]objSerialsFile.close[/tt]
 
Have you tried putting an echo in earlier, to double check you're sending what you expect for the GetObject?

"Your rock is eroding wrong." -Dogbert
 
>That's not it.
[tt]That's not it without.[/tt]
 
>set objSerialsFile = fso_OpenTextFile(serials)
[tt]set objSerialsFile = fso_OpenTextFile(serials[red],2,true[/red])[/tt]
 
Even it is default to forreading (1), it is better to make it explicit.
>set ots=fso.opentextfile(comps)
[tt]set ots=fso.opentextfile(comps[blue],1[/blue])[/tt]

I leave the plausible problems of existence and format (ascii/unicode) of the files to yourself if encounted along the road.
 
OK. Developments.

The script now, fundamentally, works, however, if a computer can not be contacted, OR if the WMI value returned is null, then the objItem.SerialNumber isn;t cleared, and reports the previously collected serial number.

So I get
Comp 1 : A1B2C3
Comp 2 : X9Y8Z7
Comp 3 : X9Y8Z7
Comp 4 : X9Y8Z7
Comp 5 : T6Y7U8 etc

I'm currently running this code
Code:
On Error Resume Next
comps="c:\computers.txt"
serials="c:\serials.txt"

set fso=createobject("scripting.filesystemobject")
set objComputersTxt=fso.opentextfile(comps,1)
set objSerialsTxt = fso.OpenTextFile(serials,2,True)

do while not objComputersTxt.atendofstream
	strComputer = objComputersTxt.readline
		Set objWMIService = GetObject("winmgmts:\\" _
		& strComputer& "\root\CIMV2") 
	Set colItems = objWMIService.ExecQuery( _
		"SELECT * FROM Win32_BIOS") 
	For Each objItem in colItems 

		if objItem.SerialNumber="NULL" OR objItem.SerialNumber="" then
		else
			Wscript.Echo "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
			objSerialsTxt.WriteLine "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
			objItem.SerialNumber = NULL 
		end if
		Next
	loop

objComputersTxt.close
objSerialsTxt.close

set objComputersTxt=nothing
set objSerialsTxt=nothing
set fso=nothing
 
You put "on error resume next" by some mystical believe and ritual. It is not scripting.
 
the objItem.SerialNumber isn;t cleared
...
do while not objComputersTxt.atendofstream
strComputer = objComputersTxt.readline
Set objWMIService = GetObject("winmgmts:\\" _
& strComputer& "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_BIOS")
For Each objItem in colItems
if objItem.SerialNumber="NULL" OR objItem.SerialNumber="" then
else
Wscript.Echo "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
objSerialsTxt.WriteLine "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
objItem.SerialNumber = NULL
end if
Next
[!]Set colItems = Nothing
Set objWMIService = Nothing[/!]
Loop
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
even including set to nothing, it still populates with the previous value.

Code:
'On Error Resume Next
comps="c:\computers.txt"
serials="c:\serials.txt"

set fso=createobject("scripting.filesystemobject")
set objComputersTxt=fso.opentextfile(comps,1)
set objSerialsTxt = fso.OpenTextFile(serials,2,True)

do while not objComputersTxt.atendofstream
	strComputer = objComputersTxt.readline
		Set objWMIService = GetObject("winmgmts:\\" _
		& strComputer& "\root\CIMV2") 
	Set colItems = objWMIService.ExecQuery( _
		"SELECT * FROM Win32_BIOS") 
	For Each objItem in colItems 
		Wscript.Echo "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
		objSerialsTxt.WriteLine "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
		set objItem=Nothing
		next
	loop

objComputersTxt.close
objSerialsTxt.close

set objComputersTxt=Nothing
set objSerialsTxt=Nothing
set fso=nothing

and if I remove the On Error Resume Next, then it errors out when it hits a machine that it can't return.
 
...
For Each objItem in colItems
if objItem.SerialNumber="NULL" OR objItem.SerialNumber="" then
else
Wscript.Echo "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
objSerialsTxt.WriteLine "Name: "& strComputer & " SerialNumber: "& objItem.SerialNumber
objItem.SerialNumber = NULL
end if
Next
[!]Set objItem = Nothing
Set colItems = Nothing
Set objWMIService = Nothing[/!]
Loop
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top