Categories

Windows updates: 4 tidy up get-update

Looking at the get-update function we created earlier I wanted to tidy it up a bit.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033

function get-update {
[CmdletBinding()] 
param
 ( 
 
[switch]$hidden
 
) 

PROCESS{

$session = New-Object -ComObject Microsoft.Update.Session
$searcher = $session.CreateUpdateSearcher()

# 0 = false & 1 = true
if ($hidden
){
 
$result = $searcher.Search("IsInstalled=0 and Type='Software' and ISHidden=1"
 )
}

else
 {
 
$result = $searcher.Search("IsInstalled=0 and Type='Software' and ISHidden=0"
 )
}


if ($result.Updates.Count -gt 0
){
 
$result.Updates |
 
 
select Title, IsHidden, IsDownloaded, IsMandatory,
 
 
IsUninstallable, RebootRequired, Description
}
else
 {
 
Write-Host " No updates available"
} 

}
#process

<# .SYNOPSIS Discovers available updates .DESCRIPTION Interrogates Windows updates for available software updates only. Optional parameter to display hidden updates .PARAMETER hidden A switch to display the hidden updates .EXAMPLE get-update Displays non-hidden updates .EXAMPLE get-update -hidden Displays hidden updates #>



}

Primary changes are:

  • to add a switch to the function to control the display of hidden updates
  • count the available updates and display message if there aren’t any
  • add more properties to the output- 7 properties defaults to a list so more readable as well
  • add comment based help

Leave a Reply