In the previous tips we investigated the UPnP.UPnPDeviceFinder and how to identify devices in your network. Let’s look at some use cases. Obviously, these use cases are meant to be your source of inspiration because what you can do depends entirely on the actual devices connected to your network.

My first use case is managing a NAS device, namely my Synology Disk Station. It can be managed via a web interface but I always tend to forget the URL, and of course URL and port can vary depending on the configuration.

Here is a way to search for any disk station and open its web interface automatically. Since all connection data is retrieved from the device(s), this works even if IP addresses change or you configured alternate ports.

$UPnPFinder = New-Object -ComObject UPnP.UPnPDeviceFinder
$UPnPFinder.FindByType("ssdp:all", 0) |
Where-Object ManufacturerName -eq 'Synology' |
Select-Object -ExpandProperty PresentationUrl |
ForEach-Object { Start-Process -FilePath $_ }

Note: if you don’t own a Synology disk station, just look at other devices that you do own. Simply look at the data returned by FindByType(), and tailor Where-Object to your needs.

Since the code enumerates all UPnP devices, it takes 10-20 seconds. If you want to speed things up, you can figure out the UDNs (unique names) of the devices you are after, and in the future use these UDNs:

$UPnPFinder = New-Object -ComObject UPnP.UPnPDeviceFinder
$UPnPFinder.FindByType("ssdp:all", 0) |
Where-Object ManufacturerName -eq 'Synology' |
Select-Object -ExpandProperty UniqueDeviceName

UDNs are unique and differ per device, so this approach just makes sense if you need to access the same device over and over again. In my case, the UDN was uuid:7379AA6F-6473-6D00-0000-001132283f5e, and opening the web interface now worked much faster:

$UPnPFinder = New-Object -ComObject UPnP.UPnPDeviceFinder
$UPnPFinder.FindByType("uuid:7379AA6F-6473-6D00-0000-001132283f5e", 0) |
Select-Object -ExpandProperty PresentationUrl |
Foreach-Object { Start-Process -FilePath $_ }

Which leads to my second use case: I am using the Philips Hue system to manage lights in my home. To automate this, there is a rich REST API available from Philips. What you do need is the IP address of your bridge(s).

This will find the IP addresses of any Philips Hue bridge in your local network:

$UPnPFinder = New-Object -ComObject UPnP.UPnPDeviceFinder
$UPnPFinder.FindByType("upnp:rootdevice", 0) |
Where-Object Description -like 'Philips hue*' |
Select-Object -ExpandProperty FriendlyName |
ForEach-Object { 
  if ($_ -match '(?<BridgeName>\w*).*?\((?<IP>.*)\)')
  {
    $null = $matches.Remove(0)
    [PSCustomObject]$matches
  }
 }

This code looks for a root device where the description starts with “Philips hue”, then uses a regular expression to split the content of “FriendlyName” and return the bridge name and its IP address.

In my case, the result looks like this:

 
IP            BridgeName
--            ----------
192.168.22.10 BridgeOne 
192.168.23.16 BridgeWork 
 



You are a PowerShell Professional, passionate about improving your code and skills? You take security seriously and are always looking for the latest advice and guidance to make your code more secure and faster? You’d love to connect to the vibrant PowerShell community and get in touch with other PowerShell Professionals to share tricks and experience? Then PowerShell Conference EU 2020 might be just the right place for you: https://psconf.eu (June 2-5, 2020 in Hanover, Germany).

It’s a unique mixture of classic conference with three parallel tracks filled with fast-paced PowerShell presentations, and advanced learning class with live discussions, Q&A and plenty of networking.

Secure your seat while they last: https://psconf.eu/register.html. Help build the agenda and make this “your” event by submitting hypothetical sessions you’d like to hear: https://powershell.one/psconfeu/psconf.eu-2020/reverse-cfp. And if you’d like to present yourself and join the psconf.eu speakers’ team, submit proposals: https://sessionize.com/psconfeu/.

Twitter This Tip! ReTweet this Tip!

Anonymous