The overall goal of this script is to hit all the servers, and if the description field does not contain the S/N, then it will put it there. BUT if it sees it is already there anywhere, it is to skip on to the next server. For starters I'm just gathering info & echoing to test the logic. Then I'll modify the AD records after I'm certain what the results will be.
Sample results:
NMS2
CN=NMS2,OU=Servers,OU=Corp,DC=thadmin,DC=com
Hp,ProLiant DL380 G3,7250LDN10151,1/30/2004 17:01
7250LDN10151
0
XX-CORP-FPS01
CN=XX-CORP-FPS01,OU=Servers,OU=Corp,DC=thadmin,DC=com
Hp,ProLiant DL380 G3,D247LDN1D154,1/30/2004 18:01
D247LDN1D154
0
How in the world does InStr NOT see 'D247LDN1D154' in 'Hp,ProLiant DL380 G3,D247LDN1D154,1/30/2004 18:01'?
Code:
'****************************************************************
'* Update Description with S/N *
'* Written by David J. 3/24/9 *
'****************************************************************
'****************************************************************
'* Create output log file *
'****************************************************************
Set objFSO4 = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO4.CreateTextFile(".\reports\UpdateSNreport.csv", True)
On error resume next
'****************************************************************
'* loop through all computers in AD *
'****************************************************************
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strDNSDomain = objRootDSE.Get("DefaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=computer)(objectClass=computer))"
strAttributes = "name,distinguishedName"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set RecordSet0 = objCommand.Execute
Do Until RecordSet0.EOF
strComputer = RecordSet0.Fields("name")
dn = RecordSet0.Fields("distinguishedName")
Set objComputer = GetObject("LDAP://" & dn )
'****************************************************************
'* Check AD to see if it's a server *
'****************************************************************
strOSName = objComputer.Get("OperatingSystem")
If instr(strOSName,"Server") <> 0 or instr(strOSName,"NT") <> 0 then
'****************************************************************
'* Get info *
'****************************************************************
desc = objComputer.description
Err.Clear
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If err.number = 0 then
Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
For Each objSMBIOS in colSMBIOS
strNumber = objSMBIOS.SerialNumber
Next
FirstDigit = instr(desc, strNumber)
If instr(desc, strNumber) = 0 then
wscript.echo strcomputer & vbcrlf & dn & vbcrlf & desc & vbcrlf & strNumber & vbcrlf & FirstDigit
Else
wscript.echo "Skipping this one" & strcomputer & vbcrlf & dn & vbcrlf & desc & vbcrlf & strnumber & vbcrlf & FirstDigit
End If
Else
objTextFile.Writeline "Could not connect to ," & strComputer
End if
End if
strNumber = ""
Set colSMBIOS = nothing
RecordSet0.MoveNext
Loop
objFSO4.Close
wscript.echo "Done"
Sample results:
NMS2
CN=NMS2,OU=Servers,OU=Corp,DC=thadmin,DC=com
Hp,ProLiant DL380 G3,7250LDN10151,1/30/2004 17:01
7250LDN10151
0
XX-CORP-FPS01
CN=XX-CORP-FPS01,OU=Servers,OU=Corp,DC=thadmin,DC=com
Hp,ProLiant DL380 G3,D247LDN1D154,1/30/2004 18:01
D247LDN1D154
0
How in the world does InStr NOT see 'D247LDN1D154' in 'Hp,ProLiant DL380 G3,D247LDN1D154,1/30/2004 18:01'?