PowerShell for Admins

Template based parsing and progress bars

Jonas Sommer Nielsen
5 min read
Share:

Working with wifi I have often needed to do a survey of the surroundings, and therefor I loved that windows 7 (maybe even Vista) introduced more advanced netsh with wifi support.

There’s a lot of useful information but it might be nice to have a more graphical overview. The thing is that a text blob like this is not very handy to work with.

image1

Some time late last year I heard a guy from the powershell team on the Powerscripting podcast talk about ConvertFrom-String and the new template based parsing. And it occurred to me that you can combine this with a simple powershell progress bar (write-progress) to give a visual representation of signal strength.

Why not try it out

Ps> help ConvertFrom-String -online

Help_ConvertFrom-String

This looks straight forward.

$TemplateSSID = @' Interface name : Wi-Fi There are 9 networks currently visible. SSID 1 : {SSID*:My Movies 5G} Network type : Infrastructure Authentication : WPA2-Personal Encryption : CCMP BSSID 1 : bc:ae:c5:eb:59:8c Signal : {SIGNAL:88}% Radio type : 802.11n Channel : 36 Basic rates (Mbps) : 6 12 24 Other rates (Mbps) : 9 18 36 48 54 SSID 3 : {SSID*:blackbox} Network type : Infrastructure Authentication : WPA2-Personal Encryption : CCMP BSSID 1 : c8:be:19:aa:98:a4 Signal : {SIGNAL:41}% Radio type : 802.11n Channel : 2 Basic rates (Mbps) : 1 2 5.5 11 Other rates (Mbps) : 6 9 12 18 24 36 48 54 SSID 4 : {SSID*:Greenbox} Network type : Infrastructure Authentication : WPA2-Personal Encryption : CCMP BSSID 1 : 20:c9:d0:28:fb:05 Signal : {SIGNAL:60}% Radio type : 802.11n Channel : 1 Basic rates (Mbps) : 1 2 5.5 11 Other rates (Mbps) : 6 9 12 18 24 36 48 54 BSSID 2 : 20:c9:d0:28:fb:06 Signal : 40% Radio type : 802.11n Channel : 100 Basic rates (Mbps) : 6 12 24 Other rates (Mbps) : 9 18 36 48 54 '@ $Netsh = netsh.exe wlan show networks mode=bssid $Netsh | ConvertFrom-String -TemplateContent $TemplateSSID Executing the the above code resulted in

testoutput1

This looks great. The data is structured nicely in a easy to use form.

Now lets combine that with a progress bar. We need a while loop to keep the progress bar alive and a one second sleep timer is probably a good idea.

while ($true) { $Netsh = netsh.exe wlan show networks mode=bssid $Networks = $Netsh | ConvertFrom-String -TemplateContent $TemplateSSID $i = 0 foreach($Network in $Networks) { Write-Progress -Id $i -Activity $Network.SSID -PercentComplete $Network.SIGNAL $i++ } Start-Sleep -Seconds 1 } The essential part is just a foreach looping through the networks objects. We use Write-Progress with parameters SIGNAL strength as PercentComplete and SSSID as Activity.

ise progress bars

It looks great in ISE and even works in the shell

shell progress

How cool is that?

The bright reader might have spotted an obvious flaw in the first template. It doesn’t handle networks with multiple radios e.g. a network with both a 2.4 ghz and 5 ghz and same ssid. And all the other nice information from netsh is simply ignored.

Second try

$TemplateSSID = @' Interface name : Wi-Fi There are 9 networks currently visible. {NETWORK*:SSID 1 : {SSID:My Movies 5G} Network type : Infrastructure Authentication : WPA2-Personal Encryption : CCMP {BSSID*:BSSID 1 : {MAC:bc:ae:c5:eb:59:8c} Signal : {SIGNAL:88}% Radio type : 802.11n Channel : {CHANNEL:36} Basic rates (Mbps) : 6 12 24 Other rates (Mbps) : 9 18 36 48 54}} {NETWORK*:SSID 3 : {SSID:blackbox} Network type : Infrastructure Authentication : WPA2-Personal Encryption : CCMP {BSSID*:BSSID 1 : {MAC:c8:be:19:aa:98:a4} Signal : {SIGNAL:41}% Radio type : 802.11n Channel : {CHANNEL:2} Basic rates (Mbps) : 1 2 5.5 11 Other rates (Mbps) : 6 9 12 18 24 36 48 54}} {NETWORK*:SSID 4 : {SSID:Greenbox} Network type : Infrastructure Authentication : WPA2-Personal Encryption : CCMP {BSSID*:BSSID 1 : {MAC:20:c9:d0:28:fb:05} Signal : {SIGNAL:60}% Radio type : 802.11n Channel : {CHANNEL:1} Basic rates (Mbps) : 1 2 5.5 11 Other rates (Mbps) : 6 9 12 18 24 36 48 54} {BSSID*:BSSID 2 : {MAC:20:c9:d0:28:fb:06} Signal : {SIGNAL:40}% Radio type : 802.11n Channel : {CHANNEL:100} Basic rates (Mbps) : 6 12 24 Other rates (Mbps) : 9 18 36 48 54}} '@ $Netsh = netsh.exe wlan show networks mode=bssid $Networks = $Netsh | ConvertFrom-String -TemplateContent $TemplateSSID $Networks There’s a bit more markup here, and I admit it took me a few tries to get my head around the nested data structure. Look more closely at SSID 4 above, and how this have 2 BSSID’s, because of this they are marked with a *.

Now $Networks contain a little more complicated data structure

testoutput2

Though if we dive into it                             

testoutput3

It does look more like what we saw first. But with more info. And we can even dig into TDC-TC network and see each channel.

testoutput4

A slightly modified loop

while ($true) { $Netsh = netsh.exe wlan show networks mode=bssid $Networks = $Netsh | ConvertFrom-String -TemplateContent $TemplateSSID $i = 0 foreach($Network in $Networks) { Write-Progress -Id $i -Activity $Network.network.SSID $i++ } Start-Sleep -Seconds 1 } And the percentage complete is a sub object. So we will need another loop to go through every BSSID attached to the SSID

while ($true) { $Netsh = netsh.exe wlan show networks mode=bssid $Networks = $Netsh | ConvertFrom-String -TemplateContent $TemplateSSID $i = 0 foreach($Network in $Networks) { foreach($bssid in $Network.NETWORK.bssid) { Write-Progress -id $i -Activity $Network.network.SSID -Status "Channel: $($bssid.CHANNEL) MAC: $($bssid.MAC)" -PercentComplete $bssid.SIGNAL $i++ } } Start-Sleep -Seconds 1 } The main thing here is of course using the template based parsing. It took me a few tries to figure it out, but it’s cool when it works and might be very useful in many other situations. The progress is just a hack that makes the presentation a little more fun.

References

Contact me

Twitter @mrhvid
Web Jonas.SommerNielsen.dk

Related Articles

Sep 15, 2023

PowerShell Escape Room

PowerShell Escape Room by Michiel Hamers

by Michiel Hamers
https://about.me/michielhamers/

Why on earth you want to create an Escape Room with PowerShell as backend?

I’ve always been a fan of escape rooms, so I decided to create my own for my kids. I wanted to make it something that would be challenging and fun for them, but also educational. I decided to use PowerShell as the backend for the escape room, as I’m a PowerShell developer and I thought it would be a great way to learn more about the language.
The first step was to design the rooms. I wanted to make sure that there were a variety of puzzles and challenges that my kids would have to solve. I also wanted to make sure that the rooms were visually appealing and engaging. Once I had the rooms designed, I started building them.
I used a variety of materials to build the rooms, including wood, cardboard, and fabric. I also used a few electronic components, such as a USB extension cable with a switch and a 3-button keyboard. The USB extension cable with a switch was used to create a physical button that my kids could press to solve one of the puzzles. The 3-button keyboard was used to enter the code that my kids had to find to solve another puzzle.
I also used a few websites to create rebus puzzles that my kids had to solve. I printed out the rebus puzzles and placed them around the rooms. Once my kids had solved all of the puzzles, they were able to enter the code on a single screen to escape the room.
In this blog post, we’ll delve into the process of creating an engaging PowerShell escape room for the global PowerShell community. We’ll emphasize the significance of storytelling and provide a detailed breakdown of the PowerShell structure used for the escape room.

Sep 15, 2023

Microsoft Graph PowerShell Module: Getting Started Guide

Microsoft Graph PowerShell Module: Getting Started Guide

by Jeff Brown

Microsoft is retiring the Azure AD Graph API sometime after June 30, 2023 (announcement). This retirement includes the Azure AD PowerShell module. In its place, Microsoft has released the Microsoft Graph PowerShell module. The Microsoft Graph PowerShell module is the next-generation way of managing Microsoft cloud services using PowerShell. If you have used MSOnline or Azure AD PowerShell in the past, you’ll need to read on to learn about this new module.

Oct 8, 2021

ICYMI: PowerShell Week of 08-October-2021

Topics include VMWare, Windows 11, Web Reports and more…

Special thanks to Robin Dadswell, Prasoon Karunan V, Kiran Patnayakuni and Kevin Laux

How to gather your vCenter inventory data with this VMware PowerShell script

by Scott Matteson on 7th October

Inventory reports are a common request when administering a VMware vCenter environment. Learn how this VMware PowerShell script can make such requests quick and easy

Building a Web Report in PowerShell, use the -Force Luke

by Chris Noring on 8th October