<# .SYNOPSIS Smart gpupdate with logging, reboot control, and remote support. .PARAMETER ComputerName Target computer (default: localhost). Requires PSRemoting. .PARAMETER Force Use /force (default: true) .PARAMETER RebootIfNeeded Automatically reboot if required. .EXAMPLE .\Invoke-GPUpdate.ps1 .\Invoke-GPUpdate.ps1 -ComputerName PC-001 -Force $true -RebootIfNeeded $false #> param( [string]$ComputerName = $env:COMPUTERNAME, [bool]$Force = $true, [bool]$RebootIfNeeded = $false )
if ($ComputerName -eq $env:COMPUTERNAME) Write-Host "Running gpupdate locally..." -ForegroundColor Cyan $params = if ($Force) "/force" else "" $result = Start-Process -Wait -NoNewWindow -FilePath "gpupdate.exe" -ArgumentList $params -PassThru $exitCode = $result.ExitCode else Write-Host "Running gpupdate on $ComputerName..." -ForegroundColor Cyan $scriptBlock = param($forceFlag) if ($forceFlag) gpupdate /force else gpupdate
:: Perform /force echo. echo [ACTION] Running gpupdate /force /wait:120 ... gpupdate /force /wait:120 >> "%LOGFILE%" 2>&1
:: Force & logoff if necessary (for user policies) gpupdate /force /logoff
@echo off title GPUpdate Smart Tool color 0A echo ============================================== echo SMART GPUPDATE UTILITY echo ============================================== echo. :: Check admin rights net session >nul 2>&1 if %errorLevel% neq 0 ( echo [ERROR] Please run as Administrator. pause exit /b 1 )