PowerShell for Windows Admins

Jun 30 2019   4:51AM GMT

Test-Connection formatting

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

Last time I showed how to write a function that replaces the current PowerShell 6/7 Test-Connection. This time I’ll show you how to do the Test-Connection formatting.

Using just the function you get something like this:

PS> test-connection -computername 127.0.0.1

Success : True
Source : W10PROIP
Destination : 127.0.0.1
IPV4Address : 10.10.54.5
Bytes : 32
Time : 0

etc

All of the properties on the object are displayed and because there are more than four properties PowerShell defaults to a list display. What we want is to drop the Success property and display the remaining fields as a table – exactly like Windows PowerShell v5.1. I’ve deliberately dropped the IPv6 data because I don’t use IPV6 for anything.

Start by exporting the format data from Windows PowerShell

PS> Get-FormatData -TypeName ‘System.Management.ManagementObject#root\cimv2\Win32_PingStatus’ | Export-FormatData –Path C:\test\ping.xml

You have to use the full name for the type which you can find via

Test-Connection 127.0.0.1 | get-member

If you open the xml file you’ll see something like this:

<?xml version=”1.0″ encoding=”utf-8″?>
<Configuration>
<ViewDefinitions><View>
<Name>System.Management.ManagementObject#root\cimv2\Win32_PingStatus</Name>
<ViewSelectedBy>
<TypeName>System.Management.ManagementObject#root\cimv2\Win32_PingStatus</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader><Label>Source</Label><Width>13</Width></TableColumnHeader>
<TableColumnHeader><Label>Destination</Label><Width>15</Width></TableColumnHeader>
<TableColumnHeader><Label>IPV4Address</Label><Width>16</Width></TableColumnHeader>
<TableColumnHeader><Label>IPV6Address</Label><Width>40</Width></TableColumnHeader>
<TableColumnHeader><Label>Bytes</Label><Width>8</Width></TableColumnHeader>
<TableColumnHeader><Label>Time(ms)</Label><Width>9</Width></TableColumnHeader>
</TableHeaders>
<TableRowEntries><TableRowEntry><TableColumnItems>
<TableColumnItem><PropertyName>__Server</PropertyName>
</TableColumnItem><TableColumnItem><PropertyName>Address</PropertyName></TableColumnItem>
<TableColumnItem><PropertyName>IPV4Address</PropertyName></TableColumnItem>
<TableColumnItem><PropertyName>IPV6Address</PropertyName></TableColumnItem>
<TableColumnItem><PropertyName>BufferSize</PropertyName></TableColumnItem>
<TableColumnItem><PropertyName>ResponseTime</PropertyName></TableColumnItem>
</TableColumnItems></TableRowEntry></TableRowEntries>
</TableControl>
</View></ViewDefinitions>
</Configuration>

Initially the XML will be all on one line. You’ll need to use a pretty printer of manually edit the XML to get into a more readable format. I’ve deliberately put a few entries on the same line where they should probably be separated on their own lines.

The test-connection function sets the object type to RSping – you can change that if you want.

The XML needs the type changing in the name and typename fields. You also need to change the tableColumnHeader and the TableColumnItem fields so they look like this:

<?xml version=”1.0″ encoding=”utf-8″?>
<Configuration>
<ViewDefinitions>
<View>
<Name>RSPing</Name>
<ViewSelectedBy>
<TypeName>RSPing</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader><Label>Source</Label><Width>13</Width></TableColumnHeader>
<TableColumnHeader><Label>Destination</Label><Width>15</Width></TableColumnHeader>
<TableColumnHeader><Label>IPV4Address</Label><Width>16</Width></TableColumnHeader>
<TableColumnHeader><Label>Bytes</Label><Width>8</Width></TableColumnHeader>
<TableColumnHeader><Label>Time(ms)</Label><Width>9</Width></TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem><PropertyName>Source</PropertyName></TableColumnItem>
<TableColumnItem><PropertyName>Destination</PropertyName></TableColumnItem>
<TableColumnItem><PropertyName>IPV4Address</PropertyName></TableColumnItem>
<TableColumnItem><PropertyName>Bytes</PropertyName></TableColumnItem>
<TableColumnItem><PropertyName>Time</PropertyName></TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>

Save the format data as RSPing.Format.ps1xml

To use the format data you need to load it

Update-FormatData -PrependPath .\RSPing.Format.ps1xml

By prepending the format data your format data will be used before any system formatting.

Now when you use test-connection you’ll get the formatting you want.

The new formatting data is session specific. It stays in the PowerShell session until you close it. If you want the formatting data in all sessions either create a module or dot source the test-connection function and load the formatting data in your profile.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: