Jump to content

Recommended Posts

Posted

What we would like to do, when a computer is added to Active Directory is to automatically populate the 'Description' with the serial number of the computer.

 

The serial is programmed in the BIOS for 95% of our machines, so basically I'm looking to do 'wmic bios get serialnumber' but transfer the result into the 'Description' field of the AD object if that makes sense?

Posted (edited)

This vbs script will do what you want, how you will trigger it to run I don't know though.

 

On Error Resume Next

Dim objNetwork, objWMIService, strComputer, intMaxProfileAge, colBIOS, objBIOS, strSerialNumber, objComputer, adoConnection, adoRecordset

Set objRootDSE = GetObject("LDAP://RootDSE")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2") 
Set colBIOS = objWMIService.ExecQuery("Select * From Win32_BIOS")

If Not colBIOS Is Nothing Then
For Each objBIOS in colBIOS
	strSerialNumber = objBIOS.SerialNumber
Next
End If

Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"

If Err.Number <> 0 Then
MsgBox "Connect Error: " & Err.Description
WScript.Quit
End If

Set adoRecordset = adoConnection.Execute(";(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")

If Err.Number <> 0 Then
MsgBox "Query Error: " & Err.Description
WScript.Quit
End If

If Not adoRecordset.EOF Then
Set objComputer = GetObject(adoRecordset.Fields(0).Value)
objComputer.Put "description", strSerialNumber
objComputer.SetInfo
End If

If Err.Number <> 0 Then
MsgBox "Write Error: " & Err.Description
WScript.Quit
End If

Edited by ChrisMiles
Posted

yup i managed to work it out thanks

now trying to work out how to bull mac address and system type as well lol i love covering for other people in schools i dont know soo much time to fiddle lol

Posted
yup i managed to work it out thanks

now trying to work out how to bull mac address and system type as well lol i love covering for other people in schools i dont know soo much time to fiddle lol

 

Well here MAC Addresses done for you :p Not sure what you mean by system type, but you can see how its done :)

 

On Error Resume Next

Dim objNetwork, objWMIService, strComputer, intMaxProfileAge, colBIOS, objBIOS, colNetworkAdapterConfiguration, objNetworkAdapterConfiguration, strSerialNumber, strMacAddresses, objComputer, adoConnection, adoRecordset

Set objRootDSE = GetObject("LDAP://RootDSE")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2") 

Set colBIOS = objWMIService.ExecQuery("Select * From Win32_BIOS")

If Not colBIOS Is Nothing Then
For Each objBIOS in colBIOS
	strSerialNumber = objBIOS.SerialNumber
Next
End If

Set colNetworkAdapterConfiguration = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

strMacAddresses = ""

For Each objNetworkAdapterConfiguration in colNetworkAdapterConfiguration
If strMacAddresses <> "" Then
	strMacAddresses = strMacAddresses & ", "
End If
strMacAddresses = strMacAddresses & objNetworkAdapterConfiguration.MACAddress
Next

Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"

If Err.Number <> 0 Then
MsgBox "Connect Error: " & Err.Description
WScript.Quit
End If

Set adoRecordset = adoConnection.Execute(";(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")

If Err.Number <> 0 Then
MsgBox "Query Error: " & Err.Description
WScript.Quit
End If

If Not adoRecordset.EOF Then
Set objComputer = GetObject(adoRecordset.Fields(0).Value)
objComputer.Put "description", "Serial Number: " & strSerialNumber & "; MAC Addresses: " & strMACAddresses 
objComputer.SetInfo
End If

If Err.Number <> 0 Then
MsgBox "Write Error: " & Err.Description
WScript.Quit
End If

Posted

the script ive got that ouputs it to screen is

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") 
For Each objItem In colItems 
WScript.Echo "In MDT Variable Make will be: " & objItem.Manufacturer 
WScript.Echo "In MDT Variable Model will be: " & objItem.Model 
Next

 

one of these days (and as it looks like 6 weeks hold is going to be slow might be a good opportunity) i will learn more vbs with wmi i still default to batch files atm

Posted

adds to the description field in ad make model serial mac as below might edit in future to add ram

 

Make: Acer Model: AP F1b Serial No: PSF1BE6U05511030BFELAY MAC Addresses: 00:11:5B:D1:88:28

 

although it works run manually as admin dosent seem to work as a startup script i suspect the user cant write to ad

On Error Resume Next
strComputer = "." 

Dim objNetwork, objWMIService, strComputer, strMake, strModel, intMaxProfileAge, colBIOS, objBIOS, colNetworkAdapterConfiguration, objNetworkAdapterConfiguration, strSerialNumber, strMacAddresses, objComputer, adoConnection, adoRecordset

Set objRootDSE = GetObject("LDAP://RootDSE")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2") 

Set objWMIService2 = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems2 = objWMIService2.ExecQuery("SELECT * FROM Win32_ComputerSystem") 

Set colBIOS = objWMIService.ExecQuery("Select * From Win32_BIOS")

For Each objItem In colItems2 
strMake = objItem.Manufacturer 
strModel = objItem.Model 
Next

If Not colBIOS Is Nothing Then
For Each objBIOS in colBIOS
	strSerialNumber = objBIOS.SerialNumber
Next
End If

Set colNetworkAdapterConfiguration = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

strMacAddresses = ""

For Each objNetworkAdapterConfiguration in colNetworkAdapterConfiguration
If strMacAddresses <> "" Then
	strMacAddresses = strMacAddresses & ", "
End If
strMacAddresses = strMacAddresses & objNetworkAdapterConfiguration.MACAddress
Next

Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"

If Err.Number <> 0 Then
MsgBox "Connect Error: " & Err.Description
WScript.Quit
End If

Set adoRecordset = adoConnection.Execute(";(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")

If Err.Number <> 0 Then
MsgBox "Query Error: " & Err.Description
WScript.Quit
End If

If Not adoRecordset.EOF Then
Set objComputer = GetObject(adoRecordset.Fields(0).Value)
objComputer.Put "description", "Make: " & strmake & " Model: " &strModel & " Serial No: " & strSerialNumber & " MAC Addresses: " & strMACAddresses
objComputer.SetInfo
End If

If Err.Number <> 0 Then
MsgBox "Write Error: " & Err.Description
WScript.Quit
End If

Posted

Nah they wont be able to, try running it as a machine startup script, the computer system account may have permissions to update itself.

 

Here, I fixed the variable decelerations, removed the unneeded 2nd wmi object and added some missing error prevention code:

 

Option Explicit
On Error Resume Next

Dim objRootDSE, objNetwork, objWMIService, objComputer
Dim strComputer, strMake, strModel, strSerialNumber, strMacAddresses
Dim colBIOS, objBIOS
Dim colNetworkAdapterConfiguration, objNetworkAdapterConfiguration
Dim colComputerSystem, objComputerSystem
Dim adoConnection, adoRecordset

strComputer = "."

strMake = ""
strModel = ""
strSerialNumber = ""
strMacAddresses = ""

Set objRootDSE = GetObject("LDAP://RootDSE")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2") 

Set colComputerSystem = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") 

If Not colComputerSystem Is Nothing Then
For Each objComputerSystem In colComputerSystem 
	strMake = objComputerSystem.Manufacturer 
	strModel = objComputerSystem.Model 
Next
End If

Set colBIOS = objWMIService.ExecQuery("Select * From Win32_BIOS")

If Not colBIOS Is Nothing Then
For Each objBIOS in colBIOS
	strSerialNumber = objBIOS.SerialNumber
Next
End If

Set colNetworkAdapterConfiguration = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

strMacAddresses = ""

If Not colNetworkAdapterConfiguration Is Nothing Then
For Each objNetworkAdapterConfiguration in colNetworkAdapterConfiguration
	If strMacAddresses <> "" Then
		strMacAddresses = strMacAddresses & ", "
	End If
	strMacAddresses = strMacAddresses & objNetworkAdapterConfiguration.MACAddress
Next
End If

Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"

If Err.Number <> 0 Then
MsgBox "Connect Error: " & Err.Description
WScript.Quit
End If

Set adoRecordset = adoConnection.Execute(";(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")

If Err.Number <> 0 Then
MsgBox "Query Error: " & Err.Description
WScript.Quit
End If

If Not adoRecordset.EOF Then
Set objComputer = GetObject(adoRecordset.Fields(0).Value)
objComputer.Put "description", "Make: " & strmake & " Model: " &strModel & " Serial No: " & strSerialNumber & " MAC Addresses: " & strMACAddresses
objComputer.SetInfo
End If

If Err.Number <> 0 Then
MsgBox "Write Error: " & Err.Description
WScript.Quit
End If

  • Thanks 3
Posted
Nah they wont be able to, try running it as a machine startup script, the computer system account may have permissions to update itself.

 

Here, I fixed the variable decelerations, removed the unneeded 2nd wmi object and added some missing error prevention code:

 

Just the ticket .. been looking for a way to do this but been a) too lazy and b) too busy, but just run this and WHOOP! Thanks

Andy T :)

Posted
Nah they wont be able to, try running it as a machine startup script, the computer system account may have permissions to update itself.

 

Here, I fixed the variable decelerations, removed the unneeded 2nd wmi object and added some missing error prevention code:

 

 

i was running it as a computer startup script so i mant the computers user (so suitepc06$ or whatever) wonder if i can delegate permissions to modify that part of ad to the computers account

 

thanks as said i can just about get by in vb but its not my "1st language"

Posted (edited)

just used delegate control and gave the pc account access to read/write ad atributes (narrowed it down to just read/write description to a group called pcs created by searching for all computers) and it now works as a startup script

 

is there any way of creating an ad group that automaticaly updates itself to include all computers?

Edited by sted
Posted

me again lol

 

right after a bit more fiddling it now logs ram as well

 

to get it working as a startup script i created a group and added all the pcs in ad to it. I then used delegate control at domain level to give that group access to read/write the description aribute of active directory. Then i assigned it as a startup script and on reboot ad starts to populate with a quick pc inventory looks like this

 

Make: FUJITSU SIEMENS Model: ESPRIMO Edition Ram: 959 MB Serial No: xxxxxxxxxx MAC Addresses: 12:12:12:12:12:12

Option Explicit
On Error Resume Next

Dim objRootDSE, objNetwork, objWMIService, objComputer, objWMIServiceRam
Dim strComputer, strMake, strModel, strSerialNumber, strMacAddresses, strMsgRam 
Dim colBIOS, objBIOS
Dim colNetworkAdapterConfiguration, objNetworkAdapterConfiguration
Dim colComputerSystem, objComputerSystem
Dim adoConnection, adoRecordset
dim wbemServices, wbemObjectSet, wbemObject

strComputer = "."
strMsgRam = ""
strMake = ""
strModel = ""
strSerialNumber = ""
strMacAddresses = ""

Set objRootDSE = GetObject("LDAP://RootDSE")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2") 
Set objWMIServiceRam = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
Set wbemServices = GetObject( "winmgmts://" & strComputer )
Set wbemObjectSet = wbemServices.InstancesOf( "Win32_LogicalMemoryConfiguration" )
Set colComputerSystem = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") 

If Not colComputerSystem Is Nothing Then
For Each objComputerSystem In colComputerSystem 
	strMake = objComputerSystem.Manufacturer 
	strModel = objComputerSystem.Model 
Next
End If

Set colBIOS = objWMIService.ExecQuery("Select * From Win32_BIOS")

If Not colBIOS Is Nothing Then
For Each objBIOS in colBIOS
	strSerialNumber = objBIOS.SerialNumber
Next
End If

Set colNetworkAdapterConfiguration = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

strMacAddresses = ""

If Not colNetworkAdapterConfiguration Is Nothing Then
For Each objNetworkAdapterConfiguration in colNetworkAdapterConfiguration
	If strMacAddresses <> "" Then
		strMacAddresses = strMacAddresses & ", "
	End If
	strMacAddresses = strMacAddresses & objNetworkAdapterConfiguration.MACAddress
Next
End If

Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"

If Err.Number <> 0 Then
MsgBox "Connect Error: " & Err.Description
WScript.Quit
End If

Set adoRecordset = adoConnection.Execute(";(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")

If Err.Number <> 0 Then
MsgBox "Query Error: " & Err.Description
WScript.Quit
End If

For Each wbemObject In wbemObjectSet
strMsgRam = Int( ( wbemObject.TotalPhysicalMemory + 1023 ) / 1024 ) & " MB" 
'WScript.Echo strMsgRam
Next

If Not adoRecordset.EOF Then
Set objComputer = GetObject(adoRecordset.Fields(0).Value)
objComputer.Put "description", "Make: " & strmake & " Model: " &strModel & " Ram: " & strMsgRam & " Serial No: " & strSerialNumber & " MAC Addresses: " & strMACAddresses
objComputer.SetInfo
End If

If Err.Number <> 0 Then
MsgBox "Write Error: " & Err.Description
WScript.Quit
End If

Posted
ok works on xp and does work on win7x64 BUT throws up an error box "write error: object required" any ideas?
Posted

Win32_LogicalMemoryConfiguration doesn't exist in windows 7, i changed it to Win32_PhysicalMemory and tidied it up again, you really should stop pasting web code into it :p

 

Download a tool called WMI Explorer to see what WMI info you can get.

 

Option Explicit
On Error Resume Next

Dim objRootDSE, objNetwork, objWMIService, objComputer
Dim strComputer, strMake, strModel, strSerialNumber, strMacAddresses, strMemory, intMemory
Dim colBIOS, objBIOS
Dim colNetworkAdapterConfiguration, objNetworkAdapterConfiguration
Dim colComputerSystem, objComputerSystem
Dim colPhysicalMemory, objPhysicalMemory
Dim adoConnection, adoRecordset

strComputer = "."
strMemory = ""
strMake = ""
strModel = ""
strSerialNumber = ""
strMacAddresses = ""

Set objRootDSE = GetObject("LDAP://RootDSE")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objWMIService = GetObject("Winmgmts:\\" & strComputer & "\root\cimv2") 

Set colComputerSystem = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem") 

If Not colComputerSystem Is Nothing Then
For Each objComputerSystem In colComputerSystem 
	strMake = objComputerSystem.Manufacturer 
	strModel = objComputerSystem.Model 
Next
End If

Set colBIOS = objWMIService.ExecQuery("Select * From Win32_BIOS")

If Not colBIOS Is Nothing Then
For Each objBIOS in colBIOS
	strSerialNumber = objBIOS.SerialNumber
Next
End If

Set colNetworkAdapterConfiguration = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

strMacAddresses = ""

If Not colNetworkAdapterConfiguration Is Nothing Then
For Each objNetworkAdapterConfiguration in colNetworkAdapterConfiguration
	If strMacAddresses <> "" Then
		strMacAddresses = strMacAddresses & ", "
	End If
	strMacAddresses = strMacAddresses & objNetworkAdapterConfiguration.MACAddress
Next
End If

Set colPhysicalMemory = objWMIService.ExecQuery("Select * From Win32_PhysicalMemory")

If Not colPhysicalMemory Is Nothing Then
intMemory = 0
For Each objPhysicalMemory In colPhysicalMemory
	intMemory = intMemory + Int(objPhysicalMemory.Capacity)
Next
strMemory = (intMemory / 1024 / 1024) & " MB"
End If

Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"

If Err.Number <> 0 Then
MsgBox "Connect Error: " & Err.Description
WScript.Quit
End If

Set adoRecordset = adoConnection.Execute(";(&(objectCategory=Computer)(name=" & objNetwork.Computername & "));adspath;subtree")

If Err.Number <> 0 Then
MsgBox "Query Error: " & Err.Description
WScript.Quit
End If

If Not adoRecordset.EOF Then
Set objComputer = GetObject(adoRecordset.Fields(0).Value)
objComputer.Put "description", "Make: " & strmake & " Model: " &strModel & " Ram: " & strMemory & " Serial No: " & strSerialNumber & " MAC Addresses: " & strMACAddresses
objComputer.SetInfo
End If

If Err.Number <> 0 Then
MsgBox "Write Error: " & Err.Description
WScript.Quit
End If

  • Thanks 1
Posted

i know but its the only way i can do much with wmi or really vbs its on my long term "things to look into" list

 

thanks

Posted

Not getting the amount of RAM in the discription box on windows 7 64bit

and like sted said "write error: object required"

Posted
just used delegate control and gave the pc account access to read/write ad atributes (narrowed it down to just read/write description to a group called pcs created by searching for all computers) and it now works as a startup script

 

is there any way of creating an ad group that automaticaly updates itself to include all computers?

 

The "Domain Computers" group has all the computers added to your domain in it, so you could create a new group with the permissions you need, add the "Domain Computers" group as a member of that group then you have individual control for the AD access rights without disturbing any other settings associated with the default group.

  • Thanks 1
Posted
The "Domain Computers" group has all the computers added to your domain in it, so you could create a new group with the permissions you need, add the "Domain Computers" group as a member of that group then you have individual control for the AD access rights without disturbing any other settings associated with the default group.

 

never noticed that group but i suppose once you engage brain it makes sense

  • 3 months later...
Posted

sorry to dig up an old thread esp for whats probably a 2 second answer but how would i get it ti write its info to say the office field in ad rather than description?

 

tried altering objComputer.Put "Description", to objComputer.Put "Office", but it just gives a write error

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...