Link to home
Start Free TrialLog in
Avatar of bbcac
bbcac

asked on

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

Avatar of Jared Luker
Jared Luker
Flag of United States of America image

Turn off on error resume next to see if it's giving you any kind of errors.
Avatar of bbcac
bbcac

ASKER

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


Avatar of bbcac

ASKER

It seems to be hanging on one particulary server. Is there anyway to supply a timeout for the serial number part?
is that a server that's different from the rest that might not have that info available from the BIOS?
Avatar of bbcac

ASKER

maybe.... but I need the script to accomodate that.
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

oops... replace line 5 with this:

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

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Jared Luker
Jared Luker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbcac

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbcac

ASKER

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
Avatar of bbcac

ASKER

thanks
Those points gave me my new rank in the VBScript zone!

Thanks!