Created
June 22, 2021 12:46
-
-
Save clarkey/b64e482cc16c78719e99211e0ebe8b5f to your computer and use it in GitHub Desktop.
PowerShell CSV Test-NetConnection Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FQDN | TCPPort | |
---|---|---|
server1.example.corp | 22 | |
server2.example.corp | 3389 | |
server3.example.corp | 21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Targets = Import-Csv $PSScriptRoot\targets.csv | |
if (!(Test-Path "$PSScriptRoot\logs\")) { | |
New-Item "$PSScriptRoot\logs\" -ItemType directory | Out-Null | |
} | |
if (!(Test-Path "$PSScriptRoot\results\")) { | |
New-Item "$PSScriptRoot\results\" -ItemType directory | Out-Null | |
} | |
$Date = Get-Date -Format "dd-MM-yyyy--HH-mm-ss" | |
$LogFilePath = "$PSScriptRoot\logs\log-$Date.log" | |
$ResultFilePath = "$PSScriptRoot\results\result-$Date.csv" | |
$ResultsTable = New-Object System.Data.DataTable | |
$ResultsColumnFQDN = New-Object System.Data.DataColumn FQDN, ([string]) | |
$ResultsColumnTCPPort = New-Object System.Data.DataColumn TCPPort, ([string]) | |
$ResultsColumnSuccessful = New-Object System.Data.DataColumn Successful, ([bool]) | |
$ResultsTable.Columns.Add($ResultsColumnFQDN) | |
$ResultsTable.Columns.Add($ResultsColumnTCPPort) | |
$ResultsTable.Columns.Add($ResultsColumnSuccessful) | |
Write-Host "Logging to $LogFilePath" | |
Start-Transcript -Path $LogFilePath | |
ForEach ($Target in $Targets) { | |
$ResultRow = $ResultsTable.NewRow() | |
$ResultRow.FQDN = $Target.FQDN | |
$ResultRow.TCPPort = $Target.TCPPort | |
Write-Host "[$($Target.FQDN):$($Target.TCPPort)] Testing " | |
$Result = Test-NetConnection -computername $Target.FQDN -Port $Target.TCPPort | |
If ($Result.TcpTestSucceeded -eq $true) { | |
Write-Host "[$($Target.FQDN):$($Target.TCPPort)] Success" | |
$ResultRow.Successful = $true | |
} | |
Else { | |
Write-Host "[$($Target.FQDN):$($Target.TCPPort)] Failure" | |
$ResultRow.Successful = $false | |
} | |
$ResultsTable.Rows.Add($ResultRow) | |
} | |
Stop-Transcript | |
$ResultsTable | Format-Table | |
Write-Host "Exporting to $ResultFilePath" | |
$ResultsTable | Export-Csv -Path $ResultFilePath -NoTypeInformation | |
Write-Host "Exported successfully" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment