PowerShell is an underrated tool. This is despite PowerShell’s ability to create powerful scripting languages and a powerful shell environment. PowerShell can be used in a variety of IT tasks, regardless of the environment. One of these IT tasks is diagnosing the reasons why devices are not connecting to a network, or troubleshooting network problems. ICMP packets are the best tool for IT professionals in these situations.
For these types of tests, the ping application in Windows command prompt has been the preferred tool. Ping allows you to send an ICMP packet to IP addresses in a network to check if there’s a response. Pinging a device can provide us with so much more information. We could find the latency time to receive a response, or the hops it takes for a packet to reach a device.
Today we will discuss how to ping a device using PowerShell and how to use Pester for creating tests.
What is Pester?
Pester is a tool to test and mock code within PowerShell. PowerShell is more than a shell environment. PowerShell can also be used as a scripting language. Pester is designed for creating PowerShell scripts and writing tests for them.
Training from SPOTO for Penetration TestingRelated Training
Start trainingPester is useful for many things. Pester will be used to determine if the latency to connect to a device is acceptable. We will also check if a service or device responds in a reasonable time frame.
Pester can be used in conjunction with native PowerShell applets to create a variety scripts to test network functions or security. Pester could be used to create an example to ping all IP addresses on a subnet to check if they are responding.
How to ping in PowerShell
Although pinging equipment in a network with PowerShell can be as simple as using Command Prompt to ping a device, it’s not difficult. I would wager that PowerShell is more complicated than the number of characters required to send a message.
How do you ping PowerShell? Use the ‘TestConnection’ applet. It’s as simple as that.
Similar to how you would use CMD to ping, type in “Test-Connection” followed by “-Ping”, “-TargetName” and an IP address to send the ping to that device. The ‘TestConnection’ applet will send four ICMP messages to that IP address, and log their responses in the PowerShell console. It’s as simple as that.
There are many commands that can be used with ‘Test Connection’. To help you remember those commands, type “Help Test-Connection” into a PowerShell console. This will give you a list of commands the ‘Test Connection’ applet will accept as well as the data type that it will return.
This is an important point to remember. PowerShell is not like other shell environments. PowerShell can return values based upon different data types, while other shell environments log information as strings to the console. This is important to remember when you write a PowerShell script that compares values. Your scripts might not work properly if you don’t.
There are a few commands we should be focusing on.
The first command is ‘-TargetName. Target name is a string value that specifies its parameters. This command will read the IP address you want to ping as a string in ‘Test-Connection.’ and not as four different octet value. If you need to send a message to multiple devices, you can pass an array of IP addresses to ‘-TargetName.
Another command is -Count’. This command tells Test-Connection how many ICMP packets should each device be sent. By default, Test-Connection sends four packets to each device and listens for four replies. However, you may not want to run all four tests. You can specify the number of times that ‘Test Connection’ should send an ICMP packet using that ‘-Count property.
Finally,
0