[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1914
  • Last Modified:

How to use PowerShell to Install Windows Updates?

On some servers we push out WSUS updates however we wish to manually install them.  The updates are already downloaded and ready to install.  We RDP to each server and install the pending updates.  I'd like to have PowerShell script that I could run to do this.  I have the first part which shows me the pending updates.  I just need to install them, the things I've tried give me an error "Exception calling "Install" with "0" arguments.  Any help would be appreciated.

 
$objSession=New-Object -com "Microsoft.Update.Session"
$objSearcher=$objSession.CreateUpdateSearcher()
$updates=$objSearcher.search("IsInstalled=0 and Type='Software'")
$UpdateCount = $updates.updates.count

# Display pending updates and prompt to continue.
If ($UpdateCount -gt 0)
  {
  # Loop through items and display pending updates.
  CLS
  Write-Host -ForegroundColor Green "$UpdateCount Pending Updates (Downloaded and Ready to Install):"
    Do  
    {
    Write-Host
    Write-Host -ForegroundColor Cyan $updates.Updates.Item($i).title
    $i++
    }
    Until ($i -eq $UpdateCount)
	
  Write-Host
  $Input = Read-Host "Continue with install? (y/N)"
    If (($input.toupper()) -eq "Y")
    {
    # Install Updates Code Here
    }

#####################
#Here's a sample installer I found but this doesn't work.

#Install Windows Updates
$objInstaller=New-Object -com "Microsoft.Update.Installer"
$objSession=New-Object -com "Microsoft.Update.Session"
$objSearcher=$objSession.CreateUpdateSearcher()
$objResults=$objSearcher.search("IsInstalled=0")
$objInstaller.Updates=$objResults.updates
foreach ($update in $objresults)
{
$objInstaller.install()
}

Open in new window

0
DigitalInfuzion
Asked:
DigitalInfuzion
  • 4
  • 2
1 Solution
 
AdamRobinsonCommented:
If you haven't already, you might want to check out: http://www.windows-scripting.info/install-windows-updates-windows-powershell.html 

Specifically relating to your error, and the common workaround.
0
 
DigitalInfuzionAuthor Commented:
Adam, thanks for the link.  I'll look into this though, it would be nicer to be able to install directly.
0
 
DigitalInfuzionAuthor Commented:
I looked into the solution provided and it helped me with another issue however it's not really the answer for my issue.  I found a .VBS script that does what exactly what I need, so I'm going to use that.  Thanks for the information.
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
AdamRobinsonCommented:
Can you provide a link to the .vbs script for future people inquiring on this issue?
0
 
DigitalInfuzionAuthor Commented:
Here's the .VBS code that I'm using.  It displays a list of any updates that are waiting to be installed and prompts you to install now.  Once script is complete, it beeps twice.  You then will need to reboot (for most updates) though you will not be prompted.  I have a .CMD file that calls this on my desktop [cscript c:\scripts\install_updates.vbs].
For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
Next

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

WScript.Echo  vbCRLF & _
"Creating collection of downloaded updates to install:" 

For I = 0 To searchResult.Updates.Count-1
    set update = searchResult.Updates.Item(I)
    If update.IsDownloaded = true Then
       WScript.Echo I + 1 & "> adding:  " & update.Title 
       updatesToInstall.Add(update)	
    End If
Next


'*** Install Downloaded Updates ***
WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo 

If (strInput = "N" or strInput = "n") Then 
	WScript.Quit
ElseIf (strInput = "Y" or strInput = "y") Then
	WScript.Echo "Installing updates..."
	Set installer = updateSession.CreateUpdateInstaller()
	installer.Updates = updatesToInstall
	Set installationResult = installer.Install()
	
	'Output results of install
	WScript.Echo "Installation Result: " & _
	installationResult.ResultCode 
	WScript.Echo "Reboot Required: " & _ 
	installationResult.RebootRequired & vbCRLF 
	WScript.Echo "Listing of updates installed " & _
	 "and individual installation results:" 
	
	For I = 0 to updatesToInstall.Count - 1
		WScript.Echo I + 1 & "> " & _
		updatesToInstall.Item(i).Title & _
		": " & installationResult.GetUpdateResult(i).ResultCode 		
	Next
End If

'*** Beep Twice ***
Do
set wshShell = Wscript.CreateObject("wscript.Shell")
beep = chr(007)
WshShell.Run "cmd /c @echo " & beep, 0
x=x+1

Open in new window

0
 
DigitalInfuzionAuthor Commented:
Here's the solution I found and I'm using.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now