NetScaler Backup by using Powershell

NetScaler Backup by using Powershell

For quite some time it is possible to interact with┬áa NetScaler by using the NITRO API. Since i got some a spare time i took a closer look at this and thought this could be cool way to backup my NetScaler config regularly. On a side note: I do not like running bash scripts started by crontab on my Appliance ­čśë As a result of this I created a little Powershell script which does the following: At first the script connects to the Appliance and creates a session for the following tasks. The script saves the current NetScaler configuration and creates a new full system backup. After that we have to use a third party tool to copy the file from the NetScaler to a desired location in the network. ┬áSadly it is currently not possible to transfer files from or to the NetScaler by using the NITRO API except following file formats: .txt, .cert, .req, .xml, .key.┬á

NetScaler Backup by using Powershell

In the end the script deletes the created system backup file (the NetScaler can only store up to 50 backups at the same time) disconnects the session and sends a mail.

If want to use SSL but haven┬┤t had time yet to configure your certificates right you can disable the SSL check.

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Citrix also published a nice blog about configuring the NetScaler by using Powershell which can be found here. You can find the NITRO API  documentation in the Citrix eDocs

 

######################################################################################################
#		Purpose:
#				Automatically create and save a NetScaler Backup
#		Scriptname:
#				ns-backup.ps1
#		$ScriptVersion =        "0.1"
#		Prerequisites:
#				- Powershell 3.0
#               - pscp.exe http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
#		Author:
#				Jens Trendelkamp, Jens.Trendelkamp@sepago.de, sepago GmbH
#		Change history:
#				06.01.2015, Jens Trendelkamp, Jens.Trendelkamp@sepago.de
#					- Initial version
#######################################################################################################
# $nsip = NetScaler NS IP Address
# $nsprotocol = HTTP or HTTPS 
# $nsuser = User 
# $nspass = Password
# $filename = How the backup file will be named
# $level = full or basic | http://support.citrix.com/proddocs/topic/ns-system-10-5-map/ns-system-backup1-tsk.html
# $pathtopscp = Where to find pscp.exe 
# $savelocation = Where to save the backup 
# $psemailserver = SMTP Address in case a mail should be send. If not leave this field empty
# $mailto = Mail Recipient
#######################################################################################################
$nsip = "10.0.0.0"
$nsprotocol = "https://"
$nsuser = "nsroot"
$nspass = "nsroot"
$filename = "ns-backup-$(get-date -uformat "%d-%m-%Y-%H-%M")"
$level = "full"
$pathtopscp = "C:\temp"
$savelocation = "\\10.0.0.2\Backups\NetScaler"
$psemailserver = "10.0.0.3"
$mailto = "jens@trendelkamp.net"

$payload = @{"login" = @{"username"=$nsuser;"password"=$nspass;"timeout"=60}}
$payloadjson = ConvertTo-Json $payload
Invoke-RestMethod -Uri "$nsprotocol$nsip/nitro/v1/config/login" -Body $payloadjson -Method POST -SessionVariable saveSession -ContentType application/json | out-null 
$nssession = New-Object -TypeName PSObject
$nssession | Add-Member -NotePropertyName WebSession  -NotePropertyValue $saveSession -TypeName Microsoft.PowerShell.Commands.WebRequestSession
$payload = @{"nsconfig"=@{}}
$payloadjson = ConvertTo-Json $payload
Invoke-RestMethod -Uri "$nsprotocol$nsip/nitro/v1/config/nsconfig?action=save" -Body $payloadjson -Method POST -ContentType application/json -WebSession $nssession.WebSession | out-null 
$payload = @{"systembackup"=@{"level"="$level";"filename"="$filename"}}
$payloadjson = ConvertTo-Json $payload
Invoke-RestMethod -Uri "$nsprotocol$nsip/nitro/v1/config/systembackup?action=create" -Body $payloadjson -Method POST -ContentType application/json -WebSession $nssession.WebSession | out-null 
& $pathtopscp\pscp.exe -pw $NSPASS $nsuser@"$nsip":/var/ns_sys_backup/$filename.tgz $savelocation | out-null 
$payload = @{"systembackup"=@{"filename"="$filename.tgz"}}
$payloadjson = ConvertTo-Json $payload
Invoke-RestMethod -Uri "$nsprotocol$nsip/nitro/v1/config/systembackup?action=rm" -Body $payloadjson -Method POST -ContentType application/json -WebSession $nssession.WebSession | out-null 
$payload = @{"logout"=@{}}
$payloadjson = ConvertTo-Json $payload
Invoke-RestMethod -Uri "$nsprotocol$nsip/nitro/v1/config/logout" -Body $payloadjson -Method POST -ContentType application/json -WebSession $nssession.WebSession | out-null 
$pathnfile = $savelocation + "\" + $filename + ".tgz"
if ($psemailserver) {
if (test-path $pathnfile){
Send-MailMessage -to "$mailto" -from "NetScaler Backup <nsbackup@trendelkamp.net>" -Subject "NetScaler Backup successfull" -body "The scheduled NetScaler Backup has successfully finished and can be found here : $pathnfile"}
else {
Send-MailMessage -to "$mailto" -from "NetScaler Backup <nsbackup@trendelkamp.net>" -Subject "NetScaler Backup failed" -body "The NetScaler Backup has failed!"}}

5 Responses to “NetScaler Backup by using Powershell”

  1. Carlo says:

    I’m very surprised no one left any comments in the year and half this has been up.
    I’ve only started learning about Netscaler and such and was trying to research into other methods of backing it up other than the GUI method. I’m glad I came upon your script. Although I can get it to run fine either from powershell, powerhshell ise, or even called from a batch file, if I try to launch it from task scheduler as an automated task, it just gets stuck in RUNNING and never ends unless I cancel it. I can’t figure out why this is.

    I’ve tried running it from the domain administrator account (which has the fingerprint for the Netscaler recorded through Putty. I have tried as a system account.

    pscp.exe and the powershell script are located under c:\pscp folder

    My application/script field for the task is as such:
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Add arguments (optional)
    -noprofile -executionpolicy Unrestricted -file “C:\pscp\ns-backup.ps1”

    start in (optional):
    C:\pscp

  2. Carsten says:

    Hi,

    the Nitro API Call for deleting the created backup file (/nitro/v1/config/systembackup?action=rm) will not work on passive HA NetScalers….
    Have you got any idea?
    The backup will fail, if the number of files is more than 50.

  3. Pam Grier & Aretha Franklin can't be serious!There are so many good actresses waiting for their "big break" that may never come, because Hollywood/people love to recycle the same old names & faces.

  4. Marylouise says:

    What a plauesre to find someone who thinks through the issues

  5. Jaylan says:

    Grade A stuff. I’m unsuaqtionebly in your debt.

Leave a Reply

Your email address will not be published.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close