Tuesday, 1 November 2011

Multi-node ping tests with PowerShell

While working on a Microsoft Lync 2010 project I was asked about network latency between points on our network - specifically what latency is like between offices, and where the worst latency occurs

To answer the question I could have made remote connections to a device in each office and pinged each other office, but with seventeen offices to test, that seemed laborious. Instead I turned to PowerShell, and created a script to do the work for me.

I decided that the easiest way to get the information I needed was to use every domain controller in my domain to ping every other DC - as we have a DC in every office this would give me a fairly good representation of ping times between sites.

The script uses the Test-Connection cmdlet to do the pinging, and utilises Start-Job to run all the ping tasks in parallel, saving a lot of time. The output is then stored in a CSV file which can easily be analysed.

# Get a list of DCs
$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Nodes = $Domain.DomainControllers | Select-Object -ExpandProperty Name

# Start the jobs
$Jobs = @()
ForEach ($Node in $Nodes) {
 $Jobs = $Jobs + (Start-Job -ArgumentList @($Node, $Nodes) -ScriptBlock {Test-Connection -Source $Args[0] -ComputerName $Args[1] -Count 10})

# Output the jobs

# Wait for the jobs to finish
$Jobs | Wait-Job

# Get the output from the jobs
$JobResults = Receive-Job $Jobs

# Write the results to a CSV file
$JobResults | Select-Object @{Name='Source';Expression={$_.__SERVER}}, @{Name='Destination';Expression={$_.Address.ToString().ToUpper().Split('.')[0]}}, ResponseTime | Export-Csv -Path "JobResults.csv"  -NoType

# Clean up
Remove-Job $Jobs
Download Script

There are a few points to note about this script:
  1. The Test-Connection cmdlet supports an "-AsJob" parameter, but using this parameter appears to make the cmdlet ignore the "-Count" parameter, and perform just one ping to each destination. I wanted to have 10 pings from each source to each destination, so worked around the issue by using Start-Job to spawn each job individually.
  2. You need to have admin access to your DCs in order to use this script.  If you don't want to use DCs you can replace the first three lines with a static array containing the computer names you want to test with.
  3. Each computer you use for the test must be enabled for PowerShell Remoting

- Ben

No comments:

Post a comment

Note: only a member of this blog may post a comment.