#
.SYNOPSIS
Rapport des machines du serveur WSUS : " + $UpdateServer + "
"
$intLineCounter = 0 # To count computers.
Remove-Item -force PcStatusReport.txt # This is a small log file to keep track of the last run results. It needs to be removed before start reading the list.
If (-Not (Import-Module UpdateServices -PassThru)) {
Add-Type -Path "$Env:ProgramFiles\Update Services\Api\Microsoft.UpdateServices.Administration.dll" -PassThru
}
$Wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer($UpdateServer,$Secure,$Port) # With this we get connected to the WSUS server.
$CTScope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope #This will the our scope, it includes all computers registered in the WSUS server.
# Now, lets write the HTML table headers.
$MsgBody = $MsgBody + ""
$MsgBody = $MsgBody + "
"
$MsgBody = $MsgBody + " "
# This is the main part: Here we will sort the list of computers by name, and get details for each one of them.
$wsus.GetComputerTargets($CTScope) | Sort -Property FullDomainName | ForEach {
$objSummary = $_.GetUpdateInstallationSummary() # This is an intermediate object that contains the details.
$Down = $objSummary.DownloadedCount # This is the amount of updates that has been downloaded already.
$Fail = $objSummary.FailedCount # This is the count for the failed updates.
$Pend = $objSummary.InstalledPendingRebootCount # This is the number of updates that need to reboot to complete installation.
$NotI = $objSummary.NotInstalledCount # These are the needed updates for this computer.
$Unkn = $objSummary.UnknownCount # These are the updates that are waiting for detection on the first search.
$Total = $Down + $Fail + $Pend + $NotI + $Unkn # Total amount of updates for this computer.
$intLineCounter = $intLineCounter + 1 # Increase the table line counter.
$IntStr = [Convert]::ToString($intLineCounter) # convert it to string to put it on the HTML code.
if ($Total -eq 0) {$Estado="OK"; $bgcolor="LightGreen"}
elseif ($Pend -ne 0) {$Estado="Reboot needed"; $bgcolor="LightSalmon"}
elseif ($Down -ne 0) {$Estado="Ready to install"; $bgcolor="Cyan"}
elseif ($NotI -ne 0) {$Estado="Pending"; $bgcolor="Yellow"}
elseif ($Fail -ne 0) {$Estado="Error"; $bgcolor="IndianRed"}
elseif ($Unkn -ne 0) {$Estado="Not reported yet"; $bgcolor="Silver"}
else {$Estado=""; $bgcolor="White"}
Write-Verbose ($IntStr + " : " + $_.FullDomainName) -Verbose # Show task progress on screen.
$LastContact = $_.LastReportedStatusTime # This is the last time when the computer reported to the wsus.
$days = [Math]::Ceiling((New-TimeSpan -Start $LastContact).TotalDays) # This is the number of days since last time.
if ($days -gt 14) {$Color="Red"} # Computer is away for too long.
elseif ($days -gt 7) {$Color="Orange"} # Computer may be in trouble.
elseif ($days -gt 2) {$Color="Yellow"} # Computer may be off.
else {$Color="White"} # Computer is ok.
# Reformat days to a more human-readable form.
if ($days -eq 0) {$Dias="Today"}
elseif ($days -eq 1) {$Dias="Yesterday"}
else {$Dias="Since " + $days + " days."}
if ($LastContact -eq [DateTime]::MinValue) {$Dias="Never"; $Color="Silver"}
# Now write the table row with all the info.
$MsgBody = $MsgBody + " Index "
$MsgBody = $MsgBody + "Status "
$MsgBody = $MsgBody + "Computer Name "
$MsgBody = $MsgBody + "IP Address "
$MsgBody = $MsgBody + "Last Contact "
$MsgBody = $MsgBody + "Total updates "
$MsgBody = $MsgBody + "Awaiting reboot "
$MsgBody = $MsgBody + "Ready to install "
$MsgBody = $MsgBody + "Download pending "
$MsgBody = $MsgBody + "Failed "
$MsgBody = $MsgBody + "Unknown State "
$MsgBody = $MsgBody + ""
$MsgBody = $MsgBody + " "
$_.FullDomainName >> PcStatusReport.txt # And append a new line on the log file.
}
$MsgBody = $MsgBody + " " + $IntStr +" "
$MsgBody = $MsgBody + " " + $Estado + " "
$MsgBody = $MsgBody + " " + $_.FullDomainName+ " "
$MsgBody = $MsgBody + " " + $_.IPAddress + " "
$MsgBody = $MsgBody + " " + $Dias +" "
$MsgBody = $MsgBody + "" + $Total + " "
$MsgBody = $MsgBody + "" + $Pend + " "
$MsgBody = $MsgBody + "" + $Down + " "
$MsgBody = $MsgBody + "" + $NotI + " "
$MsgBody = $MsgBody + "" + $Fail + " "
$MsgBody = $MsgBody + "" + $Unkn + " "
$MsgBody = $MsgBody + "
" # Finish the HTML table.
if ($intLineCounter -eq 0) {
Write-Verbose ("You must run this script from as administrator to read WSUS database.") -Verbose # Display a warning if not run with admin privileges.
$MsgBody = $MsgBody + " You must run this script from as administrator to read WSUS database.
"
} else {
$MsgBody = $MsgBody + "
"
}
#This is a footnote for the report readers.
$MsgBody = $MsgBody + "Note:
The updates are applied in a sequential three-steps process: Search, Download and Install.
Each computer has to go through these three stages to be updated."
$MsgBody = $MsgBody + "
"
$MsgBody = $MsgBody + "