Solved

vbs Script to get server serial numbers remotely

Posted on 2008-06-19
17
1,319 Views
Last Modified: 2010-04-21
I have a script that gets a list of servers from LDAP using VBS and puts those names in a list.
I added to the script that it goes and gets the serial numbers of all of those computers. It creates two files called "serial_servers.txt" and "Error_serials.txt". If it can't get the serial number then it puts the computer name and error message in the "Error_serials.txt". If it can get the serial it puts it in serial_servers.txt.

The issue is that it is only returning a fraction of the servers from the original servers.txt file.
ON ERROR RESUME NEXT 
Const ADS_SCOPE_SUBTREE = 2
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("E:\WWW\ServerInventory\Servers_List.txt", True)
 
Dim objConnection, objCommand, objRecordSet, objRootDSE
Dim strComputer, strDN
 
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
 
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
 
Set objRootDSE = GetObject("LDAP://RootDSE")
objCommand.CommandText = "SELECT name, operatingSystem " &_
	"FROM 'LDAP://" & objRootDSE.Get("defaultNamingContext") &_
	"' WHERE objectClass='computer' AND (operatingSystem='Windows Server 2003*' OR operatingSystem='*2000 Server*')"
Set objRootDSE = Nothing
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 600
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
 
Set objRecordSet = objCommand.Execute
 
While Not objRecordSet.EOF
	'f1.WriteLine(objRecordSet.Fields("operatingSystem").Value) 
	f1.WriteLine(objRecordSet.Fields("name").Value) 
	
	
	objRecordSet.MoveNext
Wend
 
objConnection.Close
f1.Close
'msgbox ("done")
Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
 
'****** GET THE SERIAL NUMBERS AND WRITE TO FILE WITH NAME ********'
 
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objFile = objFSO.OpenTextFile("E:\WWW\ServerInventory\Servers_List.txt", ForReading)
Set f2 = fso.CreateTextFile("E:\WWW\ServerInventory\Servers_Serials.txt", True)
Set f3 = fso.CreateTextFile("E:\WWW\ServerInventory\Error_Serials.txt", True)
 
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close
 
'Then you can iterate it like this
 
For Each strLine in arrFileLines
 
ComputerName = strLine
winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//"& ComputerName &""
Set SNSet = GetObject( winmgmt1 ).InstancesOf ("Win32_BIOS")
if err.number <> 0 then
 f3.writeline(strLine & ": " & err.description)
else
for each SN in SNSet
serverSerial=SN.SerialNumber
Next
 
	f2.WriteLine(strLine & " " & serverSerial)
end if
Err.Clear
Next
f2.Close
f3.Close
msgbox("done")

Open in new window

0
Comment
Question by:bbcac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
17 Comments
 
LVL 17

Expert Comment

by:Jared Luker
ID: 21823815
Turn off on error resume next to see if it's giving you any kind of errors.
0
 

Author Comment

by:bbcac
ID: 21825313
it just errors out when it can't get the serial number. Normally the

if err.number <> 0 then
 f3.writeline(strLine & ": " & err.description)
else
for each SN in SNSet
serverSerial=SN.SerialNumber
Next
 
f2.WriteLine(strLine & " " & serverSerial)
end if


0
 

Author Comment

by:bbcac
ID: 21825411
It seems to be hanging on one particulary server. Is there anyway to supply a timeout for the serial number part?
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 17

Expert Comment

by:Jared Luker
ID: 21825815
is that a server that's different from the rest that might not have that info available from the BIOS?
0
 

Author Comment

by:bbcac
ID: 21825914
maybe.... but I need the script to accomodate that.
0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 21826179
Try this:
'Then you can iterate it like this
 
For Each strLine in arrFileLines
 	ComputerName = strLine
	winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//"& ComputerName &""
	Err.Clear
	Set colBios = objWMIService.ExecQuery("Select SerialNumber from Win32_BIOS",,48)
	If err.number <> 0 then
		f3.writeline(strLine & ": " & err.description)
	Else
		For Each objBios In colBios
			Dim SN
			SN = trim(objBios.SerialNumber)
			End If
			SerialNumber = SN
		Next
			f2.WriteLine(strLine & " " & SerialNumber)
	end if
	Err.Clear
Next
f2.Close
f3.Close
msgbox("done")

Open in new window

0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 21826192
oops... replace line 5 with this:

Set objWmiService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
0
 

Author Comment

by:bbcac
ID: 21831146
no that didn't work.... I thinik that line 5 was suppose to be
Set objBios = objWMIService.ExecQuery("Select SerialNumber from Win32_BIOS",,48)

since you don't reference colBios anywhere else

Even with this change it doesn't work. The Serial_Errors.txt file contains all the severs now.
0
 
LVL 17

Accepted Solution

by:
Jared Luker earned 500 total points
ID: 21831404
I created a servers_list.txt and put two servers in it and then ran the code below and it worked for me.  It pulled the serial number for both servers.
'****** GET THE SERIAL NUMBERS AND WRITE TO FILE WITH NAME ********'
 
'On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objFile = objFSO.OpenTextFile("E:\WWW\ServerInventory\Servers_List.txt", ForReading)
Set f2 = fso.CreateTextFile("E:\WWW\ServerInventory\Servers_Serials.txt", True)
Set f3 = fso.CreateTextFile("E:\WWW\ServerInventory\Error_Serials.txt", True)
 
Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
	Redim Preserve arrFileLines(i)
	arrFileLines(i) = objFile.ReadLine
	i = i + 1
Loop
objFile.Close
 
'Then you can iterate it like this
 
For Each strLine in arrFileLines
 	ComputerName = strLine
	Set objWmiService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2")
	Err.Clear
	Set colBios = objWMIService.ExecQuery("Select SerialNumber from Win32_BIOS",,48)
	If err.number <> 0 then
		f3.writeline(strLine & ": " & err.description)
	Else
		For Each objBios In colBios
			WScript.Echo objBios.SerialNumber
			Dim SN
			SN = trim(objBios.SerialNumber)
			'End If
			SerialNumber = SN
		Next
			f2.WriteLine(strLine & " " & SerialNumber)
	end if
	Err.Clear
Next
f2.Close
f3.Close
msgbox("done")

Open in new window

0
 
LVL 17

Assisted Solution

by:Jared Luker
Jared Luker earned 500 total points
ID: 21831498
I put a bogus server in my text file and it was acting weird.  I fixed it by putting the err.clear in the right place.
For Each strLine in arrFileLines
 	Err.Clear
 	ComputerName = strLine
	Set objWmiService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2")
	'WScript.Echo Err.Number
	Set colBios = objWMIService.ExecQuery("Select SerialNumber from Win32_BIOS",,48)
	If err.number <> 0 then
		f3.writeline(strLine & ": " & err.description)
	Else
		For Each objBios In colBios
			WScript.Echo objBios.SerialNumber
			Dim SN
			SN = trim(objBios.SerialNumber)
			'End If
			SerialNumber = SN
		Next
			f2.WriteLine(strLine & " " & SerialNumber)
	end if
	Err.Clear
Next
f2.Close
f3.Close
msgbox("done")

Open in new window

0
 

Author Comment

by:bbcac
ID: 21836187
It is still hanging up on the same server. It appears to be working up to that point though. Most of the servers end up in the server_serials.txt file while a few end up in the Error_serials.txt file

Any more ideas?
0
 
LVL 17

Assisted Solution

by:Jared Luker
Jared Luker earned 500 total points
ID: 21836252
We could try to keep that server out of the text file entirely...


While Not objRecordSet.EOF
	If Not objRecordSet.Fields("name").Value = "TroubleServer" Then
		'f1.WriteLine(objRecordSet.Fields("operatingSystem").Value) 
		f1.WriteLine(objRecordSet.Fields("name").Value) 
		objRecordSet.MoveNext
	End If
Wend

Open in new window

0
 
LVL 17

Assisted Solution

by:Jared Luker
Jared Luker earned 500 total points
ID: 21836257
Of course, you will need to change TroubleServer to the name of the server that the script is hanging on.
0
 
LVL 17

Assisted Solution

by:Jared Luker
Jared Luker earned 500 total points
ID: 21837686
or you could try to figure out why it's not accepting remote wmi calls.  Is it an ISA firewall, or have some other sort of software on it that would be blocking ports/protocols?
0
 

Author Comment

by:bbcac
ID: 21866679
I fixed the script by creating a secondary .vbs file that does the querying for me. That way I can use the .execute function to run the vbscript and timeout after x number of seconds. Thanks for you help
0
 

Author Closing Comment

by:bbcac
ID: 31470617
thanks
0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 21866815
Those points gave me my new rank in the VBScript zone!

Thanks!
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question