Friday 23 November 2018

Registry wildcard search match itemproperty

The following PowerShell code is part of a larger App Deployment Toolkit for troubleshooting installation problems with Adobe Reader. The issue faced was a '1612' CCM error when trying to upgrade to the latest version. A number of PCs were displaying errors in their logs that the source file for the previous version could not be located. Troubleshooting led to Registry entries that were pointing at CCM cache folders that no longer existed. Recreating these folders and placing the relevant .msi in there allowed for a successful upgrade.

The script searches through the Installer keys of the Registry looking for Adobe Reader, it then gets the property value that identifies the CCM cache folder last used and, if it does not exist, create it and copy the .msi files into it. Finally, initiate the upgrade again.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
#Checks success value of previous code and only initiates if a '1612' error is returned
If (($ReturnCode3.ExitCode -eq '1612') -or ($ReturnCode4.ExitCode -eq '1612')) {

    Write-Log 'Install Attempt 2 has failed'


    #Use wildcard to search through Registry keys for a specific value
    $key = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Classes\Installer\Products\*' -ErrorAction SilentlyContinue | Where-Object {((Get-ItemProperty -Path $_.PsPath) -match 'AdbeRdr11000')}
       
        ForEach-Object {
                   
            #Search Registry keys for 'Transforms'
            $keytransforms = $key | Get-ItemPropertyValue -Name Transforms
               
            ForEach ($productcode in $keytransforms) {
                #Split the string via '\\' and assign the 3rd part to a variable
                $productcodeshort = $productcode.Split('\\')[3]
            }

            #Get ccmcache folder name that was used for program installation
            $ccmcachekey = $key | Get-ChildItem | Where-Object ({((Get-ItemProperty -Path $_.PsPath) -match 'SourceList')})
            $ccmcachekeysource = $ccmcachekey | Get-ItemPropertyValue -Name LastUsedSource

            ForEach ($cachefolder in $ccmcachekeysource) {
                $ccmcachefolder = $cachefolder.Split('\\')[3]
            }

                #Check if ccmcache folder exists
                If (-Not(Test-Path ($ccmfolder = "C:\Windows\ccmcache\$ccmcachefolder"))) {
                   
                    #Create new folder in directory if not already exist
                    New-Item -Path $ccmfolder -ItemType Directory
                    Write-Log "Created $ccmfolder"
                   
                    #Copy relevant Product Code folder into ccm folder
                    Copy-Item -Path "$dirFiles\*" -Destination $ccmfolder -Recurse
                    Write-Log "Copied $productcodeshort folder"
                   
                }
                #Do nothing if ccmcache folder already exists
                Else {
                    Write-Log "$ccmfolder already exists"
                }
        }


    #Re-run Adobe Reader installation (uses Transform file to alter configuration)
    Execute-MSI -Action 'Install' -Path "$DirSupportFiles\AcroRead.msi" -Transform "$DirFiles\AcroRead.mst" -Parameters '/q'
    Execute-MSI -Action 'Patch' -Path "$DirSupportFiles\AcroRdr2017Upd1701130078_MUI.msp" -Parameters '/q'
    }

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Registry wildcard search match itemproperty.ps1

No comments:

Post a Comment