A pipe dream

It all started innocently enough - while configuring the latest Windows Admin Center and adding machines to be managed by it, I noticed that you could enable System Insights on the ones what were 2019. Install was easy enough but since it required time for the feature to produce any suggestions I moved on to other things.

Fast forward a few weeks and a few IT fires later, my brain decided to purge all data related to the exact hostnames of the machines where I enabled said feature. I could remember only part of the naming convention used for that particular group of servers.

PowerShell to the rescue.....well sort of. It does have a cmdlet that generates the necessary information from a host:

Get-WindowsFeature -ComputerName someservername -Name system-insights 

Perfect! I'd just filter the machines using the naming convention remnants in my brain and pipe that output to the Get-WindowsFeature cmdlet. A few cups of coffee and only 86 iterations later I came up with this pseudo PS syntax: 

Get-ADComputer -Filter {criteria} | select name | foreach name in names {Get-WindowsFeature -ComputerName $variable -name name of windows feature}

...and the actual string of cmdlets and piping that produces output and doesn't maddeningly error out:

Get-ADComputer -Filter {name -like 'somename*'} | select name | foreach ($_.name) {Get-WindowsFeature -ComputerName $_.name | where name -eq 'system-insights'}

Here's a visual of the output:

Crap! Not a hostname in sight to be found so the information was useless... 

After many more iterations (a total of 168 to be precise according to the output of history) of what I thought would be a trivial and quick task we arrive to this "gem":

 $computers = ((Get-ADComputer -filter {name -like 'Server1*'}) | select name).name
foreach ($computer in $computers) {Get-WindowsFeature -ComputerName $computer | where {($_.name -eq 'system-insights') -and ($_.installed -eq 'installed')}| ft $computer, name, installstate}

resulting in this snippet of the output:

Server1     Name                InstallState
--------    ----                ------------
          System-Insights    Installed

As you can see, now we have the name of the system - Server1, along with the information of whether System Insights has been installed or not. 

In my stubbornness tenacity I learned a few things:

PowerShell can drive you to drinking and...

Get-ADComputer -filter {name -like 'Server1*'}) | select name

produces output with a header "name" which if you'd like to pipe and use as input for other commands can break things.

name <---- da header!
----
Server1
Server2
Server3

In order to suppress that you have either wrap it and specify the property with ".name"

$computers = ((Get-ADComputer -filter {name -like 'Server1*'}) | select name).name

or use -expandproperty

$computers = ((Get-ADComputer -filter {name -like 'Server1*'}) | select -expandproperty name

Hope the above saves someone time and/or frustration. If there is a more simple, elegant or efficient way to accomplish this task, please do let me know.