Public/AddRemovePrograms/Rename-CWAAAddRemove.ps1
|
function Rename-CWAAAddRemove { <# .SYNOPSIS Renames the Automate agent entry in the Add/Remove Programs list. .DESCRIPTION Changes the DisplayName (and optionally Publisher) registry values for the Automate agent uninstall keys, which controls how the agent appears in the Windows Add/Remove Programs (Programs and Features) list. .PARAMETER Name The display name for the Automate agent as shown in the list of installed software. .PARAMETER PublisherName The publisher name for the Automate agent as shown in the list of installed software. .EXAMPLE Rename-CWAAAddRemove -Name 'My Remote Agent' Renames the Automate agent display name to 'My Remote Agent'. .EXAMPLE Rename-CWAAAddRemove -Name 'My Remote Agent' -PublisherName 'My Company' Renames both the display name and publisher name in Add/Remove Programs. .NOTES Author: Chris Taylor Alias: Rename-LTAddRemove .LINK https://github.com/christaylorcodes/ConnectWiseAutomateAgent #> [CmdletBinding(SupportsShouldProcess = $True)] [Alias('Rename-LTAddRemove')] Param( [Parameter(Mandatory = $True)] $Name, [Parameter(Mandatory = $False)] [AllowNull()] [string]$PublisherName ) Begin { Write-Debug "Starting $($MyInvocation.InvocationName)" $RegRoots = @($Script:CWAAUninstallKeys[0], $Script:CWAAUninstallKeys[1]) + $Script:CWAAInstallerProductKeys $PublisherRegRoots = $Script:CWAAUninstallKeys $RegNameFound = 0 $RegPublisherFound = 0 } Process { Try { foreach ($RegRoot in $RegRoots) { if (Get-ItemProperty $RegRoot -Name DisplayName -ErrorAction SilentlyContinue) { if ($PSCmdlet.ShouldProcess("$($RegRoot)\DisplayName=$($Name)", 'Set Registry Value')) { Write-Verbose "Setting $($RegRoot)\DisplayName=$($Name)" Set-ItemProperty $RegRoot -Name DisplayName -Value $Name -Confirm:$False $RegNameFound++ } } elseif (Get-ItemProperty $RegRoot -Name HiddenProductName -ErrorAction SilentlyContinue) { if ($PSCmdlet.ShouldProcess("$($RegRoot)\HiddenProductName=$($Name)", 'Set Registry Value')) { Write-Verbose "Setting $($RegRoot)\HiddenProductName=$($Name)" Set-ItemProperty $RegRoot -Name HiddenProductName -Value $Name -Confirm:$False $RegNameFound++ } } } } Catch { Write-CWAAEventLog -EventId 3062 -EntryType Error -Message "Failed to rename agent in Add/Remove Programs. Error: $($_.Exception.Message)" Write-Error "There was an error setting the DisplayName registry value. $($_)" -ErrorAction Stop } if (($PublisherName)) { Try { foreach ($RegRoot in $PublisherRegRoots) { if (Get-ItemProperty $RegRoot -Name Publisher -ErrorAction SilentlyContinue) { if ($PSCmdlet.ShouldProcess("$($RegRoot)\Publisher=$($PublisherName)", 'Set Registry Value')) { Write-Verbose "Setting $($RegRoot)\Publisher=$($PublisherName)" Set-ItemProperty $RegRoot -Name Publisher -Value $PublisherName -Confirm:$False $RegPublisherFound++ } } } } Catch { Write-CWAAEventLog -EventId 3062 -EntryType Error -Message "Failed to set agent publisher name. Error: $($_.Exception.Message)" Write-Error "There was an error setting the Publisher registry value. $($_)" -ErrorAction Stop } } # Output success/warning (replaces if($?) pattern formerly in End block). # Guarded by $WhatIfPreference because SupportsShouldProcess is enabled and # these messages would be misleading during a -WhatIf dry run. if ($WhatIfPreference -ne $True) { if ($RegNameFound -gt 0) { Write-Output "Automate agent is now listed as $($Name) in Add/Remove Programs." Write-CWAAEventLog -EventId 3060 -EntryType Information -Message "Agent display name changed to '$Name' in Add/Remove Programs." } else { Write-Warning "Automate agent was not found in installed software and the Name was not changed." Write-CWAAEventLog -EventId 3061 -EntryType Warning -Message "Agent not found in installed software. Display name not changed." } if (($PublisherName)) { if ($RegPublisherFound -gt 0) { Write-Output "The Publisher is now listed as $($PublisherName)." Write-CWAAEventLog -EventId 3060 -EntryType Information -Message "Agent publisher changed to '$PublisherName' in Add/Remove Programs." } else { Write-Warning "Automate agent was not found in installed software and the Publisher was not changed." Write-CWAAEventLog -EventId 3061 -EntryType Warning -Message "Agent not found in installed software. Publisher name not changed." } } } } End { Write-Debug "Exiting $($MyInvocation.InvocationName)" } } |