Friday 16 October 2009

Update Active Directory from WMI

The Need:
I needed to update the computer description field for servers in AD with the site information where the computer was located. This value was already present for the WIN2k3 servers in WMI.

The Script:
#Update-AD-From-WMI.ps1

#Get the required value from WMI and place in a variable.
function Update-Servers-Site
{
$Serv = gwmi win32_NTDomain -ComputerName $Server | where { $_.caption -match "COMPANYNAME"}
$site = $Serv.ClientSiteName

#Set the description with the value from the variable
Get-qadComputer $server | Set-QADObject -Description $Site | Out-Null
Get-QADComputer $server | Select Name,Description
}

#Loop through a list of server running the function against each one.
$Servers = gc C:\liveServers.txt
Foreach ($server in $Servers) {Update-Servers-Site}