This project is read-only.

Can I use an IF statement Install only on systems not present

Topics: Archive - General
Jun 19, 2014 at 5:06 PM
I notice that if I use the tool it does a reinstall on systems that already have the application. I have some dependencies that are on some systems and not others. I do not want to have the dependencies reinstalling on systems that already have them. IE Microsoft Visual c++ 2005 redist 8 as an example.. can I use some sort of IF statement to only install if not on target system? in the preinstall step perhaps?
Jun 19, 2014 at 10:13 PM
Hi there,

Yes you can, try something like this:

If ((Get-InstalledApplication "Visual C++ 2008") -eq $null) {
Execute-MSI -Action Install "Microsoft_VisualC++_2008.msi"

Jun 20, 2014 at 2:26 PM
I don't often use the "Get-InstalledApplication" function, especially for version detection because I haven't always gotten it nailed down possibly due to my syntax. I'm constantly using your above method if I just want to ensure an application is installed, but to use your above method and include installation version, it may look something like this?

I'll stick with the Visual C for the example
$visualCapp = Get-InstalledApplication "Visual C++ 2008"
$visualCversion = ($visualCapp) | select "DisplayVersion" -expand "DisplayVersion"

If ($visualCapp -eq $null) {
    Execute-MSI -Action Install "Microsoft_VisualC++_2008.msi" }
ElseIf ($visualCversion -lt "9.0.30729.4148") {
    Execute-MSI -Action Install "Microsoft_VisualC++_2008.msi" }
I've been using direct registry path and key of the the application for my detection as apposed to using this built in function, but if I understand all of its capabilities and correct syntax I'd rather use whats already present.
Jun 20, 2014 at 2:41 PM
Yes, you could shorten what you have to this:

$visualCappVersion = Get-InstalledApplication "Visual C++ 2008" | select "DisplayVersion" -expand "DisplayVersion"

If ($visualCappVersion -lt "9.0.30729.4148") {
Execute-MSI -Action Install "Microsoft_VisualC++_2008.msi" }    

Jun 20, 2014 at 2:43 PM
I would cast both of those using [version] - otherwise -lt won't work properly.
Jun 20, 2014 at 3:06 PM
I'm not sure I follow what you mean by using [version], and its probably why it has failed for me before. I also used the longer code to include the "-eq $null" but I'm guessing by using the simplified it includes $null as basically "-lt" the listed version?
Jun 20, 2014 at 3:15 PM
Sorry, I was on my phone at the time. Here's an example:
[version]$visualCappVersion = Get-InstalledApplication "Visual C++ 2008" | select "DisplayVersion" -expand "DisplayVersion" 

If ($visualCappVersion -lt [version]"9.0.30729.4148" -or $visualCAppVersion -eq $null) {
     Execute-MSI -Action Install "Microsoft_VisualC++_2008.msi" }    
So you're casting to a datatype of [version] instead of [string] (default). [version] can handle multiple decimal points in a comparison operator like Less Than, whereas [string] cant. You can prove this with the following code:
[string]$test1 = ""
[string]$test2 = ""
if ($test1 -lt $test2) { Write-Host "Correct" }
You won't get any output. But if you do this:
[version]$test1 = ""
[version]$test2 = ""
if ($test1 -lt $test2) { Write-Host "Correct" }
You'll see "Correct" displayed.
Jun 20, 2014 at 3:44 PM
Thank you, that cleared up quite a bit for me and it is probably the reason some of my detection have been acting a little flakey.
Jun 27, 2014 at 4:29 AM
Edited Jun 27, 2014 at 5:55 AM
Thanks sintaxasn, that has helped me immensely!

Edit: Actually, it throws an error "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Version"

Can this code be placed in the Pre-Installation phase so that it does the detection before Show-InstallationWelcome?

Edit 2: I just tried the code again but detecting a different app title and it worked??
And I will answer my own question, yes, it can be placed in the Pre-Installation phase:
    #Application Detection
    [version]$yourAppVersion = Get-InstalledApplication "Symantec Desktop Encryption" | select "DisplayVersion" -expand "DisplayVersion"

    If ($yourAppVersion -lt [version]"" -or $yourAppVersion -eq $null)
    # Show Welcome Message, close Internet Explorer if required, allow up to 3 deferrals, verify there is enough disk space to complete the install and persist the prompt
    Show-InstallationWelcome -CloseApps "iexplore" -AllowDefer -DeferTimes 10 -CheckDiskSpace -PersistPrompt

    # Show Progress Message (with the default message)
        Exit 0