david62277

First NS Script

Aug 28th, 2015
215
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Written by David Ott
  3. This script requires the Nitro API for C#.  This is my first powershell script messing with the netscaler.  It will report
  4. all netscalers in HA with status - up or down (if you have HA enabled), all vservers up/down/degraded (if degraded will tell you
  5. which service is down).  All netscaler gateways - up/down, and all ica connections (also whether or not they are using the new
  6. Framehawk protocol - need NS 11.0 62.10 or above)
  7. credit to Ajené Hall Barrett (http://blogs.citrix.com/2014/05/29/powershell-nitro-api-and-html-load-balancing-virtual-servers/)
  8. search for ##### for fields you will want to edit
  9. #>
  10.  
  11. function check-downvserver($v){
  12. if ($v.totalservices -eq "0" -and ($v.redirurl).length -ne "0") {
  13. $r = "0"
  14. } else {
  15. $r = "1"
  16. }
  17. return $r
  18. }
  19. function check-upvserver($v) {
  20. if ($v.totalservices -eq $v.activeservices) {
  21. $r = "0"
  22. } else {
  23. $r = "1"
  24. }
  25. return $r
  26. }
  27. function check-gw($v) {
  28. if ($v.curstate -eq "UP") {
  29. $r = "0"
  30. } else {
  31. $r = "1"
  32. }
  33. return $r
  34. }
  35. <##### You can use your nsip instead of a snip with gui management enabled, BUT if that nsip happens to be a netscaler
  36. that is down the whole script will fail.  If you use a snip with gui management enabled it will hit any ha netscaler that is up. #####>
  37. $snip = "internal snip with gui management enabled"
  38. $user = "nsroot"
  39. $pass = "nsroot" ##### nsroot password (nsroot = default)
  40. <##### You need the C# nitro api (download from your netscaler), extract it all, and change $path1 and $path2 below to match your system #####>
  41. $path1 = "C:\ns_nitro-csharp_ion_62_10\lib\Newtonsoft.Json.dll"
  42. $path2 = "C:\ns_nitro-csharp_ion_62_10\lib\nitro.dll"
  43. $O = [System.Reflection.Assembly]::LoadFile($path1)
  44. $O = [System.Reflection.Assembly]::LoadFile($path2)
  45. $nitrosession = new-object com.citrix.netscaler.nitro.service.nitro_service($snip,"http")
  46. $session = $nitrosession.login($user,$pass)
  47. $HA = [com.citrix.netscaler.nitro.resource.config.ha.hanode]::get($nitrosession)
  48. $AllLBvServers = [com.citrix.netscaler.nitro.resource.config.lb.lbvserver]::get($nitrosession)
  49. $Gateways = [com.citrix.netscaler.nitro.resource.config.vpn.vpnvserver]::get($nitrosession)
  50. if (($HA | Measure-Object).Count -gt "1") {
  51. Write-Host "Netscaler HA" -ForegroundColor Gray
  52. foreach ($h in $HA) {
  53. if ($h.hastatus -ne "UP") {
  54. Write-Host "Netscaler"$h.ipaddress "DOWN" -ForegroundColor Red
  55. } else {
  56. Write-Host "Netscaler"$h.ipaddress "UP" -ForegroundColor Green
  57. }
  58. }
  59. Write-Host "`r`n"
  60. }
  61. if (($AllLBvServers | Measure-Object).count -gt "0") {
  62. Write-Host "Load Balancing Virtual Servers" -ForegroundColor Gray
  63. foreach ($lbvserver in $AllLBvServers) {
  64. if ($lbvserver.effectivestate -eq "down") {
  65. $dchk = check-downvserver -v $lbvserver
  66. if ($dchk -eq "0") {
  67. write-host $lbvserver.name $lbvserver.ipv46 "OK" -ForegroundColor Green
  68. } else {
  69. Write-Host $lbvserver.name $lbvserver.ipv46 "DOWN" -ForegroundColor Red
  70. }
  71. } else {
  72. $uchk = check-upvserver -v $lbvserver
  73. if ($uchk -eq "0") {
  74. Write-Host $lbvserver.name $lbvserver.ipv46 "OK" -ForegroundColor Green
  75. } else {
  76. Write-Host $lbvserver.name $lbvserver.ipv46 "DEGRADED" -ForegroundColor Yellow
  77. [com.citrix.netscaler.nitro.resource.config.lb.lbvserver_service_binding]::get($nitrosession,$lbvserver.name) | %{
  78. if ($_.curstate -ne "UP") {
  79. Write-Host "     "$_.servicename $_.vsvrbindsvcip $_.curstate -ForegroundColor DarkRed
  80. }
  81. }
  82. }
  83. }
  84. }
  85. Write-Host "`r`n"
  86. }
  87. if (($Gateways | Measure-Object).count -gt "0") {
  88. Write-Host "Netscaler Gateways" -ForegroundColor Gray
  89. foreach ($gw in $gateways) {
  90. $gchk = check-gw -v $gw
  91. if ($gchk -eq "0") {
  92. Write-Host $gw.name $gw.ipv46 "OK" -ForegroundColor Green
  93. } else {
  94. Write-Host $gw.name $gw.ipv46 $gw.curstate -ForegroundColor Red
  95. }
  96. }
  97. Write-Host "`r`n"
  98. }
  99.  
  100. if (([com.citrix.netscaler.nitro.resource.config.vpn.vpnicaconnection]::get($nitrosession) | Measure-Object).count -gt "0") {
  101. Write-Host "Current XD/XA Connections" -f Gray
  102. [com.citrix.netscaler.nitro.resource.config.vpn.vpnicaconnection]::get($nitrosession) | %{
  103. $n = $null
  104. $n = $_.username
  105. if ([com.citrix.netscaler.nitro.resource.config.vpn.vpnicadtlsconnection]::get($nitrosession) | ?{$_.username -eq $n}) {
  106. Write-Host $_.username "Framehawk:True"
  107. } else {
  108. Write-Host $_.username "Framehawk:False"
  109. }
  110. }
  111. }
  112. $nitrosession.logout() | Out-Null
RAW Paste Data