Get-InstalledApplication Fails because white space is stripped

Topics: Archive - Deployment Scripts
Mar 20, 2015 at 7:08 AM
I encountered an annoying anomaly working on a script to upgrade VLC from v2.1.5 to v2.2.0 with (detection) logic to exit with a message indicating the product is installed on machines where v2.2.0 exists.

I started out with the following condition
if (Get-InstalledApplication -Name "VLC media player") {

            Show-InstallationPrompt -Message "An installation of $appName $appVersion exists." -ButtonRightText 'OK' -Icon Information -NoWait; Write-Log "ErrorCode Return value is: 1638, proceeding."; Exit-Script -ExitCode 1638
        } else {

        #VLC Media Player installation routine assuming existing installation not detected 
        
        }
The installation halts immediately as it should because a product named "VLC media player" is detected (albeit a false positive of sorts). The version number is what actually makes each subsequent product release unique from the previous release, the product name (in this case VLC Media Player) does not change.
[03-20-2015 00:45:29.618] [Pre-Installation] [Get-InstalledApplication] :: Get information for installed Application Name(s) [VLC media player]...
The log entries below suggest that the Get-InstalledApplication function is aware of exactly which version installed; clearly v2.1.5 was detected.
[03-20-2015 00:45:30.455] [Pre-Installation] [Get-InstalledApplication] :: Found installed application [VLC media player] version [2.1.5] matching application name [VLC media player]
The installation should have been allowed to continue (as I understand it) because the condition was not met in that version being targeted (in this case v2.2.0) does not exist. However I understand that "VLC media player" is not unique which is why (I presume) the condition was erroneously achieved (as seen in the remaining log entries).
[03-20-2015 00:45:30.906] [Pre-Installation] [Show-InstallationPrompt] :: Displaying custom installation prompt with the non-default parameters: [-NoWait:$true -ButtonRightText "OK" -Icon "Information" -Message "An installation of VLCMediaPlayer 2.2.0 exists."]...
03-20-2015 00:45:30.965] [Pre-Installation] :: ErrorCode Return value is: 1638, proceeding.
[03-20-2015 00:45:31.024] [Pre-Installation] [Exit-Script] :: VLCMediaPlayer_2.2.0_EN_01 Installation completed with exit code [1638].
Is there a way to specify the version in the condition to mitigate potential failures (false positives) due to non-unique name? For example, perhaps something like the following:
if (Get-InstalledApplication -Name "VLC media player") -and (Get-InstalledApplication -Version "2.2.0") {
           #Condition met, exit
}eles{
       #Condition not met proceed with installation
}
According to the log the "Get-InstalledApplication" function has the ability to determine the current version of the installed app, is there some way to capture it in the logic to solidify the detection method?


Oh wait there's more! I noticed something odd which would later underscore a much larger problem than I had originally assumed. Take note of the $appName variable
        ##*===============================================
    ##* VARIABLE DECLARATION
    ##*===============================================
    ## Variables: Application
    [string]$appVendor = ''
    [string]$appName = 'VLC Media Player'
    [string]$appVersion = '2.2.0'
    [string]$appArch = ''
    [string]$appLang = 'EN'
    [string]$appRevision = '01'
    [string]$appScriptVersion = '1.0.1503.16'
    [string]$appScriptDate = '2015.03.16'
    [string]$appScriptAuthor = 'alexayr'
[03-20-2015 00:45:30.906] [Pre-Installation] [Show-InstallationPrompt] :: Displaying custom installation prompt with the non-default parameters: [-NoWait:$true -ButtonRightText "OK" -Icon "Information" -Message "An installation of VLCMediaPlayer 2.2.0 exists."]...
The log returns "VLCMediaPlayer", something is stripping the white space from the DisplayName.

Initially I missed this and it wasn't until I employed a completely different detection method (that yielded similar results) that it became apparent that I would not achieve any measurable success until the white space stripping was remedied. For example,

Even with this
if ($is64Bit -eq $true) {
    $DisplayName = (Get-ItemProperty -Path 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player' -ErrorAction SilentlyContinue).DisplayName
    $DisplayVersion = (Get-ItemProperty -Path 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player' -ErrorAction SilentlyContinue).DisplayVersion
    } else { 
    $DisplayName = (Get-ItemProperty -Path 'HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player' -ErrorAction SilentlyContinue).DisplayName
    $DisplayVersion = (Get-ItemProperty -Path 'HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\VLC media player' -ErrorAction SilentlyContinue).DisplayVersion
    }
and this
$name = "VLC media player"
$Version = "2.2.0"
This condition would still would fail to detect "VLC media player" becuase the white space is being stripped.
if ((Get-InstalledApplication -Name $Name) -and ($DisplayVersion -eq $Version)) {
        Show-InstallationPrompt -Message "An installation of $appName $appVersion exists." -ButtonRightText 'OK' -Icon Information -NoWait; Write-Log "ErrorCode Return value is: 1638, proceeding."; Exit-Script -ExitCode 1638
    } else {
        Write-Log -Message "$appName $appVersion not installed, proceeding."
    }
so in essence "VLC media player" = "VLCmediaplayer" = False.

As always I appreciate the assistance.

Best Regards,

Yves
Developer
Mar 20, 2015 at 5:10 PM
Edited Mar 20, 2015 at 5:11 PM
I think you're using the the Get-InstalledApplication function incorrectly. When you make a call to this function, it returns the following properties for every application that it found: ProductCode, DisplayName, DisplayVersion, UninstallString, InstallSource, InstallLocation, InstallDate, Publisher, Is64BitApplication.

DisplayVersion is the property with the version number of the application that it found. So to use it properly in your code to do the detection, you would use it like thus:
$VLCInstall = Get-InstalledApplication -Name "VLC media player"
If ($VLCInstall -and (VLCInstall.DisplayVersion -eq '2.2.0')) {
    # Contdition met, exit
}
Else {
    # Condition not met, proceed with install
}
You can search in the toolkit to see where we are removing white spaces from the $appName variable. It is done to avoid various issues in the script when we use it to build other variables and file names. I would suggest not using this variable to control your logic or how you log if you don't want the white space to be stripped.
Marked as answer by yralexandre on 3/20/2015 at 10:11 AM
Mar 20, 2015 at 6:11 PM
Edited Mar 20, 2015 at 6:14 PM
Thank you for the expert advice!

Best Regards,

Yves