Skip to content

Instantly share code, notes, and snippets.

@jmelosegui
Created March 5, 2018 17:45
Show Gist options
  • Save jmelosegui/62a6122f9bfc8112f1465890be1c5835 to your computer and use it in GitHub Desktop.
Save jmelosegui/62a6122f9bfc8112f1465890be1c5835 to your computer and use it in GitHub Desktop.
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[ValidateScript({Test-Path $_ -PathType Leaf})]
[string]$ConfigurationDataFile
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "SilentlyContinue"
function Get-ConfigurationDataAsObject
{
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[Microsoft.PowerShell.DesiredStateConfiguration.ArgumentToConfigurationDataTransformation()]
[hashtable] $ConfigurationData
)
return $ConfigurationData
}
function Test-NetworkConnection
{
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string] $ServerName,
[Parameter(Mandatory)]
[int] $Port
)
$Socket = New-Object System.Net.Sockets.TcpClient($ServerName, $Port)
If ($Socket)
{ $Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.AsciiEncoding
#Save all the results
While($Stream.Available)
{
$Read = $Stream.Read($Buffer, 0, 1024)
$Result += ($Encoding.GetString($Buffer, 0, $Read))
}
Write-output "Connection made successfully $($ServerName):$Port"
}
else
{
Write-Warning "Unable to connect to host: $($ServerName):$Port"
}
Write-output " "
}
Clear-Host
$ConfigurationData = Get-ConfigurationDataAsObject $ConfigurationDataFile
$ConfigurationData.AllNodes | ForEach-Object {
$server = $_.NodeName
write-output " "
write-output "*********************************************"
write-output "Testing connection from Localhost to..."
write-output $server
write-output "*********************************************"
$_.Ports | ForEach-Object {
Test-NetworkConnection -ServerName $server -Port $_
}
}
@jmelosegui
Copy link
Author

Use a file similar to the following to test network connectivity.

@{
    AllNodes = @(
        @{
            NodeName = 'Host1'
            Ports = @(80, 443, 445, 5987)
        },        
        @{
            NodeName = 'Host1'
            Ports = @(445, 5985)
        }        
    )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment