Monday, June 6, 2011

VBScript - Get Service Tags of All Dell PCs in Active Directory

I used to have access to Kaseya which will pull this for you, but my new employer doesn't use it.  This script has some issues (if a PC is offline it will just throw in the service tag of the last one) but it was quick and dirty enough for my purposes today.  I observe that Blogger doesn't handle code well, so beware of formatting.  I'll have to find a way around this and blog that.

'Pulls the Dell Service Tag or equivalent of every computer in AD and writes them to a text file with computer 'name
On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set myFSO = CreateObject("Scripting.FileSystemObject")
Set WriteStuff = myFSO.OpenTextFile("C:\users\example\desktop\test.txt", 8, True)

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
Set WshShell = WScript.CreateObject("WScript.Shell")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
    "Select Name, Location from 'LDAP://DC=example,DC=corp' " _
        & "Where objectClass='computer'" 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    strComputer=objRecordSet.Fields("Name").Value
    Set objWMIService = GetObject("winmgmts:" & _
        "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    For Each objSMBIOS in objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
        WriteStuff.WriteLine strComputer & " Serial Number: " & objSMBIOS.SerialNumber
    Next
    objRecordSet.MoveNext
Loop


WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
MsgBox "Done"

No comments:

Post a Comment