Populating Active Directory with Computer Information
June 4, 2015 Leave a comment
Here is a very quick tip i have employed to good effect at client sites.
It goes back to this very cool command i shared a few years ago now, here.
Being able to quickly identify a service tag, or even the model of a PC is very useful (At least to me) to determine if a hardware fault can be covered by a warranty repair, and with normal wear and tear stickers on machines can become difficult to read or be missing altogether!
So what is the answer i have used? Just add the info to the computers description field in AD.
Adjusting the above command slightly for use in PowerShell we are left with this..
Get-WMIObject Win32_ComputerSystemProduct | Select Vendor, Name, IdentifyingNumber
Which gives us the following output.
If we just want a single value, for example just the IdentifyingNumber, we just enclose the query in brackets, and select our value with a period and the value name, like this,
(Get-WMIObject Win32_ComputerSystemProduct).IdentifyingNumber
Excellent, but we do not want to visit each PC and note it down and then enter it into AD do we?
No we do not.
So firstly what we can do is load up the ActiveDirectory Management Tools for PowerShell.
Import-Module ActiveDirectory
Next we can set target computer.
$computer = "Rob-WIN8"
Now create some variables, to collect our system information.
(adding in “–ComputerName $computer” to direct out query to the right computer)
$vendor = (Get-WMIObject -ComputerName $computer Win32_ComputerSystemProduct).Vendor
$name = (Get-WMIObject -ComputerName $computer Win32_ComputerSystemProduct).Name
$identifyingNumber = (Get-WMIObject -ComputerName $computer Win32_ComputerSystemProduct).identifyingNumber
Now, if we execute this series of commands, the values returned are stored inside the relevant variable.
So if i just add in those variables below, it will output the value.
What is important to realise here, is now those values are stored, we can use them elsewhere
So, running this command,
Set-ADComputer $computer –Description “$vendor : $name : $identifyingNumber”
Has the following effect..
Now, currently of course, the only computer we have selected is the one we manually entered in $computer.
If we change the script now slightly,
$computers = Get-ADComputer -Filter * -searchBase "OU=Computers,DC=mydomain,DC=local"
Will collect all computers in the ‘Computers’ OU
if we then enclose all of the remaining queries inside a foreach statement,
foreach ($computer in $computers)
{
$vendor = (Get-WMIObject -ComputerName $computer Win32_ComputerSystemProduct).Vendor
$name = (Get-WMIObject -ComputerName $computer Win32_ComputerSystemProduct).Name
$identifyingNumber = (Get-WMIObject -ComputerName $computer Win32_ComputerSystemProduct).IdentifyingNumber
# Show Output of Variable
$vendor
$name
$identifyingNumber
Set-ADComputer $computer –Description “$vendor : $name : $identifyingNumber”
}
When we execute the script, each computer identified will be queried in turn* and you will see your AD populated with the info above.
* assuming it is on and there is nothing blocking a WMI query across the network.