Powershell & Netscaler API

Saving a Netscaler configuration via. Powershell & API

I’ve been learning how to use Powershell to work with APIs, so I decided to write a quick script that will query our two Netscaler devices for the running configuration, and dump it. This is a quick way to backup the configuration regularly, because I often forget to grab a copy for backup purposes (unless I’m performing extensive work, or upgrading the firmware).

#These credentials are encrypted by ServiceAccount on Server
$credFilePath = '\\SERVER\PATH\creds.xml'
$netscalerId = (Import-Clixml $credFilePath).UserName
$netscalerPwd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR((Import-Clixml $credFilePath).password))

$formatedDate = Get-Date -Format 'yyyyMMdd'
$FilePath = "\\SERVER\PATH"
$headers = @{'X-NITRO-USER'='nsroot'; 'X-NITRO-PASS'='DFt$LG6%6L3F'}

#grab Netscaler01
$uri = "https://netscaler01.company.com/nitro/v1/config/nsrunningconfig"
$result = (Invoke-RestMethod -Method Get -Uri $uri -Headers $headers).nsrunningconfig.response
($result | Convertto-Json).split('\n') | Out-File "$FilePath\Netscaler01_${formatedDate}.txt"

#grab Netscaler03
$uri = "https://netscaler03.company.com/nitro/v1/config/nsrunningconfig"
$result = (Invoke-RestMethod -Method Get -Uri $uri -ContentType application/json -Headers $headers).nsrunningconfig.response
($result | ConvertTo-Json).split('\n') | Out-File "$FilePath\Netscaler03_${formatedDate}.txt"

Greg

Built with Hugo
Theme Stack designed by Jimmy