There are a few blogs on this site that feature the Nitro API.  Here are a few examples that you should absolutely read for some excellent background information:

  1. /blogs/2011/08/27/nitro-providing-programmatic-management-and-monitoring-through-command-center/
  1. /blogs/2011/12/01/nitro-quick-tips-on-writing-java-based-program/
  1. /blogs/2011/12/07/nitro-unleash-the-power-using-powershell/

This particular article extends the last article in the sense that I demonstrate how to acquire data from the NetScalers, utilizing powershell and the Nitro API.  The use of the Nitro APIs is simple in this article, in that I only use the “GET” methods to acquire data.  The goal is to provide additional real world examples of how to utilize the Nitro APIs and how the various methods are related to each other when attempting to coalesce the right data for output.

The script featured in this article will output data relating to a Load Balancing virtual server.  Below is an example of output:

Example Output #1

Citrix Load Balancing Virtual Server
vServerName: LB_SSL_Test
Protocol: SSL
IPAddress: 192.168.1.189
Port: 443
LoadBalancingMethod: LEASTCONNECTION
PersistenceType: NONE
TimeOut: 2
BackupVirtualServer:
CertKey Name: Wildcard
Certificate File Name: certnew.cer
Certificate Key File Name: /nsconfig/ssl/Wildcard.Key
Citrix Load Balancing Services
ServiceName: Test_CNN
Port: 80
IPAddress: 0.0.0.0
Protocol: HTTP
Citrix Load Balancing Monitors
MonitorName: ping
Type: PING
MonitorName: tcp
Type: TCP
MonitorName: ftp
Type: FTP
MonitorName: https
Type: HTTP

 Example Output #2

Citrix Load Balancing Virtual Server
vServerName: Test_IPv6
Protocol: HTTP
IPAddress: 2002:d8c7:8e39:0:c9c6:9990:21f3:3806
Port: 80
LoadBalancingMethod: LEASTCONNECTION
PersistenceType: NONE
TimeOut: 2
BackupVirtualServer:
Citrix Load Balancing Services
ServiceName: Test_IPv6_Svc
Port: 80
IPAddress: 192.168.1.125
Protocol: HTTP
Citrix Load Balancing Monitors
MonitorName: arp
Type: ARP
MonitorName: ping
Type: PING
MonitorName: tcp
Type: TCP
MonitorName: http
Type: HTTP
MonitorName: tcp-ecv
Type: TCP-ECV
MonitorName: http-ecv
Type: HTTP-ECV

Below are the main Nitro APIs utilized in the script:

[com.citrix.netscaler.nitro.service.nitro_service] – nitro_service is a client interface through which Nitro operations are performed on NetScaler resources.

[com.citrix.netscaler.nitro.resource.config.lb.lbvserver] – Load Balancing virtual server configuration .

[com.citrix.netscaler.nitro.resource.config.ssl.sslcertkey] – SSL certificate configuration.

[com.citrix.netscaler.nitro.resource.config.ssl.sslcertkey_sslvserver_binding] – Binding class showing the virtual server that is bound to an SSL CertKey.

[com.citrix.netscaler.nitro.resource.config.lb.lbvserver_service_binding] – Binding class that shows the service that can be bound to a Load Balancing virtual server.

[com.citrix.netscaler.nitro.resource.config.lb.lbmonitor] – Load Balancing monitor configuration.

[com.citrix.netscaler.nitro.resource.config.lb.lbmonbindings_service_binding] – Binding class that shows the service that is bound to Load Balancing monitors.

[com.citrix.netscaler.nitro.resource.config.lb.lbvserver_servicegroup_binding] – Binding class that shows the service groups that are bound to Load Balancing virtual servers.

[com.citrix.netscaler.nitro.resource.config.lb.lbvserver_servicegroupmember_binding] – Binding class that shows the service group members that are bound to Load Balancing virtual servers.

[com.citrix.netscaler.nitro.resource.config.lb.lbmonbindings_servicegroup_binding] – Binding class that shows the monitors that are bound to the service groups

Below is the powershell script utilized to generate the output for Load Balancing virtual servers:

 

$nsip = '192.168.1.140'
$user = 'nsroot'
$pass = 'nsroot'
$path1 = "C:\Nitro\ns-10.1-122.17-sdk.tar\ns-10.1-122.17-sdk\ns_nitro-csharp_dara_122_17\lib\Newtonsoft.Json.dll"
$O = [System.Reflection.Assembly]::LoadFile($path1)
$path = "C:\Nitro\ns-10.1-122.17-sdk.tar\ns-10.1-122.17-sdk\ns_nitro-csharp_dara_122_17\lib\nitro.dll"
$O = [System.Reflection.Assembly]::LoadFile($path)

$nitrosession = new-object com.citrix.netscaler.nitro.service.nitro_service($nsip,"http")
$session = $nitrosession.login($user,$pass)
$BeginHTMLTable = "<table border=1px width=800px>"
$EndHTMLTable = "</table>"
$BeginRow = "<tr>"
$EndRow = "</tr>"
$TableHeading = "<th>NetScaler Object</th> <th>Values</th>"
$HTMLFile = "c:\Scripts\NS_LB_HTML.htm"

$AllLBvServers = [com.citrix.netscaler.nitro.resource.config.lb.lbvserver]::get($nitrosession)
$AllSSLCerts = [com.citrix.netscaler.nitro.resource.config.ssl.sslcertkey]::get($nitrosession)
$BeginHTMLTable | Add-Content $HTMLFile
$TableHeading | Add-Content $HTMLFile
foreach ($x in $AllLBvServers)
{

 $BeginRow | Add-Content $HTMLFile
 "<td bgcolor=#808080><font face=garamond color=white>Citrix Load Balancing Virtual Server</font></td><td bgcolor=#808080><font face=garamond></font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile

 $BeginRow | Add-Content $HTMLFile
 $VName = $x.name
 "<td><font face=garamond>vServerName: </font></td><td><font face=garamond>" + "$VName" +"</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile

 $BeginRow | Add-Content $HTMLFile
 $VProtocol= $x.servicetype
 "<td><font face=garamond>Protocol: </font></td><td><font face=garamond>" + "$VProtocol" + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VIP = $x.ipv46
 "<td><font face=garamond>IPAddress: </font></td><td><font face=garamond>" + $VIP + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VPort = $x.port
 "<td><font face=garamond>Port: </font></td><td><font face=garamond>" + $VPort + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VLBMethod = $x.lbmethod
 "<td><font face=garamond>LoadBalancingMethod: </font></td><td><font face=garamond>" + $VLBMethod + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VPersistence = $x.persistencetype
 "<td><font face=garamond>PersistenceType: </font></td><td><font face=garamond>" + $VPersistence + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VTimeout = $x.timeout
 "<td><font face=garamond>TimeOut: </font></td><td><font face=garamond>" + $VTimeout + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VBackupvServer = $x.backupvserver
 "<td><font face=garamond>BackupVirtualServer: </font></td><td><font face=garamond>" + $VBackupvServer + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
#SSL Cert Infor:
foreach ($b in $AllSSLCerts)
{ $SSLCertKeyBinding = [com.citrix.netscaler.nitro.resource.config.ssl.sslcertkey_sslvserver_binding]::get($nitrosession,$b.certkey)
 if ($SSLCertKeyBinding.servername -eq $x.name)
 {
$BeginRow | Add-Content $HTMLFile
 $VCertKey = $b.certkey
 "<td><font face=garamond>CertKey Name: </font></td><td><font face=garamond>" + $VCertKey + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VCert = $b.cert
 "<td><font face=garamond>Certificate File Name: </font></td><td><font face=garamond>" + $VCert + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VKey = $b.key
 "<td><font face=garamond>Certificate Key File Name: </font></td><td><font face=garamond>" + $VKey + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
 }

 } #end of $AllSSLCerts foreach loop.

 $LBvServerBinding = [com.citrix.netscaler.nitro.resource.config.lb.lbvserver_service_binding]::get($nitrosession, $x.name)
 $AllMonitors = [com.citrix.netscaler.nitro.resource.config.lb.lbmonitor]::get($nitrosession)
foreach ($y in $LBvServerBinding)
{
 $BeginRow | Add-Content $HTMLFile
 "<td bgcolor=#d3d3d3 ><font face=garamond color=#ffffff>Citrix Load Balancing Services</font></td><td bgcolor=#d3d3d3></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile

 $BeginRow | Add-Content $HTMLFile
 $VServiceName = $y.servicename
 "<td><font face=garamond>ServiceName: </font></td><td><font face=garamond>" + $VServiceName + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VServicePort = $y.port
 "<td><font face=garamond>Port: </font></td><td><font face=garamond>" + $VServicePOrt + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VServiceIP = $y.ipv46
 "<td><font face=garamond>IPAddress: </font></td><td><font face=garamond>" + $VServiceIP + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VServiceProtocol = $y.servicetype
 "<td><font face=garamond>Protocol: </font></td><td><font face=garamond>" + $VServiceProtocol + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile

 $BeginRow | Add-Content $HTMLFile
 "<td bgcolor=#778899><font face=garamond color=#ffffff>Citrix Load Balancing Monitors</font></td><td bgcolor=#778899></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
 foreach ($z in $AllMonitors)
{

 $MonitorList = [com.citrix.netscaler.nitro.resource.config.lb.lbmonbindings_service_binding]::get($nitrosession, $z.monitorname ) | Where-Object { $_.servicename -eq $y.servicename }

 foreach ($a in $MonitorList)

 {
 if ($a.monitorname -eq $Null)

 {

 }

 else

 {
 if ($a.monitorname -eq $z.monitorname)
 {
 $BeginRow | Add-Content $HTMLFile
 $VMonitorName = $a.monitorname
 "<td><font face=garamond> MonitorName: </font></td><td><font face=garamond>" + $VMonitorName + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VType = $z.type
 "<td><font face=garamond>Type: </font></td><td><font face=garamond>" + $VType + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
}
 }

 }

 }

 }

 $ServiceGroupObj = [com.citrix.netscaler.nitro.resource.config.lb.lbvserver_servicegroup_binding]::get($nitrosession, $x.name)

 if ($ServiceGroupObj -eq $Null)
 {}
else
 {
 $BeginRow | Add-Content $HTMLFile
 "<td bgcolor=#b0c4de><font face=garamond>Citrix Load Balancing Service Groups</font></td><td bgcolor=#b0c4de></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
 }

 foreach ($e in $ServiceGroupObj)

 {
 $BeginRow | Add-Content $HTMLFile
 $VServiceGroup = $e.servicegroupname
 "<td><font face=garamond>ServiceGroupName: </font></td><td><font face=garamond>" + $VServiceGroup + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile

 $ServiceGroupMemberObj = [com.citrix.netscaler.nitro.resource.config.lb.lbvserver_servicegroupmember_binding]::get($nitrosession, $x.name)
 $AllMonitors = [com.citrix.netscaler.nitro.resource.config.lb.lbmonitor]::get($nitrosession)

 foreach ($f in $ServiceGroupMemberObj )
 {
 $BeginRow | Add-Content $HTMLFile
 $VServiceGrpMember = $f.ipv46
 "<td><font face=garamond>ServiceGroupMember: </font></td><td><font face=garamond>" + $VServiceGrpMember + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VSeriveMemberPort = $f.port
 "<td><font face=garamond>Port: </font></td><td><font face=garamond>" + $VSeriveMemberPort + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile
$BeginRow | Add-Content $HTMLFile
 $VServiceGrpType = $f.servicetype
 "<td><font face=garamond>Protocol: </font></td><td><font face=garamond>" + $VServiceGrpType + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile

 foreach ($g in $AllMonitors)
 {

 $ServiceGroupMonitor = [com.citrix.netscaler.nitro.resource.config.lb.lbmonbindings_servicegroup_binding]::get($nitrosession, $g.monitorname ) | Where-Object {$_.servicegroupname -eq $e.servicename}

 if ($ServiceGroupMonitor.monitorname -eq $Null)
 { } #Do Nothing

 else

 {
 $BeginRow | Add-Content $HTMLFile
 $VServiceMonitorName = $ServiceGroupMonitor.monitorname
 "<td><font face=garamond>ServiceGroupMonitor: </font></td><td><font face=garamond>" + $VServiceMonitorName + "</font></td>" | Add-Content $HTMLFile
 $EndRow | Add-Content $HTMLFile

 }
}

 }

 }

}
$EndHTMLTable | Add-Content $HTMLFile


This article isn’t meant to teach powershell, but to provide a real-world example of how one could use powershell to generate powerful scripts to automate  tasks on the Citrix NetScaler, utilizing the Nitro API.

Note:  In order to view the script in it’s entirety, right click on the web page and click view page source.  Locate the script and copy and paste it into your favorite editor.

I hope you find this to be informative.