2 Replies Latest reply on Feb 7, 2012 4:15 AM by peter bulman

    powershell return values

      Hello,

      I am calling a powershell script via an nsh job I have created in the BL automation console, the script is a simple ps cmdlet to stop a windows service.  Is there a way I can handle a return value from the script?  At present the nsh script will call the ps script and start the service and show success in the job results.  One problem I have is if the windows service is disabled the script is still executed and returns a false success.  I want to be able to handle such error cases.

       

      nsh script:

      nexec -i -e cmd /c "echo . | powershell F:/temp/ems_stop.ps1"

       

      ps script:

      Stop-Service tibemsd

       

      Is this the correct approach to be taking?  I am new to bladelogic - any help appreciated!

       

      Thanks

        • 1. powershell return values
          Bill Robinson

          i'm not sure how to do this in powershell, but in nsh you can do like:

           

          nexec -i -e cmd /c "echo . | powershell F:/temp/ems_stop.ps1"

          RETVAL=$?

           

          if [ ${RETVAL}  -eq <some value> ]

          then

               echo "error"

               exit 1

          elfi [ ${RETVAL}  -eq <some other value> ]

               then

               echo "ok"

               exit 0

          else

               echo "maybe ok"

               exit 0

          fi

              

          • 2. powershell return values

            Thanks Bill!

            I managed to get what I wanted, changing the powershell script to:

             

            Stop-Service tibemsd -ErrorVariable errs -ea SilentlyContinue

            if ($errs.Count -eq 0)

            {

                write-host "ran fine"

            exit 0

            }

            else

            {

                write-host "failed to stop the tibemsd service"

            exit 1

            }

             

            and modified the nsh script to:

             

            nexec -i -e cmd /c "echo . | powershell F:/temp/scripts/ems_stop.ps1"

            RETVAL=$?

            ECHO "RETVAL: ${RETVAL}"

            if [ ${RETVAL}  -eq 1 ]

            then

                echo "nsh: error"

            exit 1

            else

            echo "nsh: ok"

            exit 0

            fi