Get-PC/Private/GPUpdate.ps1

29 lines
1.1 KiB
PowerShell
Raw Normal View History

2024-06-11 18:27:55 +00:00
Function Get-GPUpdate($ComputerName){
Write-Host "GPUPDATE on $ComputerName" -ForeGround Green
#Looks on the machine and tries to access the group policy.
try {
$oldPolicy = Get-ChildItem \\$ComputerName\c$\Windows\System32\GroupPolicy\Machine
}
catch{
Write-Host "Failed to access group policy" -ForegroundColor Red
return
}
#Deletes the old group policy. Reasoning is that if we are running this there is something very wrong and we will be immediately re-applying it.
if(!$oldPolicy){
Write-Host "No group policy found on machine" -ForegroundColor Red
}
else{
Remove-Item $oldPolicy
}
#Attempts to remotely re-apply group policy. If this fails I let the user know so they can go to the computer and manually do it.
try {
Invoke-Command -ComputerName $ComputerName -ScriptBlock {& gpupdate /force}
}
catch{
Write-Host "Unable to reapply group policy" -ForegroundColor Red
return
}
Write-Host 'GPUpdate Finished' -ForegroundColor Green
}