0

Team , I wrote on program that install some software and after that it display exit code by below .

$SoftwareInstall.ExitCode

It returns 0 and other value depend upon how installation goes. I have some other expression after that to execute only if the exite code is success that is 0 . How to setup a check for this using if condition . Can you please suggest if below is the right way to do

if ( $SoftwareInstall.ExitCode -eq 0){
    "Software Installed successfully "  
     #Then some other code I'll put here

     }
else{
    "Software did not installed"
}

Please suggest .

2
  • 1
    Yes, that would be the right way to go Commented Jul 11, 2016 at 9:34
  • Thanks ! will do it. Commented Jul 11, 2016 at 9:39

1 Answer 1

1

Your method is one way to do it but I suggest that you use a Switch Statement for handling return values. If you have installed using an MSI file then there are several return codes you can easily handle in this way -

#Check MSIEXEC return value            
    switch ($SoftwareInstall.ExitCode ){
        #If the uninstallation succeeded
        0    { }
        #If the Installation succeeded but requires a reboot
        3010 { }
        #If the uninstallation failed
        default { }
    }

Handling return codes in this way can make code much easier to read and modify in the future. It also allows you to handle several different error codes without too many nested ifs or if-else blocks.

Sign up to request clarification or add additional context in comments.

2 Comments

code only do installation of latest msi and if it is success it will start the services. That's why I did not thought about switch as anyhow i need to start my services after re installation .Every time I checked and got 0 as return code for success Please suggest ,
If you are sure that you will only ever need two blocks of code then your if ( $SoftwareInstall.ExitCode -eq 0) is definitely sufficient. However if you ever need more then I would definitely recommend a switch statement. For example, I have Powershell code which automates the installation and uninstallation of around 10 packages. If any operations succeed but require a reboot then the switch statement can automate that before moving on to the next package. This might be overkill for your needs, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.