5 Replies Latest reply on May 17, 2016 7:34 AM by Jaakko Lehtinen

    NSH adds unwanted apostrophes to variable in a script

    Jaakko Lehtinen

      Hi,

       

      We're running BSA 8.5 with latest hotfixes and  I have come across with odd situation with one of our automation scripts. Scripts is used to retrieve correct Active Directory Organization Unit path from csv -file and join Windows server to domain. What happens is that script does retrieves value from csv correctly and stores it into variable. But when script executes netdom join -command on target server, it does add apostrophes around spaces in OU path string? My question is how can I change NSH behavior so that it doesn't add these apostrophes around space, because those marks make netdom join -command fail? Scripts and output from NSH job log below:

       

      First we retrieve OU value from csv using subscription code:

      LOOKUP_AD_OU_LOCATION()

      {

        echo "\n### START: LOOKUP_AD_OU_LOCATION ###"

        echo "\nObtaining OU from AD OU code : ${AD_OU_LOCATION}"

       

        if [ "${AD_OU_LOCATION}" != "none" ]; then

        OU=`nexec ${FILE_SERVER} ${SCRIPT_PATH}/ou-placement/ou-placement.sh ${SCRIPT_PATH}/ou-placement/${DOMAIN}.csv    ${AD_OU_LOCATION}`

        echo "OU: ${OU}"

        fi

       

        if [ "${OU}" = "" ]; then

        print -u2 "ERROR: Unable to obtain OU from AD OU Location: ${AD_OU_LOCATION}"

        exit 1

        fi

       

        echo "\n### END: LOOKUP_AD_OU_LOCATION ###"

      }

      ou-placement.sh script is below, I have commented out version, where we do remove empty spaces from OU path (didn't work at all).

      FILE=$1

      CODE=$2

      LINE=`cat $1 | grep $2`

      # echo "LINE: $LINE"

      BUDGET=`echo "$LINE" | awk -F ',' '{print $1}'`

      # echo "Budget: $BUDGET"

      OU=`echo "$LINE" | awk -F '\"*\"' '{print $2}'`

      #OU=`echo "$LINE" | awk -F '\"*\"' '{print $2}'|tr -d ' '`

      echo "$OU"

       

      Output of these in log is:

      +LOOKUP_AD_OU_LOCATION:16> echo '\n### END: LOOKUP_AD_OU_LOCATION ###'

      +LOOKUP_AD_OU_LOCATION:11> [ 'OU=Servers,OU=Datacenter servers,DC=contoso,DC=com' '=' '' ']'

      +LOOKUP_AD_OU_LOCATION:8> echo 'OU: OU=Servers,OU=Datacenter servers,DC=contoso,DC=com'

      ### END: LOOKUP_AD_OU_LOCATION ###

      +LOOKUP_AD_OU_LOCATION:7> OU='OU=Servers,OU=Datacenter servers,DC=contoso,DC=com'

      OU: OU=Servers,OU=Datacenter servers,DC=contoso,DC=com

      +LOOKUP_AD_OU_LOCATION:7> OU=+LOOKUP_AD_OU_LOCATION:1> nexec -D //HOST1/c/ APPSERVER02 /data/bmc/bladelogic/storage/scripts-custom/winscripts/ou-placement/ou-placement.sh /data/bmc/bladelogic/storage/scripts-custom/winscripts/ou-placement/contoso.com.csv abcd001

      +LOOKUP_AD_OU_LOCATION:7> OU=+LOOKUP_AD_OU_LOCATION:1> nexec -D //HOST1/c/ APPSERVER02 /data/bmc/bladelogic/storage/scripts-custom/winscripts/ou-placement/ou-placement.sh /data/bmc/bladelogic/storage/scripts-custom/winscripts/ou-placement/contoso.com.csv abcd001

      +LOOKUP_AD_OU_LOCATION:6> [ abcd001 '!=' none ']'

      +LOOKUP_AD_OU_LOCATION:4> echo '\nObtaining OU from AD OU code : abcd001'

      +LOOKUP_AD_OU_LOCATION:3> echo '\n### START: LOOKUP_AD_OU_LOCATION ###'

      +//@/opt/bmc/bladelogic/NSH/tmp/config_deployment_APPSERVER01/scripts/job__25840e38-9638-488a-81f0-ba56cbad0a1c/script_DBKey-SJobKeyImpl-2052087-4__926ebddc-3e81-409b-b5a4-a4e1455d3a26.2056521.1_win_post_prov.nsh:693> LOOKUP_AD_OU_LOCATION

      Obtaining OU from AD OU code : abcd001

      ### START: LOOKUP_AD_OU_LOCATION ###

      After this execution of script moves to function, where we do join server to AD running netdom join -command. I have two version of netdom join -command lines, one with double apostrophes added around $OU - variable and one that doesn't add (commented out because doesn't work).

      ADD_TO_DOMAIN()

      {

        echo "\n### START: ADD_TO_DOMAIN ###"

        # Join Domain

       

        #nexec -e netdom join ${SERVER} /domain:${FQDN} /ou:${OU} /userd:${JOINUSER} /passwordd:${JOINPASSUNENC}

        nexec -e netdom join ${SERVER} /domain:${FQDN} /ou:\"${OU}\" /userd:${JOINUSER} /passwordd:${JOINPASSUNENC}

       

        if [ $? = "0" ]; then

        echo "Server successfully joined domain ${FQDN}"

        else

        print -u2 "ERROR: Error occured joining domain ${FQDN}"

          exit 1

        fi

       

        echo "\n### END: ADD_TO_DOMAIN ###"

      }

      When this function is executed, output as follows. Now as we can see from output, apostrophes (tried to highlight those with red) have been added to the OU path around space in OU=Datacenter' 'servers and to couple other places. Those weren't in place when ou-placement.sh echoed content of $OU -variable. Due to those apostrophes netdom join -command does fail (if I run same command manually on target server without these, command completes succesfully).

       

      Exit Code 1

      +ADD_TO_DOMAIN:11> exit 1

      ERROR: Error occured joining domain contoso.com

      +ADD_TO_DOMAIN:10> print -u2 'ERROR: Error occured joining domain contoso.com'

      +ADD_TO_DOMAIN:7> [ 87 '=' 0 ']'

      Try "NETDOM HELP" for more information.

      MOVENT4BDC | RENAMECOMPUTER | RESET | TRUST | VERIFY | RESETPWD ]

      NETDOM [ ADD | COMPUTERNAME | HELP | JOIN | MOVE | QUERY | REMOVE |

      The syntax of this command is:

      +ADD_TO_DOMAIN:6> nexec -D //HOST1/c/ -e netdom join HOST1 /domain:contoso.com

      '/ou:"OU=Servers,OU=Datacenter' 'servers,DC=contoso,DC=com"' '/userd:contoso.com\USERNAME' /passwordd:PASSWORD

      +ADD_TO_DOMAIN:2> echo '\n### START: ADD_TO_DOMAIN ###'

      ### START: ADD_TO_DOMAIN ###

      Could someone help me how to get rid of those apostrophes, they're breaking script I really don't understand where and why those appear.

       

      Thanks in advance,

      Jaakko