Public/AddRemovePrograms/Show-CWAAAddRemove.ps1
|
function Show-CWAAAddRemove { <# .SYNOPSIS Shows the Automate agent in the Add/Remove Programs list. .DESCRIPTION Sets the SystemComponent registry value to 0 on Automate agent uninstall keys, which makes the agent visible in the Windows Add/Remove Programs (Programs and Features) list. Also cleans up any leftover HiddenProductName registry values from older hiding methods. .EXAMPLE Show-CWAAAddRemove Makes the Automate agent entry visible in Add/Remove Programs. .EXAMPLE Show-CWAAAddRemove -WhatIf Shows what registry changes would be made without applying them. .NOTES Author: Chris Taylor Alias: Show-LTAddRemove .LINK https://github.com/christaylorcodes/ConnectWiseAutomateAgent #> [CmdletBinding(SupportsShouldProcess = $True)] [Alias('Show-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 Show $($RegKey.GetValue('DisplayName'))")) { $RegEntriesChanged++ @('SystemComponent') | ForEach-Object { if (($RegKey.GetValue("$($_)")) -eq 1) { Write-Verbose "Setting $($RegRoot)\$($_)=0" Set-ItemProperty $RegRoot -Name "$($_)" -Value 0 -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 visible in Add/Remove Programs.' Write-CWAAEventLog -EventId 3050 -EntryType Information -Message 'Agent shown in Add/Remove Programs.' } elseif ($WhatIfPreference -ne $True) { Write-Warning "Automate agent may not be visible in Add/Remove Programs." Write-CWAAEventLog -EventId 3051 -EntryType Warning -Message 'Agent may not be visible in Add/Remove Programs.' } } Catch { Write-CWAAEventLog -EventId 3052 -EntryType Error -Message "Failed to show agent in Add/Remove Programs. Error: $($_.Exception.Message)" Write-Error "There was an error setting the registry values. $($_)" -ErrorAction Stop } } End { Write-Debug "Exiting $($MyInvocation.InvocationName)" } } |