Netscaler Backup

######################################################################################################

#       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!"}}

Source: https://blog.trendelkamp.net/2015/01/netscaler-backup-using-powershell/