:: Log file set LOGFILE=%USERPROFILE%\Desktop\GPUpdate_Log_%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%_%TIME:~0,2%%TIME:~3,2%.txt set LOGFILE=%LOGFILE: =0%
$result = Invoke-Command -ComputerName $ComputerName -ScriptBlock $scriptBlock -ArgumentList $Force $exitCode = $result.ExitCode "[$(Get-Date)] Exit code: $exitCode" | Out-File -FilePath $logFile -Append Interpret exit codes switch ($exitCode) 0 Write-Host "Success: Policy updated." -ForegroundColor Green 1 Write-Host "Warning: Policy updated but reboot required." -ForegroundColor Yellow 2 Write-Host "Warning: Policy updated but logoff required." -ForegroundColor Yellow 3 Write-Host "Error: Access denied (run as admin)." -ForegroundColor Red default Write-Host "Unknown error code: $exitCode" -ForegroundColor Red Auto-reboot if flagged if ($exitCode -eq 1 -and $RebootIfNeeded -and $ComputerName -eq $env:COMPUTERNAME) Write-Host "Rebooting in 30 seconds..." -ForegroundColor Magenta shutdown /r /t 30 /c "GPUpdate required reboot" windows gpupdate
if %errorLevel% equ 0 ( echo [SUCCESS] Policy update completed. echo [%DATE% %TIME%] Success >> "%LOGFILE%" ) else ( echo [FAILED] Error code %errorLevel%. Check log. echo [%DATE% %TIME%] Failed with code %errorLevel% >> "%LOGFILE%" ) echo [%DATE% %TIME%] Failed with code %errorLevel% >>
Write-Host "Log saved to: $logFile" :: Force policy update & wait up to 5 minutes gpupdate /force /wait:300 :: Force & reboot if necessary (for security policies) gpupdate /force /boot windows gpupdate