Working with JSON in Powershell

Converting a Powershell object to JSON when making a Netscaler API call

I’ve recently been editing a Powershell script that manages our Let’s Encrypt certificates and their integration with Netscaler. I’ve been using John Billeken’s excellent GenLeCertForNS script to do the heavy lifting, but I’ve written a front-end wrapper for it to handle the way I need it to work in my environment. [I started using this with v.2.63, which didn’t have email capabilities. My front-end wrapper adds email reporting, plus obfuscuating any credentials in the log, and loading the credentials from an encypted XML file]. I was rewriting my wrapper to include SAN support and to update two Netscaler appliances with the same certificate: if the CN matched certain values, the script will find the same Let’s Encrypt certificate .pfx, upload it to the Netscaler at our warm Disaster Recovery site and properly bind it.

To do that, I needed to make a number of API calls to Netscaler, which I generally discuss in my article Powershell & Netscaler API. This time, I needed to pass specific parameters in JSON format in order to link and unlink the certificate from others in the chain.

According to the Citrix ADC NITRO API Reference, I need: URL: http://<Citrix-ADC-IP-address(NSIP)>/nitro/v1/config/sslcertkey?action=link
HTTP Method: POST
Request Headers: Cookie:NITRO_AUTH_TOKEN= Content-Type:application/json
Request Payload:

{
    "sslcertkey":
  {
    "certkey":<String_value>,
    "linkcertkeyname":<String_value>
  }
}

This is a JSON format. While I could just use a straight string (ie. $string="{"sslcertkey":{"linkcertkeyname":"Sectigo RSA CA 2018","certkey":"sts.agricorp.com"}}")) or a multiline string (ie. $string="@TEXT@"), I figured that I should take the effort to learn a bit about taking an object and converting it to JSON.

My initial attempts to get this working were all wrong. I finally managed to get the key/value pairs properly written to a hashtable, and then converted it to JSON.

$array = @{"certkey" = "$CertificateCertKeyName"; "linkcertkeyname" = "$IntermediateCACertKeyName" }
$hashtablePayload = @{"sslcertkey" = $array }
$jsonPayload = ConvertTo-Json -InputObject $hashtablePayload -Depth 100 -Compress

The output of $jsonPayload looks like this:
{"sslcertkey":{"linkcertkeyname":"Sectigo RSA CA 2018","certkey":"sts.agricorp.com"}}
…perfectly formatted JSON that I can pass through to the Netscaler:

$URI = "https://netscaler03.corp.company.com/nitro/v1/config"
$headers = @{'X-NITRO-USER' = "$netscalerID"; 'X-NITRO-PASS' = "$netscalerPwd" }
...
$hashtablePayload = @{ }
$hashtablePayload = @{"sslcertkey" = @{"certkey" = "$CertificateCertKeyName"; "linkcertkeyname" = "$IntermediateCACertKeyName" } }
$jsonPayload = ConvertTo-Json -InputObject $hashtablePayload -Depth 100 -Compress
$response = (Invoke-RestMethod -Method POST -Uri "$uri`/sslcertkey?action=link" -ContentType application/json -Headers $headers -Body $JsonPayload)
Built with Hugo
Theme Stack designed by Jimmy