Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $Computers = @("Computer1", "Computer2", "Computer3")
- Invoke-Command -ComputerName $Computers -ScriptBlock {
- Import-Module PSWindowsUpdate
- # Path to log file
- $LogPath = "C:\Scripts\Logs\Update-Windows.log"
- function Write-Log {
- param ([string]$Message)
- $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
- Add-Content -Path $LogPath -Value "$timestamp - $Message"
- }
- Write-Log "Starting Windows Update process."
- try {
- # Check for updates
- $Updates = Get-WindowsUpdate -AcceptAll -IgnoreReboot
- if ($Updates) {
- Write-Log "Updates found: $($Updates.Count). Installing updates."
- Install-WindowsUpdate -AcceptAll -AutoReboot -Verbose | Tee-Object -FilePath $LogPath -Append
- Write-Log "Updates installed successfully."
- } else {
- Write-Log "No updates available at this time."
- }
- }
- catch {
- Write-Log "Error during update process: $_"
- }
- } -Credential (Get-Credential)
Advertisement
Add Comment
Please, Sign In to add comment