sage9x
(Sage9x)
1
Description
This script automatically populates the description field and the phonetic display name field in AD for computer objects. It pulls the model number, serial number, and machine description from the registry. The only think you need to tweak in it is how you want the description field formatted. If you add it as a logon script, it will keep these fields updated for you.
Source Code
' This script changes a computer's description as well as registers it in active directory with the serial number and last logged in user.
' If you want this to update automatically, make this a logon script in group policy.
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colsystemenclosure = objwmiservice.execquery("Select * from Win32_SystemEnclosure")
' Get Serial number
For Each objcomputer In colsystemenclosure
serialnumber = Trim(objcomputer.serialnumber)
Next
'get AD user
Set adsinfo = CreateObject("adsysteminfo")
Set adcomputer = GetObject("LDAP://" & adsinfo.ComputerName)
Set aduser = GetObject("LDAP://" & adsinfo.UserName)
username = aduser.cn
strcomputer = wshnetwork.computername
Set objwmiservice = GetObject("winmgmts:{impersonationlevel=impersonate}!\\" & strcomputer & "\root\cimv2")
Set colcomputer = objwmiservice.execquery("Select * from Win32_computersystem")
Set colcomputerdisk = objwmiservice.execquery("Select * from Win32_diskdrive")
Set coloperatingsystem = objwmiservice.execquery("Select * from Win32_OperatingSystem")
Set colsystemenclosure = objwmiservice.execquery("Select * from Win32_SystemEnclosure")
Set colmacid = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionID = 'Local Area Connection'")
'get model
For Each objcomputer In colcomputer
cpumanufacturer = objcomputer.manufacturer
cpumodel = Trim(objcomputer.model)
Next
' Get OS version
For Each objcomputer In coloperatingsystem
servicepack = objcomputer.servicepackmajorversion
osver = objcomputer.name
osver1 = Mid( osver, 18,3)
Next
'Pull the machine description. This can be formatted however you want. We use a "RM:" then a five letter description filled in with leading zeros.
desc = GetDesc()
'sets the information in AD
adcomputer.put "description", "RM: " & desc & ", OS: " & osver1 & ", Mod: " & cpumodel & ", SN: " & ucase(serialnumber)
adcomputer.Setinfo
'This bit sets the last logged on user as the "phonetic display name" in AD. It makes it easy to figure out what computer someone is talking about just by adding a column.
adcomputer.put "msDS-PhoneticDisplayName", "Last User: " & username
adcomputer.setinfo
Function GetDesc()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT Description FROM Win32_OperatingSystem")
For Each objItem in colItems
desc = objItem.Description
Next
GetDesc = desc
End Function
Function GetUser()
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
user = objComputer.UserName
Next
GetUser = user
End Function
Screenshots
4 Spice ups
dslong
(DanLong)
2
Thanks for the share! I’m going to talk to my supervisor about using this.
sage9x
(Sage9x)
3
Dan, I made it specifically for a School District so I hope it helps out!
Nice script, just added it to my environment. Thanks a bunch.
rawr55
(Rawr55)
5
This scripts seems to require some sort of elevated rights, is that so? If so, what does it need?
Rob-Dunn
(Rob Dunn)
6
I believe you need to delegate write permissions for your users on the ‘description’ field for computer objects in order for this to work. Great script, btw!
Rob was right too. I had to delegate write access to computer objects for authenticated users.
Greap Script , i had run this scripe unforecutinlty its not dispalying user information ,like you mention in you scripte. could you please advice me …!
rawr55
(Rawr55)
9
Does not seem to support Win10 “out-of-the-box”. Is there a quick fix for this?
sage9x
(Sage9x)
10
I honestly wrote this for Windows 7. I doubt it’s going to support Windows 10 properly, but I would need to research a solution. My current environment doesn’t contain any windows 10 devices, so I don’t have a way to test it.
Works great. Can it write to “Owner” instead? (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOwer) How?
Or: Can I extract “Phonetic Display Name” to Spiceworks Device General info? Or just to a report? Somewhat solved. I write to “location” instead of “msDS-PhoneticDisplayName”
Hi Ladies and Gents, I copied this script, saved it as .vbs, added it as a log on script for users but it is not working. Can anyone suggest anything for this? Thank you in advance.
sage9x
(Sage9x)
13
josephorlando2 , what OS are you running on the user machines?
fenomenal
(fenomenal)
14
Hi, I would just like to ask a question. I am interested in adding only the last login data in windows. How can I use the script?