Computer name by service tag in VBS

I'd like to search for a computer on my network by service tag number using VBS

Thanks,
LVL 2
smurteiraAsked:
Who is Participating?
 
yehudahaConnect With a Mentor Commented:
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
 
purplepomegraniteCommented:
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
 
yehudahaCommented:
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
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.

 
smurteiraAuthor Commented:
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
 
smurteiraAuthor Commented:
Yehudaha,
This works great!  This is going to save me so much time.

Thanks for all your help.
0
 
yehudahaCommented:
no problem
glad to help
thanks for the grade
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.