11 Replies Latest reply on Aug 31, 2017 2:33 PM by Stephane Tardif

    Using the %f macro in an NSH Script Job

    S Crawford

      Where is the file with host names supposed to live when you use the %f in the parameter value?  Is a path supposed to be referenced in the NSH script?  If so, then what's the point of using the %f macro?  Why wouldn't I just list the filename (with path) directly in the parameter value?

        • 1. Re: Using the %f macro in an NSH Script Job
          Bill Robinson

          I think it drops in into /tmp or /tmp/stage (??STAGING_DIR??)  the %f reads the targets you assign in the job and dumps it into the file so it's dynamic, if your targets change.

          • 2. Re: Using the %f macro in an NSH Script Job
            S Crawford

            So you're saying the %f is only used to create an output with the lists of target server names?

             

            The BladeLogic user guide states:

            If you are running a script once against multiple hosts (as defined using the

             

            panel), you must pass the %h or %f macro as a parameter when you execute the script. The %h macro is replaced by a space-delimited list of host names. The %f macro is replaced by a file containing a list of host names.

             

            I thought the %f was used when you wanted to pass in a file that had a list of server names.

            • 3. Re: Using the %f macro in an NSH Script Job
              Bill Robinson

              Yes - I created a script and passed %f as the only argument. I targeted a server smart group w/ all my servers.  My script did:

              Echo "value: $1"

              Cat "$1"

               

              This was the output:

               

              Info     Feb 26, 2010 3:28:20 PM     Value: /usr/nsh/tmp/application_server/scripts/job__a43f0d2c-58fc-49ad-8375-4521f353fdc1/hosts_4b5a793f-6a91-4d99-8e35-3d2e27ebae55

              Info     Feb 26, 2010 3:28:20 PM     win1

              Info     Feb 26, 2010 3:28:20 PM     red1

              Info     Feb 26, 2010 3:28:20 PM     blprov01

              Info     Feb 26, 2010 3:28:20 PM     esx1

              Info     Feb 26, 2010 3:28:20 PM     blapp

              Info     Feb 26, 2010 3:28:20 PM     blapp-win

              Info     Feb 26, 2010 3:28:20 PM     sles2

              Info     Feb 26, 2010 3:28:20 PM     sol1

              Info     Feb 26, 2010 3:28:20 PM     red4

              Info     Feb 26, 2010 3:28:20 PM     cos1

              Info     Feb 26, 2010 3:28:20 PM     bsara

              Info     Feb 26, 2010 3:28:20 PM     red2

              Info     Feb 26, 2010 3:28:20 PM     win2

              Info     Feb 26, 2010 3:28:20 PM     blfs

              Info     Feb 26, 2010 3:28:20 PM     sles1

              Info     Feb 26, 2010 3:28:20 PM     192.168.66.115

              Info     Feb 26, 2010 3:28:20 PM     Exit Code 0

              Info     Feb 26, 2010 3:28:20 PM     The job 'foo' has succeeded

              • 4. Re: Using the %f macro in an NSH Script Job
                S Crawford

                So /usr/nsh/tmp/application_server/scripts/job__a43f0d2c-58fc-49ad-8375-4521f353fdc1/hosts_4b5a793f-6a91-4d99-8e35-3d2e27ebae55 is the output file that it created or is that just a temporary copy of the NSH script used for execution?

                • 5. Re: Using the %f macro in an NSH Script Job
                  Bill Robinson

                  It's the output file created on the fly by the job.  so if you wanted to use it in your script you'd do something like:

                   

                  For server in `cat $1`

                  Do

                  Echo "server: $server"

                  Done

                  • 6. Re: Using the %f macro in an NSH Script Job

                    i've also found that the %f file gets automatically deleted after job execution

                    i was also forced to use %f instead of %h in one job where my target list was more than 1000 servers

                    at least on a windows appserver there seems to be some lenght limit on the %h parameter

                    %f does not seem to have such a limit

                    • 7. Re: Using the %f macro in an NSH Script Job
                      Stephane Tardif

                      Is the 1000 servers limit for %h in version 8.7 and above still exists?

                      • 8. Re: Using the %f macro in an NSH Script Job
                        Bill Robinson

                        afaik there's no hard limit.  the shell can only handle a certain number of characters in memory -like if you do 'export FOO=$(<hugefile)' that will be a problem.

                         

                        is there some problem w/ using %f ?

                        • 9. Re: Using the %f macro in an NSH Script Job
                          Stephane Tardif

                          I confirm with version 8.7 that %h passed the 3454 servers to my nsh.  I don't have a problem using %f else than I'm an idiot learning to shell script and it feels more complicated.

                          But surprisingly it took 15 minutes to run and all I do is write the server names to a file.

                          Here is my very simple nsh script if it can help some people.

                           

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

                          # ExportToCSV-TargetServer.nsh
                          # by Stephane Tardif
                          # August 29, 2017
                          #
                          # .SYNOPSIS
                          #   Export the batch job's target List to a CSV file
                          # .PARAMETER FileServer
                          # 1st Attribute Required File server example fileserver.domain.local
                          # .PARAMETER FileLocation
                          # 2nd Attribute: Required Directory path including the drive letter
                          #   but do not include the beginning and ending slash
                          # .PARAMETER FileName
                          # 3rd Attribute Required File Name no extension. note that file name is prefixed by the date
                          # .PARAMETER ServerList
                          # 4th Attribute Required is %h Macro
                          # .SCRIPT TYPE
                          # Type 2
                          ###################################################################

                          FileServer=$1
                          FileLocation=$2
                          FileName=$3
                          ServerList="${4}"
                          DateTime=`date +%Y%m%d-%H%M%S`
                          ExportFile="//$FileServer/$FileLocation/$DateTime$FileName.csv"

                          echo "Zeroing $ExportFile"
                          rm -fi $ExportFile
                          echo "ComputerName"'\r' > $ExportFile

                          for SERVER in $ServerList
                          do 
                            echo "$SERVER"
                            echo "$SERVER"'\r' >> $ExportFile
                          done

                          • 10. Re: Using the %f macro in an NSH Script Job
                            Bill Robinson

                            all I do is write the server names to a file.

                            ok, so why not use %f as the first arg and do:

                            cp "${1}" "${ExportFile}"

                            ?

                            • 11. Re: Using the %f macro in an NSH Script Job
                              Stephane Tardif

                              Great stuff!  it went from 15 minutes to 7 seconds to run.

                               

                               

                               

                               

                               

                              ServerList="${1}"

                               

                              FileServer=$2

                               

                              FileLocation=$3

                               

                              FileName=$4

                               

                               

                               

                               

                               

                              DateTime=`date +%Y%m%d-%H%M%S`

                               

                              ExportFile="//$FileServer/$FileLocation/$DateTime$FileName.csv"

                               

                               

                               

                               

                               

                              rm -fi $ExportFile

                               

                              cp "$ServerList" "${ExportFile}"

                               

                              It is not really a CSV since it is missing the column's title but I can live with that.