Advertisement
Guest User

Untitled

a guest
Jan 15th, 2025
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 4.56 KB | Source Code | 0 0
  1. function Set-RegistryKey_AppInstalled
  2. {
  3.     <#
  4.         .SYNOPSIS
  5.          Detection Method for successful installed program.
  6.         .DESCRIPTION
  7.          Creates a registry key under HKLM\software\RM\AppStatus. The Key value is set to "Installed" or "Uninstalled"
  8.          The Variables are from the Deploy-Application.ps1 script.
  9.          The Function Call has to be made in the POST INSTALLATION part.
  10.    
  11.          If UninstallOldVersions does not work, you are most likly not complying with the naming standard.
  12.         .PARAMETER Uninstall
  13.             Switch parameter to set the application as uninstalled
  14.         .PARAMETER Remove
  15.             Switch parameter to remove the key, use the Uninstall parameter instead.
  16.         .PARAMETER UninstallOldVersions
  17.             Switch parameter to set the all old versions of the application as uninstalled
  18.             Normaly set in PRE-INSTALLATION after the removal of old versions
  19.         .EXAMPLE
  20.          Set-registryKey_AppInstalled
  21.    
  22.          Set-registryKey_AppInstalled -Uninstall
  23.    
  24.          Set-registryKey_AppInstalled -UninstallOldVersions
  25.         .LINK
  26.          http://psappdeploytoolkit.com/
  27.     #> 
  28.     [CmdletBinding(DefaultParameterSetName = "Uninstalled", SupportsShouldProcess = $True)]
  29.     Param (
  30.         [parameter(Mandatory = $false, ParameterSetName = "Uninstalled")]
  31.         [switch]$Uninstall,
  32.         [parameter(Mandatory = $false, ParameterSetName = "Remove")]
  33.         [switch]$Remove,
  34.         [parameter(Mandatory = $false, ParameterSetName = "UninstallOldVersions")]
  35.         [switch]$UninstallOldVersions
  36.     )
  37.    
  38.     Initialize-ADTFunction -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
  39.     $AppVendor = $adtSession.AppVendor
  40.     $AppName = $adtSession.AppName
  41.     $AppVersion = $adtSession.AppVersion
  42.     $AppRevision = $adtSession.AppRevision
  43.    
  44.     if ($Uninstall)
  45.     {
  46.         Write-ADTLogEntry -Message "Writing registry detection Method for successful Uninstalled program" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  47.         if (-not (Test-path -Path "HKLM:\SOFTWARE\RM\AppStatus")) { New-Item -Path "HKLM:\SOFTWARE\RM\AppStatus" }
  48.         Set-ItemProperty -Path "HKLM:\SOFTWARE\RM\AppStatus" -Name "$AppVendor $AppName $AppVersion $AppRevision" -Value "Uninstalled"
  49.         Write-ADTLogEntry -Message "Registry -> $AppVendor $AppName $AppVersion $AppRevision=Uninstalled" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  50.        
  51.     }
  52.     elseif ($Remove)
  53.     {
  54.         Write-ADTLogEntry -Message "Deleting registry detection Method for program" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  55.         Remove-ItemProperty -Path "HKLM:\SOFTWARE\RM\AppStatus" -Name "$AppVendor $AppName $AppVersion $AppRevision" -ErrorAction SilentlyContinue
  56.         Write-ADTLogEntry -Message "Registry -> $AppVendor $AppName $AppVersion $AppRevision" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  57.     }
  58.     elseif ($UninstallOldVersions)
  59.     {
  60.         Write-ADTLogEntry -Message "Writing registry detection Method for successful Uninstalled old versions of the program" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  61.         $AppCount = 0
  62.         $AppStatus = Get-RegistryKey -Key "HKLM:SOFTWARE\RM\AppStatus"
  63.         foreach ($item in $AppStatus.PSObject.Properties)
  64.         {
  65.             if ($item.name -like "$AppVendor $AppName *")
  66.             {
  67.                 if ($item.Value -eq "Installed" -and $item.Name -notlike "$AppVendor $AppName $AppVersion $AppRevision")
  68.                 {
  69.                     Set-ItemProperty -Path "HKLM:\SOFTWARE\RM\AppStatus" -Name $item.name -Value "Uninstalled"
  70.                     Write-ADTLogEntry -Message "Registry -> $($item.name)=Uninstalled" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  71.                     $AppCount++
  72.                 }
  73.             }
  74.         }
  75.         if ($AppCount -eq 0)
  76.         {
  77.             Write-ADTLogEntry -Message "No old application detection rule found" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  78.         }
  79.         elseif ($AppCount -eq 1)
  80.         {
  81.             Write-ADTLogEntry -Message "1 detection rule set to uninstalled" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  82.         }
  83.         else
  84.         {
  85.             Write-ADTLogEntry -Message "$AppCount detection rules set to uninstalled" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  86.         }
  87.     }
  88.     else
  89.     {
  90.         Write-ADTLogEntry -Message "Writing registry detection Method for successful installed program" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  91.         if (-not (Test-path -Path "HKLM:\SOFTWARE\RM")) { New-Item -Path "HKLM:\SOFTWARE\RM" }
  92.         if (-not (Test-path -Path "HKLM:\SOFTWARE\RM\AppStatus")) { New-Item -Path "HKLM:\SOFTWARE\RM\AppStatus" }
  93.         Set-ItemProperty -Path "HKLM:\SOFTWARE\RM\AppStatus" -Name "$AppVendor $AppName $AppVersion $AppRevision" -Value "Installed"
  94.         Write-ADTLogEntry -Message "Registry -> $AppVendor $AppName $AppVersion $AppRevision=Installed" -Source 'Set-RegistryKey_AppInstalled' -LogType 'CMTrace'
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement