Sample NSH script - SetServerPropertyValue.nsh
This example script sets a certain property to a certain value on all the servers that are enrolled in your BMC Server Automation system. This script will be run from within an NSH Script Job.
- Before you begin
- Adding a new server property
- Examining SetServerPropertyValue.nsh
- Where to go from here
Before you begin
- Add a new server property called MY_PROPERTY. For information on how to do this, see Add new server property.
- Enroll the Application Server as a managed server in the BMC Server Automation system.
From within the BMC Server Automation Console, expand the Servers folder, right click a server group, and select Add Server. Then fill in the name or IP address of the Application Server.
Adding a new server property
To add a new server property, do either of the following:
- Adding a new server property using the BLCLI
- Adding a new server property using the BMC Server Automation Console
Adding a new server property by using the BLCLI
- Open a command prompt and start NSH by typing nsh.
C:\some_directory>nsh
The prompt changes to your NSH prompt.
MY_NSH_PROMPT% Assuming that you have set up authentication, (see Setting up authentication), use the blcli_setoptioncommand to specify the service profile and role that you want to use.
MY_NSH_PROMPT% blcli_setoption serviceProfileName myServiceProfile
MY_NSH_PROMPT% blcli_setoption roleName BLAdminsPopulate the credentials cache.
MY_NSH_PROMPT% blcred cred -acquire -profile myServiceProfile
-username testUser -password testPassword
Authentication succeeded: acquired session credential- Connect to the Application Server.
MY_NSH_PROMPT% blcli_connect Use the PropertyClass addProperty command to add a new Server
property called MY_PROPERTY.MY_NSH_PROMPT% blcli_execute PropertyClass addProperty "Class://SystemObject/Server" "MY_PROPERTY" "My Property" "Primitive:/String" true false "initialDefaultValue"The PropertyClass addProperty command returns void.
MY_NSH_PROMPT% void
Adding a new server property by using the BMC Server Automation Console
- Start the BMC Server Automation Console.
- Choose Configuration > Property Dictionary View.
- Expand Built-in Property Classes.
- Scroll down to the Server property class and then click the Server property class.
- Click Add new property
.
- Fill in the Add Property window as follows. Be sure to call the new property MY_PROPERTY and accept all defaults.
Examining SetServerPropertyValue.nsh
02 blcli_execute Server listAllServers
03 blcli_storeenv SERVERS
04
05 for SERVER in ${SERVERS}
06 do
07 blcli_execute Server setPropertyValueByName ${SERVER} MY_PROPERTY "some Value"
08 done
Line 1: Connect to the Application Server, using the blcli_connect command. (For more information about this command, see blcli_connect.)
Why no authentication? In the previous tutorial (Learning-how-to-run-your-first-BLCLI-command), you performed authentication by using the -v option to specify a service profile and the -r command to specify a role. In this tutorial, the SetServerPropertyValue.nsh script will be run within an NSH Script Job, and therefore will inherit authentication from the Application Server.
For more advanced information on authentication in different scripting environments, see Additional-authentication-details-Running-scripts-from-different-places.
Line 2: Use blcli_execute to execute the command:
This command lists all the servers that are enrolled in the BMC Server Automation system, and does not take any arguments. (For more information about this command, see blcli_execute.)
You can store the output from this command (the list of servers) in an environment variable, as explained in the next line.
Line 3: Use the blcli_storeenv command to store the output from the previous blcli_execute command in an environment variable. In this case, the script stores the list of enrolled servers in the SERVERS variable.
Lines 5-7: Run the Server setPropertyValueByName command against each SERVER in the SERVERS list:
This command takes three arguments:
- The name of the individual server on which you want to set a property. This is taken from the SERVERS list.
- The name of the property that you want to set. In this case, the property is MY_PROPERTY, which you created at the start of this tutorial. (See Adding a new server property.)
The value that you want to set the property to. In this case, you are setting MY_PROPERTY to "some Value".
Where to go from here
Now that you have examined the script, you need to add it to the Depot. See Adding-the-NSH-script-to-the-Depot.