Check… check… Did you try the Java based NITRO API program discussed in last blog (/blogs/2011/12/01/nitro-quick-tips-on-writing-java-based-program/)? Needless to say, you can only realize the power of this great framework by trying out these examples. May be you are the Microsoft fan and here is the same example using PowerShell which you are certainly going to try out 🙂
NetScaler NITRO API module has built-in NetScaler SDK for CSharp which is similar to what we have for Java and it helps when you start using these built-in APIs to code your PowerShell program. The SDK for CSharp is a bundle of documents, Samples and library files. The libraries are just DLLs containing object files for the classes used in NITRO. The SDK documents include a getting started guide and detailed information on the classes and methods available within the classes. SDK’s can be found under the ‘Downloads’ Tab of your Netscaler UI.
Let us start writing the PowerShell script to do the following operations on the Netscaler:
- Login to the Netscaler
- Add a lbvserver
- Set the lbvserver
- Get the lbvserver by Name
- Remove the lbvserver
- Log out of the session
Step 1: Load the required Modules.
The two library files in the SDK are loaded in to the assembly.
- $path1 = Resolve-Path Newtonsoft.Json.dll
- $path = Resolve-Path nitro.dll
Step 2: Create a Session with the NetScaler.
The class ‘nitro_service’ can be used for creating a session with the Netscaler. To start with the script, please first create an object for the nitro_service class available in the nitro.dll.
- $nitrosession = new-object com.citrix.netscaler.nitro.service.nitro_service($nsip,”https”)
- $session = $nitrosession.login($user,$pass)
You have created the session object and logged into the NetScaler using username and password. Any configuration call through NITRO returns a ‘base_response’ object. This response will include an error code and a message. For a successful transaction, the response will be ‘zero’ for error code and ‘Done’ for message. Any Non-zero error code is considered as a failure.
Step 3: Add an lbvserver
Now that you have created the session with Netscaler, you can create the LB vserver on NetScaler. To add an lbvserver, please create the object for lbvserver class and set the mandatory arguments for the object. The mandatory arguments for lbvserver will be ‘name’ and ‘servicetype’.
- $lbvserver1 = New-Object com.citrix.netscaler.nitro.resource.config.lb.lbvserver
When you are done with the object, call the ‘add’ method which takes two parameters, the session object and the lbvserver object. The Response can be processed for any failures.
Step 4: Set an lbvserver
In dynamic situations, you might have to change the properties of existing entities on the Netscaler. To change the properties you can use the method ‘update’ available in the corresponding base class. Let us see how to set/update the ‘lbmethod’ property for the lbvserver that you created. We will be using the same lbvserver object which was created while adding the lbvserver. The set operation is done by calling the update method from the lbvserver class.
Step 5: Get an lbvserver
How do you verify if all the operations that were done before, have successfully updated the Netscaler. Get operation on any entity can be done in two ways.
- Get all the entities based on the type or group
- Get a specific entity by name of the same
Sample below gets the LB vserver by Name:
- $ret_obj= [com.citrix.netscaler.nitro.resource.config.lb.lbvserver]::get($nitrosession,$lbvserver1.name)
Here, the return value will be lbvserver object itself. So variable $ret_obj will have all the properties of the lbvserver that you have retrieved using NITRO. This variable can be parsed for getting the values.
- Write-Host “Show lbvserver: lbvserver name – ” $ret_value.name “, servicetype – ” $ret_value.servicetype “, port – ” $ret_value.port ” , IP Address – ” $ret_value.ipv46
The $ret_value.name parameter returns the Name of the lbvserver. All the values related to the lbvserver object can be obtained the same way. For more details on the structure of the lbvserver class, you can refer the documents in the SDK.
Step 6: Remove an lbvserver
Time to remove the LB vserver you have created. The operation is similar to other operations like add and set. The method used for removing the resources will be ‘delete’.
Step 7: Log out of the Session
Learning from last example, it is always advised to log out the session established with the Netscaler for security reasons. Logout is again a simple operation that will terminate the established session.
- $session = $nitrosession.logout()
With this you have ensured successful logout from the system. Be sure to parse the response and ensure a successful logout. Now you will say that the language and environment does not matter, it is just easy to use NITRO APIs using NetScaler NITRO SDK.