Public/AddRemovePrograms/Hide-CWAAAddRemove.ps1
|
function Hide-CWAAAddRemove { <# .SYNOPSIS Hides the Automate agent from the Add/Remove Programs list. .DESCRIPTION Sets the SystemComponent registry value to 1 on Automate agent uninstall keys, which hides the agent from the Windows Add/Remove Programs (Programs and Features) list. Also cleans up any leftover HiddenProductName registry values from older hiding methods. .EXAMPLE Hide-CWAAAddRemove Hides the Automate agent entry from Add/Remove Programs. .EXAMPLE Hide-CWAAAddRemove -WhatIf Shows what registry changes would be made without applying them. .NOTES Author: Chris Taylor Alias: Hide-LTAddRemove .LINK https://github.com/christaylorcodes/ConnectWiseAutomateAgent #> [CmdletBinding(SupportsShouldProcess = $True)] [Alias('Hide-LTAddRemove')] Param() Begin { Write-Debug "Starting $($MyInvocation.InvocationName)" $RegRoots = $Script:CWAAInstallerProductKeys $PublisherRegRoots = $Script:CWAAUninstallKeys $RegEntriesFound = 0 $RegEntriesChanged = 0 } Process { Try { foreach ($RegRoot in $RegRoots) { if (Test-Path $RegRoot) { if (Get-ItemProperty $RegRoot -Name HiddenProductName -ErrorAction SilentlyContinue) { if (!(Get-ItemProperty $RegRoot -Name ProductName -ErrorAction SilentlyContinue)) { Write-Verbose 'Automate agent found with HiddenProductName value.' Try { Rename-ItemProperty $RegRoot -Name HiddenProductName -NewName ProductName } Catch { Write-Error "There was an error renaming the registry value. $($_)" -ErrorAction Stop } } else { Write-Verbose 'Automate agent found with unused HiddenProductName value.' Try { Remove-ItemProperty $RegRoot -Name HiddenProductName -EA 0 -Confirm:$False -WhatIf:$False -Force } Catch { Write-Debug "Failed to remove unused HiddenProductName from '$RegRoot': $($_)" } } } } } foreach ($RegRoot in $PublisherRegRoots) { if (Test-Path $RegRoot) { $RegKey = Get-Item $RegRoot -ErrorAction SilentlyContinue if ($RegKey) { $RegEntriesFound++ if ($PSCmdlet.ShouldProcess("$($RegRoot)", "Set Registry Values to Hide $($RegKey.GetValue('DisplayName'))")) { $RegEntriesChanged++ @('SystemComponent') | ForEach-Object { if (($RegKey.GetValue("$($_)")) -ne 1) { Write-Verbose "Setting $($RegRoot)\$($_)=1" Set-ItemProperty $RegRoot -Name "$($_)" -Value 1 -Type DWord -WhatIf:$False -Confirm:$False -Verbose:$False } } } } } } # Output success/warning at end of try block (replaces if($?) pattern in End block) if ($RegEntriesFound -gt 0 -and $RegEntriesChanged -eq $RegEntriesFound) { Write-Output 'Automate agent is hidden from Add/Remove Programs.' Write-CWAAEventLog -EventId 3040 -EntryType Information -Message 'Agent hidden from Add/Remove Programs.' } elseif ($WhatIfPreference -ne $True) { Write-Warning "Automate agent may not be hidden from Add/Remove Programs." Write-CWAAEventLog -EventId 3041 -EntryType Warning -Message 'Agent may not be hidden from Add/Remove Programs.' } } Catch { Write-CWAAEventLog -EventId 3042 -EntryType Error -Message "Failed to hide agent from Add/Remove Programs. Error: $($_.Exception.Message)" Write-Error "There was an error setting the registry values. $($_)" -ErrorAction Stop } } End { Write-Debug "Exiting $($MyInvocation.InvocationName)" } } |