Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Test-Webrequest
- {
- param(
- [Parameter(ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true)]
- [string]$url,
- [long]$expectedresult
- )
- # Accept all certificates including unsigned and wrong commonnames (for testing purpose)
- add-type @"
- using System.Net;
- using System.Security.Cryptography.X509Certificates;
- public class TrustAllCertsPolicy : ICertificatePolicy {
- public bool CheckValidationResult(
- ServicePoint srvPoint, X509Certificate certificate,
- WebRequest request, int certificateProblem) {
- return true;
- }
- }
- "@
- [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
- try {
- $response = Invoke-WebRequest $url -UseBasicParsing -TimeoutSec .5 -UseDefaultCredentials
- if ($response.StatusDescription,$response.StatusCode -eq $expectedresult)
- {
- # expected result
- log "$url - ($($response.StatusDescription,$response.StatusCode)) " -level "INFO"
- }
- else
- {
- # unexpected, but still a result
- log "$url - ($($response.StatusDescription,$response.StatusCode)) " -level "ERROR"
- }
- }
- catch
- {
- # unexpected, error handling procedure
- log "$url - ERROR: ($($_.Exception.Response.StatusCode.Value__)) " -level "ERROR"
- }
- }
- Function Test-Services
- {
- param(
- [Parameter(ValueFromPipeline = $true,ValueFromPipelineByPropertyName = $true)]
- [string]$computername,
- [string]$servicelist,
- [string]$expectedresult
- )
- $services_mandatory = $servicelist.Split(",")
- # Old and slow: $services = Get-Service -ComputerName $computername
- $services = gwmi win32_service -ComputerName $computername
- foreach ($service_mandatory in $services_mandatory)
- {
- if ($services | where { $_.displayname -ieq $service_mandatory } | where { $_.State -ieq $expectedresult} )
- {
- # service running
- log "$computername $service_mandatory is running" -level "INFO"
- }
- else
- {
- # service other status
- $state = ($services | where { $_.displayname -ieq $service_mandatory }).state
- log "$computername $service_mandatory is $state " -level "ERROR"
- }
- }
- }
- function Test-Port
- {
- Param(
- [parameter(ParameterSetName='ComputerName', Position=0)]
- [string]
- $ComputerName,
- [parameter(ParameterSetName='IP', Position=0)]
- [System.Net.IPAddress]
- $IPAddress,
- [parameter(Mandatory=$true , Position=1)]
- [int]
- $Port,
- [parameter(Mandatory=$true, Position=2)]
- [ValidateSet("TCP", "UDP")]
- [string]
- $Protocol
- )
- $RemoteServer = If ([string]::IsNullOrEmpty($ComputerName)) {$IPAddress} Else {$ComputerName};
- If ($Protocol -eq 'TCP')
- {
- $test = New-Object System.Net.Sockets.TcpClient;
- Try
- {
- $test.Connect($RemoteServer, $Port);
- Log -logtext "$($RemoteServer):$Port Connection successful" -level "INFO"
- return $true;
- }
- Catch
- {
- Log -logtext "$($RemoteServer):$Port Connection failed" -level "ERROR"
- return $false;
- }
- Finally
- {
- $test.Dispose();
- }
- }
- If ($Protocol -eq 'UDP')
- {
- Write-Host "UDP port test functionality currently not available."
- #$test = New-Object System.Net.Sockets.UdpClient;
- #Try
- #{
- #Write-Host "Connecting to "$RemoteServer":"$Port" (UDP)..";
- #$test.Connect($RemoteServer, $Port);
- #Write-Host "Connection successful";
- #}
- #Catch
- #{
- #Write-Host "Connection failed";
- #}
- #Finally
- #{
- #$test.Dispose();
- #}
- }
- }
- Function Log
- {
- Param($logtext, $level)
- $logline = Get-Date -format "yyyy-MM-dd HH:mm:ss "
- $logline += "[$level] "
- $logline += $logtext
- If ($level -eq "INFO")
- {
- # the message send is a INFO message
- If ($reporting -eq "INFO")
- {
- Write-Host $logline
- }
- }
- Elseif ($level -eq "ERROR")
- {
- Write-Host $logline
- }
- }
- Function Test
- {
- Write-Host $reporting
- }
- # Storefront servers
- # Report all information (INFO) or report only ERRORs (ERROR)
- $reporting="INFO"
- #$reporting="ERROR"
- foreach ($server in $servers_storefront)
- {
- # test if server online
- If (Test-Port -ComputerName $server -Port 3389 -Protocol TCP)
- {
- # server online
- 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"
- Test-Webrequest -url "https://$server/" -expectedresult 200
- }
- Else
- {
- # server offline
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement