Guest User

WIndows Update script for remote PCs

a guest
Jan 9th, 2025
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $Computers = @("Computer1", "Computer2", "Computer3")
  2.  
  3. Invoke-Command -ComputerName $Computers -ScriptBlock {
  4.     Import-Module PSWindowsUpdate
  5.  
  6.     # Path to log file
  7.     $LogPath = "C:\Scripts\Logs\Update-Windows.log"
  8.  
  9.     function Write-Log {
  10.         param ([string]$Message)
  11.         $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
  12.         Add-Content -Path $LogPath -Value "$timestamp - $Message"
  13.     }
  14.  
  15.     Write-Log "Starting Windows Update process."
  16.  
  17.     try {
  18.         # Check for updates
  19.         $Updates = Get-WindowsUpdate -AcceptAll -IgnoreReboot
  20.  
  21.         if ($Updates) {
  22.             Write-Log "Updates found: $($Updates.Count). Installing updates."
  23.             Install-WindowsUpdate -AcceptAll -AutoReboot -Verbose | Tee-Object -FilePath $LogPath -Append
  24.             Write-Log "Updates installed successfully."
  25.         } else {
  26.             Write-Log "No updates available at this time."
  27.         }
  28.     }
  29.     catch {
  30.         Write-Log "Error during update process: $_"
  31.     }
  32. } -Credential (Get-Credential)
Advertisement
Add Comment
Please, Sign In to add comment