Click an Ad

If you find this blog helpful, please support me by clicking an ad!

Monday, June 24, 2013

Automated WSUS Report - Computers in the Unassigned Computers Container

It seems like one of the other admins is always adding a computer to the domain and not telling me, and I realized that I need to make it a priority to check the "Unassigned Computers" container every so often to ensure there isn't anything in it. This is the sort of thing Powershell shines at!

Before you run this, make sure you have a C:\Temp folder, or change the output file path. I did my explanation in the comments within the script below:



$WsusServer = "WSUSServer.contoso.com"
$UseSSL = $false
$PortNumber = 80
$TempFile = "C:\Temp\Output.txt"

#E-mail Configuration
$SMTPServer = "SMTPServer.contoso.com"
$From = "administrator@contoso.com"
$To = "me@contoso.com"
$Subject = "PS Report - Unassigned Computers in WSUS"

#Connect to the WSUS 3.0 interface.
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
$Wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer($WsusServer,$UseSSL,$PortNumber);

#Get the FQDN of computers that reside in the Unassigned Computers container
$Unassigned = (($Wsus.GetComputerTargetGroups() | ?{$_.Name -eq 'Unassigned Computers'}).GetComputerTargets() | select FullDomainName)

#Output each to the temp file
Foreach ($Computer in $Unassigned){
($Computer.FullDomainName) | out-string | add-content $TempFile
}

#Create the body of the email, inserting <BR> tags between each line.
$Body = (Get-Content $TempFile) -join '<BR>'

#If there are computers in the unassigned computers group, send me an email listing them
If ($Unassigned -ne $null){
Send-Mailmessage -From $From -To $To -Subject $Subject -SMTPServer $SMTPServer -Body $Body -BodyAsHTML
}

#Delete the temp file
Remove-Item $TempFile -ea silent



One of the challenges I faced here was getting the computer names into the text file. Out-String didn't work because of the type of object the $Unassigned was, for some reason. I ended up outputting to the temp file, then using -join to put in <BR> tags, then sending the email with the BodyAsHTML option. In HTML, the <BR> tag just represents a carriage return (go to the next line).

No comments:

Post a Comment