Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1330
  • Last Modified:

vbs Script to get server serial numbers remotely

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
bbcac
Asked:
bbcac
  • 10
  • 7
5 Solutions
 
Jared LukerCommented:
Turn off on error resume next to see if it's giving you any kind of errors.
0
 
bbcacAuthor Commented:
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
 
bbcacAuthor Commented:
It seems to be hanging on one particulary server. Is there anyway to supply a timeout for the serial number part?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Jared LukerCommented:
is that a server that's different from the rest that might not have that info available from the BIOS?
0
 
bbcacAuthor Commented:
maybe.... but I need the script to accomodate that.
0
 
Jared LukerCommented:
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
 
Jared LukerCommented:
oops... replace line 5 with this:

Set objWmiService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
0
 
bbcacAuthor Commented:
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
 
Jared LukerCommented:
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
 
Jared LukerCommented:
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
 
bbcacAuthor Commented:
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
 
Jared LukerCommented:
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
 
Jared LukerCommented:
Of course, you will need to change TroubleServer to the name of the server that the script is hanging on.
0
 
Jared LukerCommented:
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
 
bbcacAuthor Commented:
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
 
bbcacAuthor Commented:
thanks
0
 
Jared LukerCommented:
Those points gave me my new rank in the VBScript zone!

Thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now