/build/static/layout/Breadcrumb_cap_w.png

Powershell IF's

Morning,

I am working on a script to install an in house application. One Pre Req is .Net. I need my script to check if the version exists. I have set this up but how do I say if the value is false run setup.exe

        $val = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" -Name "Version"
        if ($val.Version -ne 4.6.000.81)
        {
        Start-Process "\\Share\rollout\Installations\Scripts\Files\Confirm\NDP46-KB3045557-x86-x64-AllOS-ENU.exe" /s
        }

On this part -   if ($val.Version -ne 4.6.000.81) I would like a if this is false run the below kind of thing, Any help is appreciated. 

0 Comments   [ + ] Show comments

Answers (2)

Posted by: djordan 6 years ago
Senior White Belt
0

#Checks and Installs .Net 4.6.1
        $val = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" -Name "Version"
        if ($val.Version -ne 4.6.000.81 -eq $True)
        {
        Start-Process "\\Share\rollout\Installations\Scripts\Files\Confirm\NDP46-KB3045557-x86-x64-AllOS-ENU.exe" /s
        }


Worked it out.
Posted by: rad33k 6 years ago
Fourth Degree Brown Belt
0
Does it really works now?
I checked above code with few values and it always returned TRUE for me.

I would suggest to use a quotes for version value that you are comparing to and also consider [version] type, as then you can use other comparison operators like -lt -gt etc.
I mean something like this:
    #Checks and Installs .Net 4.6.1
$val = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" -Name "Version"
$val2 = "4.6.000.81"

if ([version]$val.Version -lt [version]$val2)
{
    Start-Process "\\Share\rollout\Installations\Scripts\Files\Confirm\NDP46-KB3045557-x86-x64-AllOS-ENU.exe" /s
}

Comments:
  • Hi, Thanks for the suggestion. Yes it was working when I tested but yours appears more bullet proof, Quite new to using powershell ha. Thanks! - djordan 6 years ago
  • You might possibly be able to answer my other question.

    http://www.itninja.com/question/vcreds-powershell - djordan 6 years ago

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ