Skip to content

Instantly share code, notes, and snippets.


hospitableit/PRTGNetScaler.ps1 Secret

Last active Nov 17, 2018
What would you like to do?
$ReadOnlyUser = "Read Only Username"
$EncryptedPassword = "Encrypted Password"
#Decrypt the password for use in the script
$SecurePassword = $EncryptedPassword | ConvertTo-SecureString
$Marshal = [System.Runtime.InteropServices.Marshal]
$Bstr = $Marshal::SecureStringToBSTR($SecurePassword)
$Password = $Marshal::PtrToStringAuto($Bstr)
# Disable SSL certificate checking so we can connect to servers using a cert signed by a private CA and enable TLS1.2
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#Grab the Host IP/FQDN from the Device Config in PRTG
$TargetHost = $env:prtg_host
#Send our request and convert the result into a JSON object
$NetScalerQuery = @{uri = "https://$TargetHost/nitro/v1/config/lbvserver";
Method = 'GET';
Headers = @{'X-NITRO-USER' = '$ReadOnlyUser'; 'X-NITRO-PASS' = '$Password';}
$NetScalerQueryResult = Invoke-WebRequest @NetScalerQuery -UseBasicParsing
$NetScalerQueryJSON = ConvertFrom-Json -InputObject $NetScalerQueryResult
#Count the number of Virtual Servers returned
$LBServerTotal = $NetScalerQueryJSON.lbvserver.count
$MessageText = "OK"
Write-Host "<prtg>"
#Loop through the Virtual Servers, evaluate their state and output for PRTG
For ($LBServerCount=0; $LBServerCount -lt $LBServerTotal; $LBServerCount++){
$ChannelName = $NetScalerQueryJSON.lbvserver[$LBServerCount].name
$CurrentState = $NetScalerQueryJSON.lbvserver[$LBServerCount].curstate
$EffectiveState = $NetScalerQueryJSON.lbvserver[$LBServerCount].effectivestate
$Health = $NetScalerQueryJSON.lbvserver[$LBServerCount].health
If ($CurrentState -eq "DOWN" -or $EffectiveState -eq "DOWN" -or $Health -ne "100") {
$StateValue = 1
If ($MessageText -eq "OK") {
$MessageText = $ChannelName
Else {
$MessageText = $MessageText + " " + $ChannelName
Else {
$StateValue = 0
Write-Host "<result>"
Write-Host "<Text>$MessageText</Text>"
Write-Host "</prtg>"
#If try code block fails then throw an error to PRTG
Write-Host "<prtg>"
Write-host "<error>1</error>"
Write-Host "<text>Sensor Error</text>"
Write-Host "</prtg>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.