Solved

vbs Script to get server serial numbers remotely

Posted on 2008-06-19
17
1,312 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check version 13 70
Enable Intel-VT on a IBM Yoga 7 31
Add and format columns in vb6 7 47
Programmatically signing Word macros 4 61
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now