Skip to content

Instantly share code, notes, and snippets.

@joshooaj
Last active November 8, 2023 19:21
Show Gist options
  • Save joshooaj/7ef3fb3ed4f816ed5aa0a8a78085b067 to your computer and use it in GitHub Desktop.
Save joshooaj/7ef3fb3ed4f816ed5aa0a8a78085b067 to your computer and use it in GitHub Desktop.
Use Pester to perform a basic network availability test on whatever app/service you want.

Service Availability Test

Introduction

This is a basic test harness using Pester with a JSON configuration file to describe and test the accessibility of any number of TCP ports on any number of host machines.

Usage

Edit services.json to describe the services you want to test by creating a named object record under Services, with a DisplayName string property, and a Ports array property with one or more TCP ports expressed as an integer:

    "Services": {
        "WebServer": {
            "DisplayName": "Web Server (HTTP/S)",
            "Ports": [80,443]
        },
        "SSH": {
            "DisplayName": "SSH Server",
            "Ports": [22]
        }
    }

With your services defined, add one or more objects to the Hosts array with a Host string property describing the computer hostname, fully-qualified domain name, or IP address, and a Services array of strings representing the services described in the JSON file:

    "Hosts": [
        {
            "Host": "www.milestonesys.com",
            "Services": [
                "WebServer",
                "SSH",
                "RDP",
                "DNS"
            ]
        }
    ]
BeforeDiscovery {
$script:config = Get-Content -Path $PSScriptRoot/services.json -Raw -ErrorAction Stop | ConvertFrom-Json
}
Describe "<_.Host>" -ForEach $config.Hosts {
BeforeDiscovery {
$script:services = $_.Services
$script:computerName = $_.Host
}
Context "<_>" -Foreach $services {
BeforeDiscovery {
$serviceName = $_
$script:testCases = $script:config.Services.$_.Ports | ForEach-Object {
@{
ComputerName = $script:computerName
Port = $_
ServiceName = $serviceName
}
}
}
It "TCP port <port> is open" -TestCases $testCases {
$result = Test-NetConnection -ComputerName $ComputerName -Port $Port -InformationLevel Detailed
$result.TcpTestSucceeded | Should -BeTrue -Because "$ServiceName on $ComputerName should listen on TCP port $Port"
}
}
}
{
"Services": {
"WebServer": {
"DisplayName": "Web Server (HTTP/S)",
"Ports": [
80,
443
]
},
"EventServer": {
"DisplayName": "Event Server",
"Ports": [
22331,
22333
]
},
"LogServer": {
"DisplayName": "Log Server",
"Ports": [
22337
]
},
"DataCollector": {
"DisplayName": "DataCollector",
"Ports": [
7609
]
},
"RecordingServer": {
"DisplayName": "Recording Server",
"Ports": [
7563,
9001,
11000,
5210
]
},
"LprServer": {
"DisplayName": "LPR Server",
"Ports": [
22334
]
},
"SSH": {
"DisplayName": "SSH Server",
"Ports": [
22
]
},
"RDP": {
"DisplayName": "Terminal Services",
"Ports": [
3389
]
},
"DNS": {
"DisplayName": "DNS",
"Ports": [
53
]
}
},
"Hosts": [
{
"Host": "www.milestonesys.com",
"Services": [
"WebServer",
"RecordingServer"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment