2 Replies Latest reply on Feb 24, 2011 3:46 AM by Jeff Turner

    Problem with creation of String Enumeration Property in a NSH script

      I am trying to create a StringEnumeration Property in the Server class and then populate that property across multiple servers. The script is shown below, followed by the (NSH-traced) error output.

       

      Can anyone tell me where I'm going wrong with this script?

       

      Firstly... Here is the script (called addenumprop.nsh):

       

      #!/bin/nsh

      ###
      #
      # This script is used to create an Enumerated String Property in the Server class
      # and generate a list of valid entries
      # The script then applies an appropriate value against a server based on the OS of that server.
      #
      #
      #  Note: All of the values have been created to NOT include spaces so as to avoid excessive quoting!
      #        This is a deliberate choice while the script is being tested in its early evelopment stages.
      #
      # Created by Jeff Turner, BladeLogic Instructor, BMC Software
      # For use as a demonstration of scripting in the Foundations Part 2 classroom lab environment only.
      # Version 1.0 / 22-February-2011
      ###


      ###
      # Create aliases and variables
      ###

      alias blcli='blcli -v defaultProfile'

      CLASSNAME="//Server"
      PROPNAME="MY_PROP"
      DESCRIPTION="A_property_to_set_appropriate_administration_accounts"
      HOST_LIST=$( blcli Server listAllServers )

      PROPERTY_LIST=$( blcli PropertyClass listAllPropertyNames Class://Server )

      ###
      # Check existence of the Property
      ###

      echo "Checking for the existence of the Property < ${PROPNAME} >.  Please wait..."

      for CHECK in ${PROPERTYLIST}
      do
      if [ "${CHECK}" = "${PROPNAME}" ]
      then
        echo "The Property < ${PROPNAME} > already exists in the Server Class"
        echo "Exiting from the script now."
        exit 1
      fi
      done

      echo "Property < ${PROPNAME} > Does not yet exist. Making it now..."

      ###
      # Now create the Property in the Server Class (only if does not yet exist)
      ###

      blcli PropertyClass addStringEnumProperty Class:${CLASSNAME} ${PROPNAME} ${DESCRIPTION} \
      true false true \
      Administrator,Administrator \
      Renamed_Admin,BuiltInAdmin  \
      UNIX_Linux_Admin,root  \
      "Administrator" || exit 2

       

      ###
      # Now, set the values appropriate to the server types
      ###
      echo "Property values now being set for the servers... Please wait..."
      sleep 2


      ##############################
      # MAIN - Body of script
      #

      for HOST in ${HOST_LIST}
      do

          typeset -L20 HOSTX="${HOST}"
          case ${HOST} in

      red[123]) echo "Linux - Working ( ${HOSTX} )\c"
        HOST_ID=$(blcli Server getServerIdByName ${HOST})
        HOST_KEY=$(blcli Server getServerDBKeyByName ${HOST})
        CURRENT_VAL=$(blcli Server printPropertyValue ${HOST} ${PROPNAME})
        NEW_PROP_VAL="UNIX_Linux_Admin"

        if [ "${CURRENT_VAL}" != "${NEW_PROP_VAL}" ]
        then
        RESULT=`blcli Server setPropertyValue $HOST_KEY $PROP_KEY "${NEW_PROP_VAL}"`  && echo " ... set to < ${NEW_PROP_VAL} >"
        else
        echo "Kept existing value < ${CURRENT_VAL} >"
        fi
        ;;

      win[123]) echo "Windows - Working ( ${HOSTX} ) \c"
        HOST_ID=$(blcli Server getServerIdByName ${HOST})
        HOST_KEY=$(blcli Server getServerDBKeyByName ${HOST})
        CURRENT_VAL=$(blcli Server printPropertyValue ${HOST} ${PROPNAME})
        NEW_PROP_VAL="Renamed_Admin"

        if [ "${CURRENT_VAL}" != "${NEW_PROP_VAL}" ]
        then
        RESULT=`blcli Server setPropertyValue $HOST_KEY $PROP_KEY "${NEW_PROP_VAL}"`  && echo " ... set to < ${NEW_PROP_VAL} >"
        else
        echo "Kept existing value < ${CURRENT_VAL} >"
        fi
        ;;

      [bB][lL][aA][pP][pP]) echo "AppSrvr ( ${HOSTX} ) ... No action taken"
        ;;

      *) echo "Server  ( ${HOSTX} ) ... no action taken"
        ;;

          esac

      done

       

       

       

       

      and here is the NSH-traced output:

      [Note: the script was executed as   "nsh -xc addenumprop.nsh" ]

       


      +nsh:23> alias 'blcli=blcli -v defaultProfile'
      +nsh:25> CLASSNAME=//Server
      +nsh:26> PROPNAME=MY_PROP
      +nsh:27> DESCRIPTION=A_property_to_set_appropriate_administration_accounts
      +nsh:28> HOST_LIST=+nsh:1> blcli -v defaultProfile Server listAllServers
      +nsh:28> HOST_LIST='BLAPP
      red1
      red2
      red3
      win1
      win2
      win3'
      +nsh:30> PROPERTY_LIST=+nsh:1> blcli -v defaultProfile PropertyClass listAllPropertyNames Class://Server
      +nsh:30> PROPERTY_LIST='Command execution failed.  fully qualified path did not start at SystemObjectServer'
      +nsh:36> echo 'Checking for the existence of the Property < MY_PROP >.  Please wait...'
      Checking for the existence of the Property < MY_PROP >.  Please wait...
      +nsh:48> echo 'Property < MY_PROP > Does not yet exist. Making it now...'
      Property < MY_PROP > Does not yet exist. Making it now...
      +nsh:54> blcli -v defaultProfile PropertyClass addStringEnumProperty Class://Server MY_PROP A_property_to_set_appropriate_administration_accounts true false true Administrator,Administrator Renamed_Admin,BuiltInAdmin UNIX_Linux_Admin,root Administrator
      Unknown command : name space = 'PropertyClass', command = 'addStringEnumProperty'.com.bladelogic.cli.factory.CommandNotFoundException: Could not find any instance of command "PropertyClass,addStringEnumProperty" with expected number of arguments: 10
      +nsh:59> exit 2

        • 1. Problem with creation of String Enumeration Property in a NSH script
          Bill Robinson

          you are passing 10 arguments to this command:

           

          blcli PropertyClass addStringEnumProperty Class:${CLASSNAME} ${PROPNAME} ${DESCRIPTION} \
          true false true \
          Administrator,Administrator \
          Renamed_Admin,BuiltInAdmin  \
          UNIX_Linux_Admin,root  \
          "Administrator" || exit 2

           

          which is not the number it expects.

           

          i'd also think about using the performance commands and not the normal blcli.

          1 of 1 people found this helpful
          • 2. Problem with creation of String Enumeration Property in a NSH script

            Hi Bill.

             

            I took some of your feedback and then looked at my script, again, and finally worked out what was happening.

             

            The BLCLI Help is not too helpful as the syntax requirements and value assignments are not, explicitly detailed in the help text. It took several attempts to get it to work but I finally got there.

             

            Many thanks for the pointers.

             

             

             

            Here is the working script:

             

            #!/bin/nsh

             

            ###

            #

            # This script is used to create an Enumerated String Property in the Server class

            # and generate a list of valid entries

            # The script then applies an appropriate value against a server based on the OS of that server.

            #

            #

            # Note: All of the values have been created to NOT include spaces so as to avoid excessive quoting!

            #       This is a deliberate choice while the script is being tested in its early evelopment stages.

            #

            # Created by Jeff Turner, BladeLogic Instructor, BMC Software

            # For use as a demonstration of scripting in the Foundations Part 2 classroom lab environment only.

            # Version 1.9 / 23-February-2011

            ###

             

            blcli_setoption serviceProfileName defaultProfile

            blcli_setoption roleName BLAdmins

             

            ###

            # Create aliases and variables

            ###

             

            alias blcli='blcli -v defaultProfile'

             

            CLASSNAME="//SystemObject/Server"

            PROPNAME="ENUM_PROP"

            DESCRIPTION="An_enumerated_property"

            HOST_LIST=$( blcli Server listAllServers )

             

            PROPERTY_LIST=$( blcli PropertyClass listAllPropertyNames Class:${CLASSNAME} )

             

            ###

            # Check existence of the Property

            ###

             

            echo "Checking for the existence of the Property < ${PROPNAME} >. Please wait..."

             

            for CHECK in ${PROPERTYLIST}

            do

                            if [ "${CHECK}" = "${PROPNAME}" ]

                            then

                                            echo "The Property < ${PROPNAME} > already exists in the Server Class"

                                            echo "Exiting from the script now."

                                            exit 1

                            fi

            done

             

            echo "Property < ${PROPNAME} > Does not yet exist. Making it now..."

             

            ###

            # Now create the Property in the Server Class (only if does not yet exist)

            ###

             

            STATE=0

             

            blcli PropertyClass addStringEnumProperty Class:${CLASSNAME} \

            ${PROPNAME} ${DESCRIPTION} true false true \

            "aaa,bbb" "ccc,ddd" "ccc" || STATE=$?

             

             

            if [ "${STATE}" -ne 0 ]

            then

                            exit 2     # Exit if the status value is not 0

            fi

             

            ###

            # Now, add more name/value pairs to that Enumerated Property

            ###

             

            blcli PropertyClass addNewValuesToStringEnumProperty Class:${CLASSNAME} ${PROPNAME} "Administrator,Renamed_Admin" "Administrator,BuiltInAdmin"

            blcli PropertyClass addNewValuesToStringEnumProperty Class:${CLASSNAME} ${PROPNAME} "Linux_UNIX_Admin,NotAdmin" "root,nobody"

             

            ###

            # Now, set the values appropriate to the server types

            ###

            echo "Property values now being set for the servers... Please wait..."

            sleep 2

             

             

            ##############################

            # MAIN - Body of script

            #

             

            for HOST in ${HOST_LIST}

            do

             

               typeset -L20 HOSTX="${HOST}"

               case ${HOST} in

             

                            red[123])             echo "Linux - Working ( ${HOSTX} )\c"

                                            CURRENT_NAME=$(blcli Server printPropertyValue ${HOST} ${PROPNAME})

                                            NEW_PROP_NAME="Linux_UNIX_Admin"

                                            NEW_PROP_VAL="root"

             

                                            if [ "${CURRENT_NAME}" != "${NEW_PROP_NAME}" ]

                                            then

                                            RESULT=`blcli Server setPropertyValueByName $HOST $PROPNAME "${NEW_PROP_VAL}"` && echo " ... set to < ${NEW_PROP_NAME} >"

                                            else

                                            echo "Kept existing value < ${CURRENT_NAME} >"

                                            fi

                                            ;;

             

                            win[123])             echo "Windows - Working ( ${HOSTX} ) \c"

                                            CURRENT_NAME=$(blcli Server printPropertyValue ${HOST} ${PROPNAME})

                                            NEW_PROP_NAME="Renamed_Admin"

                                            NEW_PROP_VAL="BuiltInAdmin"

             

                                            if [ "${CURRENT_NAME}" != "${NEW_PROP_NAME}" ]

                                            then

                                            RESULT=`blcli Server setPropertyValueByName $HOST $PROPNAME "${NEW_PROP_VAL}"` && echo " ... set to < ${NEW_PROP_NAME} >"

                                            else

                                            echo "Kept existing value < ${CURRENT_NAME} >"

                                            fi

                                            ;;

             

                            [bB][lL][aA][pP][pP])     echo "AppSrvr ( ${HOSTX} ) ... No action taken"

                                            ;;

             

                            *)            echo "Server ( ${HOSTX} ) ... no action taken"

                                            ;;

             

               esac

             

            done