5 Replies Latest reply on Dec 15, 2016 2:20 PM by Yanick Girouard

    Exit Code 13 ... again...

    Yanick Girouard

      I've got a type 1 NSH Script job that I use generically to call powershell scripts either on the app server that runs the job, or on a specific console server:

       

      #! nsh

      # Executes powershell script on given target

       

       

      START_TIME=$SECONDS

      source //bsafs/D/Bladelogic/Scripts/inc/base.inc.nsh

       

       

      TARGET_NAME=${1:l}

      CONSOLE_SERVER=${2:l}

      SCRIPT_DIR=$3

      SCRIPT_NAME=$4

      PARAM[1]=${5:s/\\000//}

      PARAM[2]=${6:s/\\000//}

      PARAM[3]=${7:s/\\000//}

      PARAM[4]=${8:s/\\000//}

      PARAM[5]=${9:s/\\000//}

      PARAM[6]=${10:s/\\000//}

      PARAM[7]=${11:s/\\000//}

      PARAM[8]=${12:s/\\000//}

      PARAM[9]=${13:s/\\000//}

      PARAM[10]=${14:s/\\000//}

      UNAME=$(uname)

       

       

      if [ "${CONSOLE_SERVER}" = "_appserver_" ]; then

          CONSOLE_SERVER="${HOST:l}" 

      fi

       

       

      function check_script_dir() {

          egrep "^/[a-z]/.*" <<< "${SCRIPT_DIR}" &> /dev/null

          if [ $? -ne 0 ]; then

              base_echo_log "SCRIPT_DIR parameter must start with a drive letter (i.e. /d/ or /c/)" 1

              exit 1

          fi

          REMOTE_SCRIPT_DIR=$(sed -e 's;^/\([a-z]\)/;\1:\\;' -e 's;/;\\;g' <<< "${SCRIPT_DIR}")

      }

       

       

      function check_script() {

          if [ ! -d "//$CONSOLE_SERVER${SCRIPT_DIR}" ]; then

              base_echo_log "Specified script directory does not exist: '${SCRIPT_DIR}'" 1

              exit 1

          fi

         

          if [ ! -f "//$CONSOLE_SERVER${SCRIPT_DIR}/${SCRIPT_NAME}" ]; then

              base_echo_log "Specified script file does not exist: '${SCRIPT_NAME}'" 1

              exit 1

          fi

      }

       

       

      function run_ps_script() {

          base_echo_log "Console Server: ${CONSOLE_SERVER}"

          base_echo_log "Script Directory: ${SCRIPT_DIR}"

          base_echo_log "Script Name: ${SCRIPT_NAME}"

          PARAM_STRING=""

          for i in {1..10}; do

              if [ ! -z "${PARAM[$i]}" ]; then

                  cat <<< "[INFO] PARAM[$i]: ${PARAM[$i]}"

                  PARAM_STRING="${PARAM_STRING} ${PARAM[$i]}"

              fi

          done

          base_echo_log "Calling script..."

          cat <<< "Command-line: nexec -ncq ${CONSOLE_SERVER} cmd /c powershell -noninteractive -noprofile -file ${REMOTE_SCRIPT_DIR}\\${SCRIPT_NAME}${PARAM_STRING}"

          base_echo_separator

          sleep 1 # Sleep 1 second to help with log item sorting in job run logs

          nexec -ncq ${CONSOLE_SERVER} cmd /c powershell -noninteractive -noprofile -file ${REMOTE_SCRIPT_DIR}\\${SCRIPT_NAME}${PARAM_STRING}

          PS_EXIT_CODE=$?

          sleep 1 # Sleep 1 second to help with log item sorting in job run logs

          base_echo_separator

          base_echo_log "Script completed with exit code ${PS_EXIT_CODE}"

      }

       

      function main() {

          check_script_dir

          check_script

          run_ps_script

       

       

          cd //@

          disconnect $TARGET_NAME

          disconnect $CONSOLE_SERVER

       

       

          DURATION=$(($SECONDS - $START_TIME))

          if [ $DURATION -eq 0 ]; then

          base_echo_log "Duration = <1s" 3

          else

          base_echo_log "Duration = ${DURATION}s" 3

        fi

         

          if [ "${PS_EXIT_CODE}" -ne 0 ]; then

              exit 1 

          else

              exit 0

          fi

      }

       

       

      main

       

      Not sure why, but for a given script that connects to VMware VCenter and runs some actions against a VM, the powershell completes with success, but the NSH Script job ends in error with the Exit Code 13 error. I've tried adding the usual cd //@; disconnect blah blah blah stuff, but that didn't fix it.

       

      Any idea what this could be caused by?

        • 2. Re: Exit Code 13 ... again...
          Bill Robinson

          does it happen at the end or during the script ?

           

          how long does the script run for?

           

          try the 'disconnect SERVER' after each nexec ?

          does the <<< spawn a child process?  if so using nexec in there could be an issue because that connection could be the one throwing the error.

          • 3. Re: Exit Code 13 ... again...
            Yanick Girouard

            The scripts last a mere 5 minutes or less, and the exit code 13 happens after it's done executing (the powershell is done).

             

            I could try the disconnect after the nexec... I'll let you know if that works.

             

            The <<< is basically a stdin redirection. It prints a line of text directly to the stdin of the command. So instead of doing like:

             

            echo tata | grep something

             

            ...which spawns the echo process to pipe it in the grep process, you only spawn the grep process. It's a nice one-liner shortcut I use often.

            • 4. Re: Exit Code 13 ... again...
              Yanick Girouard

              ...I think I found it. testing, but the script is changing the number of cpu and core on the target VM, but even though the script is running against a different server, the initial NSH session is connected to target. I will try to detatch (cd //@; disconnect $TARGET_NAME) before I start the nexec to see if that changes things.

              • 5. Re: Exit Code 13 ... again...
                Yanick Girouard

                That was it, silly me... Since the script affected the OS resources of the target, which seemed to disrupt agent connectivity, I had to detatch from it before calling nexec.

                 

                This is the fixed script:

                 

                #! nsh

                # Executes powershell script on given target

                 

                START_TIME=$SECONDS

                source //bsafs/D/Bladelogic/Scripts/inc/base.inc.nsh

                 

                TARGET_NAME=${1:l}

                CONSOLE_SERVER=${2:l}

                SCRIPT_DIR=$3

                SCRIPT_NAME=$4

                PARAM[1]=${5:s/\\000//}

                PARAM[2]=${6:s/\\000//}

                PARAM[3]=${7:s/\\000//}

                PARAM[4]=${8:s/\\000//}

                PARAM[5]=${9:s/\\000//}

                PARAM[6]=${10:s/\\000//}

                PARAM[7]=${11:s/\\000//}

                PARAM[8]=${12:s/\\000//}

                PARAM[9]=${13:s/\\000//}

                PARAM[10]=${14:s/\\000//}

                UNAME=$(uname)

                 

                 

                if [ "${CONSOLE_SERVER}" = "_appserver_" ]; then

                    CONSOLE_SERVER="${HOST:l}"

                fi

                 

                 

                function check_script_dir() {

                    egrep "^/[a-z]/.*" <<< "${SCRIPT_DIR}" &> /dev/null

                    if [ $? -ne 0 ]; then

                        base_echo_log "SCRIPT_DIR parameter must start with a drive letter (i.e. /d/ or /c/)" 1

                        exit 1

                    fi

                    REMOTE_SCRIPT_DIR=$(sed -e 's;^/\([a-z]\)/;\1:\\;' -e 's;/;\\;g' <<< "${SCRIPT_DIR}")

                }

                 

                 

                function check_script() {

                    if [ ! -d "//$CONSOLE_SERVER${SCRIPT_DIR}" ]; then

                        base_echo_log "Specified script directory does not exist: '${SCRIPT_DIR}'" 1

                        exit 1

                    fi

                  

                    if [ ! -f "//$CONSOLE_SERVER${SCRIPT_DIR}/${SCRIPT_NAME}" ]; then

                        base_echo_log "Specified script file does not exist: '${SCRIPT_NAME}'" 1

                        exit 1

                    fi

                }

                 

                 

                function run_ps_script() {

                 

                  # Detatch from target before executing

                  cd //@; disconnect $TARGET_NAME

                 

                    base_echo_log "Console Server: ${CONSOLE_SERVER}"

                    base_echo_log "Script Directory: ${SCRIPT_DIR}"

                    base_echo_log "Script Name: ${SCRIPT_NAME}"

                    PARAM_STRING=""

                    for i in {1..10}; do

                        if [ ! -z "${PARAM[$i]}" ]; then

                            cat <<< "[INFO] PARAM[$i]: ${PARAM[$i]}"

                            PARAM_STRING="${PARAM_STRING} ${PARAM[$i]}"

                        fi

                    done

                    base_echo_log "Calling script..."

                    cat <<< "Command-line: nexec -ncq ${CONSOLE_SERVER} cmd /c powershell -noninteractive -noprofile -file ${REMOTE_SCRIPT_DIR}\\${SCRIPT_NAME}${PARAM_STRING}"

                    base_echo_separator

                    sleep 1 # Sleep 1 second to help with log item sorting in job run logs

                    nexec -ncq ${CONSOLE_SERVER} cmd /c powershell -noninteractive -noprofile -file ${REMOTE_SCRIPT_DIR}\\${SCRIPT_NAME}${PARAM_STRING}

                    PS_EXIT_CODE=$?

                    sleep 1 # Sleep 1 second to help with log item sorting in job run logs

                    base_echo_separator

                    base_echo_log "Script completed with exit code ${PS_EXIT_CODE}"

                }

                 

                function main() {

                    check_script_dir

                    check_script

                    run_ps_script

                 

                    DURATION=$(($SECONDS - $START_TIME))

                    if [ $DURATION -eq 0 ]; then

                    base_echo_log "Duration = <1s" 3

                    else

                    base_echo_log "Duration = ${DURATION}s" 3

                  fi

                  

                    if [ "${PS_EXIT_CODE}" -ne 0 ]; then

                        exit 1

                    else

                        exit 0

                    fi

                }

                main

                1 of 1 people found this helpful