Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Set-RegistryKey_AppInstalled
- {
- <#
- .SYNOPSIS
- Detection Method for successful installed program.
- .DESCRIPTION
- Creates a registry key under HKLM\software\RM\AppStatus. The Key value is set to "Installed" or "Uninstalled"
- The Variables are from the Deploy-Application.ps1 script.
- The Function Call has to be made in the POST INSTALLATION part.
- If UninstallOldVersions does not work, you are most likly not complying with the naming standard.
- .PARAMETER Uninstall
- Switch parameter to set the application as uninstalled
- .PARAMETER Remove
- Switch parameter to remove the key, use the Uninstall parameter instead.
- .PARAMETER UninstallOldVersions
- Switch parameter to set the all old versions of the application as uninstalled
- Normaly set in PRE-INSTALLATION after the removal of old versions
- .EXAMPLE
- Set-registryKey_AppInstalled
- Set-registryKey_AppInstalled -Uninstall
- Set-registryKey_AppInstalled -UninstallOldVersions
- .LINK
- http://psappdeploytoolkit.com/
- #>
- [CmdletBinding(DefaultParameterSetName = "Uninstalled", SupportsShouldProcess = $True)]
- Param (
- [parameter(Mandatory = $false, ParameterSetName = "Uninstalled")]
- [switch]$Uninstall,
- [parameter(Mandatory = $false, ParameterSetName = "Remove")]
- [switch]$Remove,
- [parameter(Mandatory = $false, ParameterSetName = "UninstallOldVersions")]
- [switch]$UninstallOldVersions
- )
- Initialize-ADTFunction -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
- $AppVendor = $adtSession.AppVendor
- $AppName = $adtSession.AppName
- $AppVersion = $adtSession.AppVersion
- $AppRevision = $adtSession.AppRevision
- if ($Uninstall)
- {
- Write-ADTLogEntry -Message "Writing registry detection Method for successful Uninstalled program" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- if (-not (Test-path -Path "HKLM:\SOFTWARE\RM\AppStatus")) { New-Item -Path "HKLM:\SOFTWARE\RM\AppStatus" }
- Set-ItemProperty -Path "HKLM:\SOFTWARE\RM\AppStatus" -Name "$AppVendor $AppName $AppVersion $AppRevision" -Value "Uninstalled"
- Write-ADTLogEntry -Message "Registry -> $AppVendor $AppName $AppVersion $AppRevision=Uninstalled" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- }
- elseif ($Remove)
- {
- Write-ADTLogEntry -Message "Deleting registry detection Method for program" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- Remove-ItemProperty -Path "HKLM:\SOFTWARE\RM\AppStatus" -Name "$AppVendor $AppName $AppVersion $AppRevision" -ErrorAction SilentlyContinue
- Write-ADTLogEntry -Message "Registry -> $AppVendor $AppName $AppVersion $AppRevision" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- }
- elseif ($UninstallOldVersions)
- {
- Write-ADTLogEntry -Message "Writing registry detection Method for successful Uninstalled old versions of the program" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- $AppCount = 0
- $AppStatus = Get-RegistryKey -Key "HKLM:SOFTWARE\RM\AppStatus"
- foreach ($item in $AppStatus.PSObject.Properties)
- {
- if ($item.name -like "$AppVendor $AppName *")
- {
- if ($item.Value -eq "Installed" -and $item.Name -notlike "$AppVendor $AppName $AppVersion $AppRevision")
- {
- Set-ItemProperty -Path "HKLM:\SOFTWARE\RM\AppStatus" -Name $item.name -Value "Uninstalled"
- Write-ADTLogEntry -Message "Registry -> $($item.name)=Uninstalled" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- $AppCount++
- }
- }
- }
- if ($AppCount -eq 0)
- {
- Write-ADTLogEntry -Message "No old application detection rule found" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- }
- elseif ($AppCount -eq 1)
- {
- Write-ADTLogEntry -Message "1 detection rule set to uninstalled" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- }
- else
- {
- Write-ADTLogEntry -Message "$AppCount detection rules set to uninstalled" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- }
- }
- else
- {
- Write-ADTLogEntry -Message "Writing registry detection Method for successful installed program" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- if (-not (Test-path -Path "HKLM:\SOFTWARE\RM")) { New-Item -Path "HKLM:\SOFTWARE\RM" }
- if (-not (Test-path -Path "HKLM:\SOFTWARE\RM\AppStatus")) { New-Item -Path "HKLM:\SOFTWARE\RM\AppStatus" }
- Set-ItemProperty -Path "HKLM:\SOFTWARE\RM\AppStatus" -Name "$AppVendor $AppName $AppVersion $AppRevision" -Value "Installed"
- Write-ADTLogEntry -Message "Registry -> $AppVendor $AppName $AppVersion $AppRevision=Installed" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement