Advertisement
easternnl

Citrix Check Script (draft)

Aug 7th, 2015
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Test-Webrequest
  2. {
  3.  
  4. param(
  5.         [Parameter(ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true)]
  6.         [string]$url,
  7.         [long]$expectedresult
  8.     )
  9.  
  10. # Accept all certificates including unsigned and wrong commonnames (for testing purpose)
  11. add-type @"
  12.    using System.Net;
  13.    using System.Security.Cryptography.X509Certificates;
  14.    public class TrustAllCertsPolicy : ICertificatePolicy {
  15.        public bool CheckValidationResult(
  16.            ServicePoint srvPoint, X509Certificate certificate,
  17.            WebRequest request, int certificateProblem) {
  18.            return true;
  19.        }
  20.    }
  21. "@
  22. [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
  23.  
  24. try {
  25.     $response = Invoke-WebRequest $url -UseBasicParsing -TimeoutSec .5 -UseDefaultCredentials
  26.  
  27.     if ($response.StatusDescription,$response.StatusCode -eq $expectedresult)
  28.     {
  29.         # expected result
  30.         log "$url - ($($response.StatusDescription,$response.StatusCode)) " -level "INFO"
  31.     }
  32.     else
  33.     {
  34.         # unexpected, but still a result
  35.         log "$url - ($($response.StatusDescription,$response.StatusCode)) " -level "ERROR"
  36.     }    
  37. }
  38. catch
  39. {
  40.     # unexpected, error handling procedure
  41.     log "$url - ERROR: ($($_.Exception.Response.StatusCode.Value__)) " -level "ERROR"  
  42. }
  43.  
  44. }
  45.  
  46.  
  47.  
  48. Function Test-Services
  49. {
  50.  
  51. param(
  52.         [Parameter(ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true)]
  53.         [string]$computername,
  54.         [string]$servicelist,
  55.         [string]$expectedresult
  56.     )
  57.  
  58.     $services_mandatory = $servicelist.Split(",")
  59.  
  60.     # Old and slow: $services = Get-Service -ComputerName $computername
  61.     $services = gwmi win32_service -ComputerName $computername
  62.  
  63.     foreach ($service_mandatory in $services_mandatory)
  64.     {
  65.         if ($services | where { $_.displayname -ieq $service_mandatory } | where { $_.State -ieq $expectedresult} )
  66.         {
  67.             # service running
  68.             log "$computername $service_mandatory is running" -level "INFO"
  69.         }
  70.         else
  71.         {
  72.             # service other status
  73.             $state = ($services | where { $_.displayname -ieq $service_mandatory }).state
  74.             log "$computername $service_mandatory is $state " -level "ERROR"          
  75.            
  76.         }
  77.     }
  78.    
  79.  
  80. }
  81.  
  82. function Test-Port
  83. {
  84.     Param(
  85.         [parameter(ParameterSetName='ComputerName', Position=0)]
  86.         [string]
  87.         $ComputerName,
  88.  
  89.         [parameter(ParameterSetName='IP', Position=0)]
  90.         [System.Net.IPAddress]
  91.         $IPAddress,
  92.  
  93.         [parameter(Mandatory=$true , Position=1)]
  94.         [int]
  95.         $Port,
  96.  
  97.         [parameter(Mandatory=$true, Position=2)]
  98.         [ValidateSet("TCP", "UDP")]
  99.         [string]
  100.         $Protocol
  101.         )
  102.  
  103.     $RemoteServer = If ([string]::IsNullOrEmpty($ComputerName)) {$IPAddress} Else {$ComputerName};
  104.  
  105.     If ($Protocol -eq 'TCP')
  106.     {
  107.         $test = New-Object System.Net.Sockets.TcpClient;
  108.         Try
  109.         {            
  110.             $test.Connect($RemoteServer, $Port);
  111.             Log -logtext "$($RemoteServer):$Port Connection successful" -level "INFO"            
  112.             return $true;          
  113.         }
  114.         Catch
  115.         {
  116.             Log -logtext "$($RemoteServer):$Port Connection failed" -level "ERROR"          
  117.             return $false;
  118.         }
  119.         Finally
  120.         {
  121.             $test.Dispose();
  122.         }
  123.     }
  124.  
  125.     If ($Protocol -eq 'UDP')
  126.     {
  127.         Write-Host "UDP port test functionality currently not available."
  128.        
  129.         #$test = New-Object System.Net.Sockets.UdpClient;
  130.         #Try
  131.         #{
  132.             #Write-Host "Connecting to "$RemoteServer":"$Port" (UDP)..";
  133.             #$test.Connect($RemoteServer, $Port);
  134.             #Write-Host "Connection successful";
  135.         #}
  136.         #Catch
  137.         #{
  138.             #Write-Host "Connection failed";
  139.         #}
  140.         #Finally
  141.         #{
  142.             #$test.Dispose();
  143.         #}
  144.        
  145.     }
  146. }
  147.  
  148. Function Log
  149. {
  150.     Param($logtext, $level)
  151.    
  152.     $logline = Get-Date -format "yyyy-MM-dd HH:mm:ss "
  153.     $logline += "[$level] "
  154.     $logline += $logtext
  155.  
  156.     If ($level -eq "INFO")
  157.     {
  158.         # the message send is a INFO message
  159.         If ($reporting -eq "INFO")
  160.         {
  161.             Write-Host $logline
  162.         }
  163.     }
  164.     Elseif ($level -eq "ERROR")
  165.     {
  166.         Write-Host $logline
  167.     }
  168.  
  169. }
  170.  
  171. Function Test
  172. {
  173.     Write-Host $reporting
  174. }
  175.  
  176. # Storefront servers
  177.  
  178. # Report all information (INFO) or report only ERRORs (ERROR)
  179. $reporting="INFO"
  180. #$reporting="ERROR"
  181.  
  182. foreach ($server in $servers_storefront)
  183. {
  184.     # test if server online
  185.    
  186.     If (Test-Port -ComputerName $server -Port 3389 -Protocol TCP)
  187.     {
  188.         # server online        
  189.         Test-Services -computername $server -servicelist "World Wide Web Publishing Service,Citrix Configuration Replication,Citrix Credential Wallet,Citrix Default Domain Services,Citrix Peer Resolution Service,Citrix Subscriptions Store" -expectedresult "RUNNING"
  190.         Test-Webrequest -url "https://$server/" -expectedresult 200
  191.        
  192.     }
  193.     Else
  194.     {
  195.         # server offline        
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement