Get netstat and traceroute results from remote servers

netstat traceroute files

Today you will find out how to get netstat and traceroute results from remote machines. I often had a situation where I was asked to do some basic checks from a network perspective. It’s easy once you already have a script for that.

Netstat and traceroute

There are multiple ways to do network checks. I guess that most of you are probably familiar with commands like ipconfig, tracert, netstat and ping. In this example, I had to combine a simple netstat command and PowerShell Test-NetConnection cmdlet with -Traceroute parameter.

Please note that Traceroute can take a long time to complete. If you want to check multiple servers it will take few hours probably especially if URLs used for the testing purpose are inaccessible.

Below you can find a script which will run network tests on the remote machines. Here is the output in the console for a single machine:

netstat and traceroute

And if you want to display property:

netstat example

As you can see its difficult to read all the information in PowerShell console. This is why script will save results in Netstat and Traceroute folders on your desktop:

netstat and traceroute

Final script:

# Server list
$Servers = Get-Content "c:\users\$env:username\desktop\input.txt"
# Define empty array
$Results = @()
# Looping each server and adding objects to array
$Results = Invoke-Command -cn $Servers {
            # URLs
            $URLs = "",
            # Creating new object
            $Object = New-Object PSCustomObject
            $Object | Add-Member -MemberType NoteProperty -Name "Servername" -Value $env:computername
            $Object | Add-Member -MemberType NoteProperty -Name "Netstat" -Value $(netstat -an)
            # Looping each URL
            Foreach ($URL in $URLs){
                $ObjectProp = (($URL -split "\//")[1]).trim()
                $Trace = Test-NetConnection $ObjectProp -traceroute
                $Object | Add-Member -MemberType NoteProperty -Name $($ObjectProp) -Value $Trace -Force
            # Adding object to array
} | select * -ExcludeProperty runspaceid, pscomputername,PSShowComputerName

# Paths for netstat and traceroute results
$NetStatPath = "c:\users\$env:username\desktop\netstat\"
$TracePath   = "c:\users\$env:username\desktop\traceroute\"
# Creating folders
$NetFolder   = Test-Path $NetStatPath; if ( -not $NetFolder)   { New-Item $NetStatPath -type Directory }
$TraceFolder = Test-Path $TracePath; if ( -not $TraceFolder) { New-Item $TracePath -type Directory }

# Saving results to txt files
foreach ($Item in $Results) {
    #Getting all properties from object
    $Properties = ($item | Get-Member -MemberType Properties).name | where {$_ -ne "Servername"}
    # Looping each property
    Foreach ($p in $Properties) {
        If($p -notmatch "netstat"){
            $Path = $TracePath + "$($item.Servername)" + "_$($p)" + "_Traceroute.txt"
            $item.$p | Out-File $Path -Force
            $Path = $NetStatPath + "$($item.Servername)" + "_NetStat.txt"
            $Item.$p | Out-File $Path -Force

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.