This function will invoke a query against a Citrix NetScaler with the 'NITRO' API.

Citrix makes something called a NetScaler.  They use fancy terms like "world's most advanced cloud network platform."  It does have a good number of functions.  Unfortunately, for those on the Windows side of the house, Citrix has stuck with a Java based management solution, barring direct CLI access.

On a positive note, it turns out they provide a decent API (for my needs), albeit with an odd name.


This function is quite rudimentary - wrote it with the minimal guidance provided online (Didn't realize this, but NetScaler REST API docs are buried in the NetScaler bits, not available online yet).  TODO:



    #Edit this section and save the new Invoke-NSCustomQuery.ps1 
        [string]$Address = "CTX-NS-TST-01"#... 
#dot source the script (or add to your profile or a custom module):      
    . "\\path\to\Invoke-NSCustomQuery.ps1"     
#Get help on Invoke-NSCustomQuery  
    Get-Help Invoke-NSCustomQuery -Full 
#List available 'config' objects we can query.  lbvserver, server, service and servicegroup are a few examples: 
    Invoke-NSCustomQuery -Address CTX-NS-TST-01 -QueryType config -list -Credential $cred 
#pull the same list for the stat objects 
    Invoke-NSCustomQuery -Address CTX-NS-TST-01 -QueryType stat -list -Credential $cred 
#Pull all lbvservers, servers, services, servicegroups from ctx-ns-tst-01 
    Invoke-NSCustomQuery -Address "CTX-NS-TST-01" -ResourceType "lbvserver" -Credential $cred 
    Invoke-NSCustomQuery -Address "CTX-NS-TST-01" -ResourceType "server" -Credential $cred 
    Invoke-NSCustomQuery -Address "CTX-NS-TST-01" -ResourceType "service" -Credential $cred 
    Invoke-NSCustomQuery -Address "CTX-NS-TST-01" -ResourceType "servicegroup" -Credential $cred 
#Return details on lbvserver with name SomeLBVServer from CTX-NS-TST-01 
    Invoke-NSCustomQuery -Address "CTX-NS-TST-01" -ResourceType "lbvserver" -ResourceName "SomeLBVServer" 
#Return details on lbvservers from CTX-NS-TST-01 where IPV46 is "" 
    Invoke-NSCustomQuery -Address "CTX-NS-TST-01" -ResourceType "lbvserver" -FilterTable @{ipv46=""} 
#Pull lbvserver_bindings for SomeValidVServerName.  Invoke-NSCustomQuery provides the data, but does not help parsing the results or validating vserver: 
    Invoke-NSCustomQuery -Address "CTX-NS-TST-01" -ResourceType "lbvserver_binding" -Argument "SomeValidVServerName" -Credential $cred 
#Return all enabled servers on CTX-NS-TST-01 
    Invoke-NSCustomQuery -Address "CTX-NS-TST-01" -ResourceType "server" -FilterTable @{state="ENABLED"} 
#Return all disabled servers on CTX-NS-TST-01 
    Invoke-NSCustomQuery -Address "CTX-NS-TST-01" -ResourceType "server" -FilterTable @{state="DISABLED"} 
#This example illustrates how to disable a server.  Note that this does not save changes! 
    #Build the JSON for a server you want to disable 
$json = @" 
    "server": { 
    #Create a session on CTX-NS-TST-01 
    #This function is included in the GitHub Module:    #https://github.com/RamblingCookieMonster/Citrix.NetScaler 
    $session = Get-NSSessionCookie -Address ctx-ns-tst-01 -AllowHTTPAuth 
    #use that session to disable a server 
    Invoke-NSCustomQuery -Address "CTX-NS-TST-01" -ResourceType "server" -method Post -Body $json -ContentType application/vnd.com.citrix.netscaler.server+json -AllowHTTPAuth -action disable -verbose -WebSession $session 
    #Note that an error will be returned indicating null output.  Not sure how else to handle this, as null output is usually bad.  Will work on it... 
    #verify the change: 
    Invoke-NSCustomQuery -Address CTX-NS-TST-01 -ResourceType server -ResourceName SomeServerName -Credential $cred -AllowHTTPAuth



The binding data is buried in the object.  You need to expand these properties to get at the data.  Here's the same query shown another way:


I couldn't find much out there on using PowerShell to work with a NetScaler.  There's an outdate project on CodePlex, and some examples that introduce a .NET dependency.  I chose to go with the REST API Citrix provides.  A few links for further reading:

My apologies for the messy code.  Haven't had the time to straighten things out or follow best practices.