Test-Connection: Ping Remote Hosts the PowerShell Way

Today’s cmdlet of the day is Test-Connection. Test-Connection is a cmdlet that not surprisingly tests your network connection. Think of Test-Connection as PowerShell’s implementation of the popular ping utility. Even though both have ICMP in common, you’ll see that the two methods are a little different under the covers. Using this cmdlet is simple. At it’s most basic, just specify a ComputerName parameter, and it will send four ICMP requests to the destination host.

This output looks similar to ping.exe and, on the surface, it is but Test-Connection issues the ICMP request a little differently. Unlike ping.exe, Test-Connection is using the local computer’s WMI class Win32_PingStatus to send the ICMP request. Using the local WMI repository means you’d better be sure your local WMI repository is healthy else Test-Connection will not work. Test-Connection’s Object Output Also, as with the beauty of PowerShell, this cmdlet doesn’t merely return what immediately […]

Read more

Powershell as a Telnet Replacement

You know what grinds my gears? Telnet is no longer a default option in Windows. Gone are the days where one could easily test an open TCP port by doing telnet HOSTNAME PORTNAME. Good ol’ telnet. Sure, all activity could easily be seen across the wire but it was sure convenient. What’s an admin to do? Well, where should an admin look to nearly every time? Powershell, of course! Technically, you could get it added via adding a feature but we’re not going to go there. Using the .NET TcpClient class we can instruct Powershell to use this to create an object to open up a temporary connection against a remote computer. Based on if an error is thrown or not lets us know if the TCP port is open or not. In a nutshell, this function initiates the Connect method on the TcpClient object on a remote computer against […]

Read more

Start and Stop a Packet Capture from PowerShell

Have you ever had a need to do a quick packet capture on something but didn’t want to mess with installing Wireshark or Netmon? You’re in luck! Today, I decided to PowerShellify the method to kick off a packet capture via netsh. In this script there are two functions; Start-PacketCapture and Stop-PacketCapture. Both are very simple functions that just act as wrappers around the netsh commands with a little bit of validation in there. Let’s say you’re having some problems with network connectivity on your local machine and want to quickly fire up a packet capture. Download the PS1 file I’ve created with the functions inside and dot source it. This will get both functions in your current session.

Once you do this, you can then start the packet capture.

Give this a second to get the trace setup and once it releases control back to your console […]

Read more

Get-LocalPort: netstat Powershellified and Text Parsing Shenanigans

Get-LocalPort and Text Parsing Shenanigans I recently had a request from a coworker that seemed to be innocent enough. He simply needed a “Powershell netstat”. I’ve been known to throw down some regex from time to time so I was up the for the challenge and didn’t think too much of it….until I dove in. As usual, I severely underestimated the time it would take! The basic premise of this little project is to figure out some kind of structure that netstat outputs. Even though it’s a big ol’ string there’s always some kind of common form that it uses. It turns out discovering this was extremely difficult with netstat. Why? Because sometimes a port had the associated process owner, sometimes a port had an associated service, each port could either be IPv4 or IPv6 which had a very different string format and on top of that, the hardest part […]

Read more

An All-in-One Server Port Testing Tool

An All-in-One Server Port Testing Tool I’m lazy; too lazy sometimes. I’m lazy of not doing my due diligence before attempting to connect to a server. “It pings so it’s online and all the services I’d possible want to connect to are available!” Riiight. In order to build a robust script that’s not going to fall over and die on half your servers it’s important to first make sure the prerequisites that exist in order for you to get your end result are met. What are these prereqs? Services like FTP, HTTP, DCOM, WMI, WsMan, etc. There is a hierarchy of several layers of checks you can perform on your servers before attempting to make a connection depending on how anal you want to get. For starters you’ve got the entire OSI stack to traverse on the network side. That’s not including services on the host system that you’re executing […]

Read more

Get TCP/UDP Connection Count

Here’s a simple situation I ran into today. I’ve been troubleshooting an intermittent problem with a Windows 2008 R2 server for a few weeks now. I opened a Microsoft support ticket and the problem was believed to be due to port exhaustion in the dynamic port range. I confirmed the max ports were at 16,184 which was the default at the command prompt. netsh int ipv4 show dynamicport tcp netsh int ipv4 show dynamicport udp I then needed a quick way to test this so I came up with this simple script. I had to use the CLI utility netstat because this server does not have Powershell v4 on it else I would have used Get-NetTCPConnection. I figured I’d share to save someone a little bit of time figuring out how to get the TCP/UDP connection count on a remote server.

Read more

Here Is Why You Should Automate Your VPN Connection

I’m lazy. I don’t want to do anything twice, and if you read my blog I have a feeling you’re just as lazy as I am. I’ll automate anything! Here’s a prime example. Rather than waste 30 seconds of my time every day when I VPN into my client’s site and fire up remote desktop, I want to do it with a script. All this script does is connect to a VPN gateway then connect to a device via remote desktop. It’ll save you 25 seconds of your time every day, but it’ll easily save you an hour’s worth of time over the year! I use this with the older Cisco IPSEC VPN client so I make no guarantees with other VPN clients. To prevent typing in your RDP password every time, save your RDP credentials to a file.

Read more