Solved

vbs Script to get server serial numbers remotely

Posted on 2008-06-19
17
1,310 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

758 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

18 Experts available now in Live!

Get 1:1 Help Now