Skip to content

Instantly share code, notes, and snippets.

@alexinnes
Created December 2, 2016 08:15
Show Gist options
  • Save alexinnes/9e45bcc5c170715782bdb4be139130ca to your computer and use it in GitHub Desktop.
Save alexinnes/9e45bcc5c170715782bdb4be139130ca to your computer and use it in GitHub Desktop.
Gets pending/failed updates on a Windows Update server
<#
.Synopsis
Function to get updates that are pending on a WSUS server.
.DESCRIPTION
Function looks at the NotInstalled, IntalledPendingReboot, Downloaded and Failed updates and returns them into a hash table.
.EXAMPLE
Get-PendingWindowsUpdates
.EXAMPLE
Get-PendingWindowsUpdates -ComputerName "Update-server"
.INPUTS
You can pass a computer name of any computer on the networ: Get-PendingWindowsUpdates -ComputerName "Update-server"
.OUTPUTS
Returns a hash table of arrays, the arrays have information on the updates.
.NOTES
This needs to be run on a server that has WSUS installed.
#>
function Get-PendingWindowsUpdates {
[CmdletBinding()]
Param (
# Specify the name of one computer
[Parameter(
Position = 0,
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias("Computer", "__SERVER", "IPAddress", "name" )]
$ComputerName = $env:COMPUTERNAME
)
Begin {
#Gets all the updates.
$UpdateScopeValue = "All"
[void] [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
#Get the WSUS Server
$WSUS = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer()
#Create the computer scope to get a list of updates for a specified machine.
$ComputerScope = New-Object Microsoft.UpdateServices.Administration.ComputerTargetScope
$Computers = $WSUS.GetComputerTargets($ComputerScope) | where {$_.fulldomainname -like "$ComputerName*"}
#Get the scope of what updates to get
$UpdateScope = New-Object Microsoft.UpdateServices.Administration.UpdateScope
$UpdateScope.IncludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::$UpdateScopeValue
#Gets the information about the updates
$Updates = $Computers.GetUpdateInstallationInfoPerUpdate($UpdateScope)
}
Process {
#Get the different types of updates filtered on the action.
$UpdateNotInstalled = Try{($updates | where{$_.UpdateApprovalAction -eq "Install" -and $_.UpdateInstallationState -eq "NotInstalled"}).getupdate()|where{$_.IsSuperseded -eq $false}}Catch{"There are no Updates"}
$UpdateInstalledPendingReboot = Try{($Updates | where{$_.UpdateApprovalAction -eq "Install" -and $_.UpdateInstallationState -eq "InstalledPendingReboot"}).getUpdate()|where{$_.IsSuperseded -eq $false}}Catch{"There are no Updates"}
$UpdateDownloaded = Try{($Updates | where{$_.UpdateApprovalAction -eq "Install" -and $_.UpdateInstallationState -eq "Downloaded"}).getUpdate()|where{$_.IsSuperseded -eq $false}}Catch{"There are no Updates"}
$UpdateFailed = Try{($Updates | where{$_.UpdateApprovalAction -eq "Install" -and $_.UpdateInstallationState -eq "Failed"}).getUpdate()|where{$_.IsSuperseded -eq $false}}Catch{"There are no Updates"}
#All the update types in a hash table
$updateSummary = @{
NotInstalled = $UpdateNotInstalled
InsalledPendingReboot = $UpdateInstalledPendingReboot
Downloaded = $UpdateDownloaded
Failed = $UpdateFailed
}
}
End {
Return $updateSummary
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment