?
Solved

Computer name by service tag in VBS

Posted on 2009-04-20
6
Medium Priority
?
736 Views
Last Modified: 2012-05-06
I'd like to search for a computer on my network by service tag number using VBS

Thanks,
0
Comment
Question by:smurteira
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:purplepomegranite
ID: 24191741
While I haven't unfortunately got a Dell computer to test this on, the below script should search the domain for a given service tag.  It queries each domain computer as shown, and the code I have used is documented in several places on the web as returning the service tag.

Just change the line declaring sServiceTag to declare the service tag you are after (use upper case).

DIM cn,cmd,rs
DIM objRoot
DIM intFailed, intSucceeded
DIM sServiceTag
 
sServiceTag="SERVICE TAG"
 
set cmd = createobject("ADODB.Command")
set cn = createobject("ADODB.Connection")
set rs = createobject("ADODB.Recordset")
 
cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn
 
set objRoot = getobject("LDAP://RootDSE")
 
cmd.commandtext = "<LDAP://" & objRoot.get("defaultNamingContext") & ">;(objectCategory=Computer);" & _
		  "dnsHostName;subtree"
cmd.properties("page size")=1000
 
set rs = cmd.execute
 
while rs.eof <> true and rs.bof <> true
	if UCase(ServiceTag(rs("dnsHostName")))=sServiceTag then
		' This computer has the service tag we are after
		wscript.echo rs("dnsHostName") & " has service tag " & sServiceTag
		wscript.quit
	end if
	rs.movenext
wend
 
cn.close
 
private function ServiceTag(byval strComputer)
	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
	For Each objSMBIOS in colSMBIOS
		ServiceTag = objSMBIOS.SerialNumber
	Next 
end function

Open in new window

0
 
LVL 14

Expert Comment

by:yehudaha
ID: 24192195
hey

purplepomegranite faster then me but i all ready wrote the script with a diffrent approch
the script will notify abount unreachable computers and permission problems,

strTag = inputbox("Enter Service Tag")
strSubnet = InputBox("Enter The Subnet to run the script against. e.g : 192.168.2")
strStartIP = "1"
strEndIP = "255"
 
 
For i = strStartIP To strEndIP
strComputer = strSubnet & "." & i
If Ping(strComputer) = True Then
If per(strComputer) then
Set objWMIService = GetObject("winmgmts:" _
	        & "\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery("Select * from Win32_BIOS",, 48)
For Each objBIOS In colBIOS
    If LCase(objBIOS.SerialNumber) = LCase(strTag) then
	WScript.Echo strTag & " Found On " & strComputer & ": " & objBIOS.SerialNumber 
	WScript.Quit
	End if
Next
Else
	WScript.Echo "You Don't Have Permission On " & strComputer
End If
Else
    WScript.Echo "Unable to ping " & strComputer
End If
next
 
Function Ping(strComputer)
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function
 
 
Function per(computer)
	strcomputer = computer
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	If err.number <> 0 Then
		err.Clear
		per = False
		On Error goto 0
	Else
		per = True
		On Error goto 0
	End If
End Function

Open in new window

0
 
LVL 2

Author Comment

by:smurteira
ID: 24195049
yehudaha,
Your script works, purplepomogranite's did not.
Is there a way to Export the results to a text file and put it on the C drive, like C:\Results.txt

When you run the script against a subnet, you have to sit there and click ok to every failed attempt until it finally finds it.  If we could redirect the results to a text file, I could run the script and look at it later when it found the computer.  

Also, it will give me the IP address but not the computer name.  I can then run a NBTSTAT -A <IP ADDRESS> to find the computer name, but can this also be incorporated into the script?

Thanks,
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
yehudaha earned 2000 total points
ID: 24195376
try this script

change this line as you wish:

strFile = "c:\result.txt"


strFile = "c:\result.txt"
Set objfso = CreateObject("scripting.filesystemobject")
Set objlog = objfso.CreateTextFile(strFile,True)
strTag = inputbox("Enter Service Tag")
strSubnet = InputBox("Enter The Subnet to run the script against. e.g : 192.168.2")
strStartIP = "1"
strEndIP = "255"
 
 
For i = strStartIP To strEndIP
strComputer = strSubnet & "." & i
If Ping(strComputer) = True Then
If per(strComputer) then
Set objWMIService = GetObject("winmgmts:" _
                & "\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery("Select * from Win32_BIOS",, 48)
For Each objBIOS In colBIOS
    If LCase(objBIOS.SerialNumber) = LCase(strTag) then
        objlog.WriteLine strTag & " Found On " & ResolveHost(strComputer) & ": " & objBIOS.SerialNumber & vbnewline
        WScript.Quit
        End if
Next
Else
        objlog.WriteLine "You Don't Have Permission On " & strComputer & vbnewline
End If
Else
    objlog.WriteLine "Unable to ping " & strComputer & vbnewline
End If
next
 
Function Ping(strComputer)
        Set objShell = CreateObject("WScript.Shell")
        boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
        If boolCode = 0 Then
                Ping = True
        Else
                Ping = False
        End If
End Function
 
 
Function per(computer)
        strcomputer = computer
        On Error Resume Next
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        If err.number <> 0 Then
                err.Clear
                per = False
                On Error goto 0
        Else
                per = True
                On Error goto 0
        End If
End Function
 
Function ResolveHost(ip)
strComputer = ip
 
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
 
Set colComputers = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration " _
& "Where IPEnabled = True")
 
For Each objComputer in colComputers
ResolveHost = objComputer.DNSHostName
Next 
End Function

Open in new window

0
 
LVL 2

Author Comment

by:smurteira
ID: 24197797
Yehudaha,
This works great!  This is going to save me so much time.

Thanks for all your help.
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 24197947
no problem
glad to help
thanks for the grade
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline

829 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