Powershell menu to perform WSUS tasks

After my previous post, WSUS is actually a bit more complicated than I’d thought so I’ve developed an entire, stand-alone WSUS script because that previous one was getting quite big.

A couple of notes: the location of the log files aren’t really important, when I started with the script it was just listing the updates inside the PowerShell windows which wasn’t very visually friendly. So I stuck them in a text file to make it easier to read. The other thing is, it doesn’t seem to run properly immediately after a reboot- I don’t know if this is just my Server Core 2012 VM being a bit sluggish, but it just does nothing if you run it within 5 or 10 minutes of rebooting. But it does work, and I’ve kept an eye on our WSUS Server and it now has all relevant updates installed.

Oh great. All the formatting has gone as well. Sorry about that, it was neatly tabbed before…

EDIT 17-MAY-2013: After some problems with my main W8 host, I’ve modified the script below to remove the option not to reboot (which I’m not sure worked anyway). So now the script automatically reboots after running a batch of updates. I’ve left the code in for interest, but would strongly advise removing the lines highlighted in red below.

======================================================================================================================================================================================
#Split in to three main sections: first of all just searches for updates, next option is to install, then final option is to display installed updates

#Equivalent to DOS CLS- just wipes the console screen
Clear-Host

#Sets the menu up- this is a 4-option menu
$menuText=”1) Display new (not yet installed) updates in Notepad”,”2) Download and install new updates”,”3) Display installed updates in Notepad”,”4) Quit”

#Displays the menu options
$menuText

do
{
#Starts the menu system- asks for user to make a choice
switch ($chooseOption=Read-Host -Prompt “Please choose your option”)
{
1 {
#Deletes the old log file- this stops the log file displaying false (too many) updates
Remove-Item C:\Support\new_WSUS_Patches.log
#Defines update criteria
$newUpdateCriteria = “IsInstalled=0 and Type=’Software'”
#Search for new updates.
$newUpdateSearcher = New-Object -ComObject Microsoft.Update.Searcher
$newSearchResults = $newUpdateSearcher.Search($newUpdateCriteria)
#Catches 0 updates error, gracefully exists when there are no new updates
If ($newSearchResults.updates.Count -eq 0) {
Clear-Host
Write-Host “I couldn’t find any updates…”
Start-Sleep 5
}
Else {
#This just performs the search, outputs the results to a text file and then opens the text file
$newSearchResults.Updates | ForEach-Object {Out-File -FilePath C:\Support\new_WSUS_Patches.log -Append -InputObject $_.Title }
Invoke-Item C:\Support\new_WSUS_Patches.log
}
cls
$menuText
}
2 {
#Define update criteria.
$newUpdateCriteria = “IsInstalled=0 and Type=’Software'”
#Search for new updates.
$newUpdateSearcher = New-Object -ComObject Microsoft.Update.Searcher
$newSearchResults = $newUpdateSearcher.Search($newUpdateCriteria)
If ($newSearchResults.updates.Count -eq 0) {
Clear-Host
Write-Host “I couldn’t find any updates…”
Start-Sleep 5
cls
$menuText
}
Else {
#Download updates.
$Session = New-Object -ComObject Microsoft.Update.Session
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $newSearchResults.Updates
$Downloader.Download()

#Install updates.
$Installer = New-Object -ComObject Microsoft.Update.Installer
$Installer.Updates = $newSearchResults.Updates
$installUpdates = $Installer.Install()
#Reboot if required by updates.
If ($Result.rebootRequired) {
Clear-Host
Write-Host “This computer needs to be restarted so will do so in 1 minute”
Start-Sleep 60
Restart-Computer
}
Else {
Clear-Host
Write-Host “Reboot NOT required!”
Start-Sleep 5
cls
$menuText
}
}
}
3 {
#Deletes the old log file
Remove-Item C:\Support\installed_WSUS_Patches.log
#Define update criteria.
$installedUpdateCriteria = “IsInstalled=1 and Type=’Software'”
#Search for installed updates.
$installedUpdateSearcher = New-Object -ComObject Microsoft.Update.Searcher
$installedSearchResults = $installedUpdateSearcher.Search($installedUpdateCriteria)
$installedSearchResults.Updates | ForEach-Object {Out-File -FilePath C:\Support\installed_WSUS_Patches.log -Append -InputObject $_.Title }
Invoke-Item C:\Support\installed_WSUS_Patches.log
cls
$menuText
}
4 {Break}
}
#As soon as the user preses 4, the script exits
} while ($chooseOption -ne 4)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s