From 4671387d85db3f5abbedeec87f50578b9f65f7f8 Mon Sep 17 00:00:00 2001 From: Zachary Gorman Date: Thu, 20 Mar 2025 12:59:59 -0700 Subject: [PATCH] Better gpUpdate --- Get-PC/Get-PC.psd1 | 2 +- Get-PC/Private/GPUpdate.ps1 | 19 +++++++++++++++---- Get-PC/patchnotes.txt | 3 +++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Get-PC/Get-PC.psd1 b/Get-PC/Get-PC.psd1 index 3578997..4570403 100644 --- a/Get-PC/Get-PC.psd1 +++ b/Get-PC/Get-PC.psd1 @@ -12,7 +12,7 @@ RootModule = 'Get-PC.psm1' # Version number of this module. -ModuleVersion = '0.4.18' +ModuleVersion = '0.4.19' # Supported PSEditions # CompatiblePSEditions = @() diff --git a/Get-PC/Private/GPUpdate.ps1 b/Get-PC/Private/GPUpdate.ps1 index 554cfd8..c30e97e 100644 --- a/Get-PC/Private/GPUpdate.ps1 +++ b/Get-PC/Private/GPUpdate.ps1 @@ -4,22 +4,33 @@ #Looks on the machine and tries to access the group policy. try { - $oldPolicy = Get-ChildItem \\$ComputerName\c$\Windows\System32\GroupPolicy\Machine + $oldPolicy = Get-ChildItem \\$ComputerName\c$\Windows\System32\GroupPolicy + $oldPolicyUsers = Get-ChildItem \\$ComputerName\c$\Windows\System32\GroupPolicyUsers } 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){ + if(!$oldPolicy -and !$oldPolicyUsers){ Write-Host "No group policy found on machine" -ForegroundColor Red } else{ - Remove-Item $oldPolicy + Remove-Item $oldPolicy -ErrorAction 'SilentlyContinue' + Remove-Item $oldPolicyUsers -ErrorAction 'SilentlyContinue' } + #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 -SessionOption (New-PSSessionOption -NoMachineProfile) -ScriptBlock {& gpupdate /force} + Invoke-Command -ComputerName $ComputerName -SessionOption (New-PSSessionOption -NoMachineProfile) -ScriptBlock { + @( + "HKLM\Software\Policies\Microsoft", + "HKCU\Software\Policies\Microsoft", + "HKCU\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects", + "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies" + ) | ForEach-Object { Remove-Item -Recurse "Registry::$_" } + & gpupdate /force + } } catch{ Write-Host "Unable to reapply group policy" -ForegroundColor Red diff --git a/Get-PC/patchnotes.txt b/Get-PC/patchnotes.txt index 3beab1a..0f9fa93 100644 --- a/Get-PC/patchnotes.txt +++ b/Get-PC/patchnotes.txt @@ -1,3 +1,6 @@ +Patch 2025-3-20 +-Update GPUpdate is now more robust by clearing more files and registry keys + Patch 2025-3-14 -Fix Queries Spark! by asset tag rather than hostname to also hit in stock workstations -Add Elo monitor ports to -devices