This blog is a follow up to my last blog where I featured a powershell script for acquiring data for configured load balancing virtual servers on a NetScaler appliance.  This article features a powershell script that acquires configuration data for content switching.  If you haven’t read the first blog to this series, you can locate it here:

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

Example Output #1

Citrix Content Switching Virtual Server
Name TestSSL
Protocol SSL
IPAddress 192.168.1.186
Port 443
Citrix Content Switching Policy
Content Switching Policy Name FoobarTest
Content Switching Expression HTTP.REQ.HOSTNAME.EQ(“192.168.1.186”)
Citrix Responder Policy
Responder Policy Name Global-SSL-Redirect
Responder Expression true
Responder Action Type redirect
Responder Action Target https://” + HTTP.REQ.HOSTNAME.HTTP_HEADER_SAFE + HTTP.REQ.URL.HTTP_URL_SAFE
Citrix Content Switching Target Load Balancing Server
Target Load Balancing vServer CitrixSF1_LB

Example Output #2

Citrix Content Switching Virtual Server
Name Test2
IPAddress 192.168.1.186
Port 80
Citrix Content Switching Policy
Content Switching Policy Name CS_2
Content Switching Expression http.REQ.HOSTNAME.SERVER.EQ(“www.example.com”)
Content Switching Policy Name IOT_CS_Policy
Content Switching Expression http.REQ.HOSTNAME.SERVER.EQ(“www.example.com”) && http.REQ.URL.PATH.CONTAINS(“/VirtualDirectory”)
Citrix Responder Policy
Responder Policy Name IOT_Responder_Policy
Responder Expression HTTP.REQ.URL.PATH.CONTAINS(“/VirtualDirectory/Folder“)
Responder Action Type respondwith
Responder Action Target http://www.example.com/VirtualDirectory/Folder
Citrix Rewrite Policy
Rewrite Policy Name IOT_Rewrite_Policy
Rewrite Policy Expression http.REQ.HOSTNAME.SERVER.EQ(“IOT.xlion.net”) && http.REQ.URL.PATH.CONTAINS(“/INDOT”)
Rewrite Policy Action IOT_Rewrite_Action
Rewrite Policy Type replace
Rewrite Policy Target HTTP.REQ.HOSTNAME.SERVER
Rewrite Policy Replacement Text “CitrixSF1.xlion.net”
Citrix Transform Policy
Transform Policy Name IOT_URL_TRansform_Policy
Transform Policy Expression http.REQ.HOSTNAME.SERVER.EQ(“IOT.xlion.net”) && http.REQ.URL.PATH.CONTAINS(“/INDOT”)
Transform Profile Name IOT_URL_TRansform_Profile
Transform Action Name IOT_URL_Transform_Action
Transform Action State ENABLED
Transform Action Req From http://IOT.xlion.net/INDOT
Transform Action Req Into http://CitrixSF1.Xlion.net/INDOT
Transform Action Res From http://CitrixSF1.Xlion.net/INDOT
Transform Action Res Into http://IOT.xlion.net/INDOT
Citrix Content Switching Target Load Balancing Server
Target Load Balancing vServer CitrixSF1_LB

Below are the main Nitro APIs utilized in the script:

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

com.citrix.netscaler.nitro.resource.config.cs.csvserver – Content Switching virtual server configuration.

com.citrix.netscaler.nitro.resource.config.cs.csvserver_cspolicy_binding – Binding class showing the content switching policy that is bound to the content switching virtual server.

com.citrix.netscaler.nitro.resource.config.cs.cspolicy – Content Switching policy configuration.

com.citrix.netscaler.nitro.resource.config.cs.csvserver_responderpolicy_binding –  Binding class showing the responder policy that is bound to the content switching virtual server.

com.citrix.netscaler.nitro.resource.config.responder.responderpolicy – Responder policy configuration.

com.citrix.netscaler.nitro.resource.config.responder.responderaction – Responder policy action configuration.

com.citrix.netscaler.nitro.resource.config.cs.csvserver_rewritepolicy_binding – Binding class showing the rewrite policy that is bound to the content switching virtual server.

com.citrix.netscaler.nitro.resource.config.rewrite.rewritepolicy – Rewrite policy configuration.

com.citrix.netscaler.nitro.resource.config.rewrite.rewriteaction – Rewrite policy action configuration.

com.citrix.netscaler.nitro.resource.config.cs.csvserver_transformpolicy_binding – Binding class showing the transform policy that is bound to the content switching virtual server.

com.citrix.netscaler.nitro.resource.config.transform.transformpolicy – Transform policy configuration.

com.citrix.netscaler.nitro.resource.config.transform.transformaction – Transform policy action configuration.

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_CS_HTML.htm”

$BeginHTMLTable | Add-Content $HTMLFile
$TableHeading | Add-Content $HTMLFile
$ret_obj = [com.citrix.netscaler.nitro.resource.config.lb.lbvserver]::get($nitrosession)
$AllCSvServers = [com.citrix.netscaler.nitro.resource.config.cs.csvserver]::get($nitrosession)

foreach ($a in $AllCSvServers)

{
$BeginRow | Add-Content $HTMLFile
“<td bgcolor=#808080><font face=garamond color=white>Citrix Content Switching Virtual Server </font></td><td bgcolor=#808080><font face=garamond></font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$CSName = $a.name
“<td><font face=garamond>Name </font></td><td><font face=garamond>” + $CSName + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$VProtocol = $a.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 = $a.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 = $a.port
“<td><font face=garamond>Port </font></td><td><font face=garamond>” + $VPort + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$CSPolicy = [com.citrix.netscaler.nitro.resource.config.cs.csvserver_cspolicy_binding]::get($nitrosession,$a.name)

$BeginRow | Add-Content $HTMLFile
“<td><font face=garamond>Citrix Content Switching Policy</font></td><td><font face=garamond></font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

foreach ($b in $CSPolicy)
{

$BeginRow | Add-Content $HTMLFile
$CSPolicyName = $b.policyname
“<td><font face=garamond>Content Switching Policy Name </font></td><td><font face=garamond>” + $CSPolicyName + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$GetCSPolicyObj = [com.citrix.netscaler.nitro.resource.config.cs.cspolicy]::get($nitrosession, $b.policyname)
foreach ($c in $GetCSPolicyObj)

{
$BeginRow | Add-Content $HTMLFile
$CSExpression = $c.rule
“<td><font face=garamond>Content Switching Expression </font></td><td><font face=garamond>” + $CSExpression + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

}

}
$ResponderPolicy = [com.citrix.netscaler.nitro.resource.config.cs.csvserver_responderpolicy_binding]::get($nitrosession, $a.name)

if ($ResponderPolicy -eq $null)
{ } #Do Nothing

else

{
foreach ($d in $ResponderPolicy)
{
$BeginRow | Add-Content $HTMLFile
“<td><font face=garamond>Citrix Responder Policy</font></td><td><font face=garamond></font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$CSRespPolName = $d.policyname
“<td><font face=garamond>Responder Policy Name </font></td><td><font face=garamond>” + $CSRespPolName + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile
}
$ResponderPolicyBinding = [com.citrix.netscaler.nitro.resource.config.responder.responderpolicy]::get($nitrosession,$d.policyname)

$BeginRow | Add-Content $HTMLFile
$ResExpression = $ResponderPolicyBinding.rule
“<td><font face=garamond>Responder Expression </font></td><td><font face=garamond>” + $ResExpression + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$ResponderAction = [com.citrix.netscaler.nitro.resource.config.responder.responderaction]::get($nitrosession,$ResponderPolicyBinding.action)

$BeginRow | Add-Content $HTMLFile
$ResAction = $ResponderAction.type
“<td><font face=garamond>Responder Action Type </font></td><td><font face=garamond>” + $ResAction + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$ResTarget = $ResponderAction.target
“<td><font face=garamond>Responder Action Target</font></td><td><font face=garamond>” + $ResTarget + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile
}
$RewritePolicy = [com.citrix.netscaler.nitro.resource.config.cs.csvserver_rewritepolicy_binding]::get($nitrosession, $a.name)

if ($RewritePolicy -eq $null)
{}
else
{

$BeginRow | Add-Content $HTMLFile
“<td><font face=garamond>Citrix Rewrite Policy</font></td><td><font face=garamond></font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

#”RewritePolicyName ” + $RewritePolicy.policyname
foreach ($e in $RewritePolicy)

{
$BeginRow | Add-Content $HTMLFile
$RePolName = $e.policyname
“<td><font face=garamond>Rewrite Policy Name </font></td><td><font face=garamond>” + $RePolName + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$RewritePolicyObj = [com.citrix.netscaler.nitro.resource.config.rewrite.rewritepolicy]::get($nitrosession, $e.policyname)

$BeginRow | Add-Content $HTMLFile
$RePolExpression = $RewritePolicyObj.rule
“<td><font face=garamond>Rewrite Policy Expression </font></td><td><font face=garamond>” + $RePolExpression + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$RewritePolicyAction = [com.citrix.netscaler.nitro.resource.config.rewrite.rewriteaction]::get($nitrosession, $RewritePolicyObj.action)

$BeginRow | Add-Content $HTMLFile
$RePolicyActionName = $RewritePolicyAction.name
“<td><font face=garamond>Rewrite Policy Action </font></td><td><font face=garamond>” + $RePolicyActionName + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$RePolicyType = $RewritePolicyAction.type
“<td><font face=garamond>Rewrite Policy Type </font></td><td><font face=garamond>” + $RePolicyType + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$RePolicyTarget = $RewritePolicyAction.target
“<td><font face=garamond>Rewrite Policy Target </font></td><td><font face=garamond>” + $RePolicyTarget + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$RePolicyReplacement = $RewritePolicyAction.stringbuilderexpr
“<td><font face=garamond>Rewrite Policy Replacement Text </font></td><td><font face=garamond>” + $RePolicyReplacement + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile
}

}

$TransformPolicyName = [com.citrix.netscaler.nitro.resource.config.cs.csvserver_transformpolicy_binding]::get($nitrosession, $a.name)

if ($TransformPolicyName -eq $null)
{}

else
{

$BeginRow | Add-Content $HTMLFile
“<td><font face=garamond>Citrix Transform Policy</font></td><td><font face=garamond></font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

foreach ($g in $TransformPolicyName)
{

$BeginRow | Add-Content $HTMLFile
$TranPolName = $g.policyname
“<td><font face=garamond>Transform Policy Name </font></td><td><font face=garamond>” + $TranPolName + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$TransformPolicyObj = [com.citrix.netscaler.nitro.resource.config.transform.transformpolicy]::get($nitrosession, $g.policyname)

$BeginRow | Add-Content $HTMLFile
$TranPolExpression = $TransformPolicyObj.rule
“<td><font face=garamond>Transform Policy Expression </font></td><td><font face=garamond>” + $TranPolExpression + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$TranProfileName = $TransformPolicyObj.profilename
“<td><font face=garamond>Transform Profile Name </font></td><td><font face=garamond>” + $TranProfileName + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$TransformPolicyActionObj = [com.citrix.netscaler.nitro.resource.config.transform.transformaction]::get($nitrosession) | Where-Object {$_.profilename -eq $TransformPolicyObj.profilename}

$BeginRow | Add-Content $HTMLFile
$TranActionName = $TransformPolicyActionObj.name
“<td><font face=garamond>Transform Action Name </font></td><td><font face=garamond>” + $TranActionName + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$TranActionState = $TransformPolicyActionObj.state
“<td><font face=garamond>Transform Action State </font></td><td><font face=garamond>” + $TranActionState + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$TranPolActionFrom = $TransformPolicyActionObj.requrlfrom
“<td><font face=garamond>Transform Action Req From </font></td><td><font face=garamond>” + $TranPolActionFrom + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$TranPolActionInto = $TransformPolicyActionObj.requrlinto
“<td><font face=garamond>Transform Action Req Into </font></td><td><font face=garamond>” + $TranPolActionInto + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$TranPolActionURLFrom = $TransformPolicyActionObj.resurlfrom
“<td><font face=garamond>Transform Action Res From </font></td><td><font face=garamond>” + $TranPolActionURLFrom + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$TranPolActionURLInto = $TransformPolicyActionObj.resurlinto
“<td><font face=garamond>Transform Action Res Into </font></td><td><font face=garamond>” + $TranPolActionURLInto + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

}
}

$BeginRow | Add-Content $HTMLFile
“<td><font face=garamond>Citrix Content Switching Target Load Balancing Server </font></td><td><font face=garamond></font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

$BeginRow | Add-Content $HTMLFile
$TargetLBServer = $b.targetlbvserver
“<td><font face=garamond>Target Load Balancing vServer </font></td><td><font face=garamond>” + $TargetLBServer + “</font></td>” | Add-Content $HTMLFile
$EndRow | Add-Content $HTMLFile

}
$EndHTMLTable | Add-Content $HTMLFile

This article isn’t intended 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.