f.68
f.68

Reputation: 13

Multiple Select-Object as dot-notation

Problem

I want to know if there is a easy way to search multiple objects in a WMI object or CIM instance.

I'm aware it's possible with commands like

Get-CimInstance Win32_BaseBoard | Select-Object Manufacturer,Product

But I want a command with a dot notation where you can set multiple objects for a search like (Get-CimInstance Win32_BaseBoard).Manufacturer with more than one object.

Something like (Get-CimInstance Win32_BaseBoard).Object1.Obejct2.Object3

Upvotes: 1

Views: 1213

Answers (2)

postanote
postanote

Reputation: 16076

As noted, DotNotation (Intellisense) is 1:1 / single node / collection thing. You can validate this, by pushing the result of the call to XML and walking the nodes.

(Get-CimInstance Win32_BaseBoard | ConvertTo-Xml).Objects.Object.Property
(Get-CimInstance Win32_BaseBoard | ConvertTo-Xml).Objects.Object.Property.name
(Get-CimInstance Win32_BaseBoard | ConvertTo-Xml).Objects.Object.Property.'#text'

The only other option off the top of my head is constructing a proxy that uses enums and switches for all possible properties for the namespace you are using.

That's way more unneeded effort and code to just do what BenH has pointed out, specifically because you'd have to do that for every class.

Now, if you just wanted to shorthand this, maybe do this

$Base = Get-CimInstance Win32_BaseBoard
$Base.Manufacturer;$base.Model;$base.Name;$Base.PartNumber

But that is just unwieldy, especially, since all this really doing is single commands set on a single line with the command break separator, the semi-colon. Which is a wrong thing to do. IMHO.

---small rant --- If you need the separator, then just put the next thing on a new line and avoid the semi-colon. I mean, I can see that semi-colon use as the PoSH console host, but in a real script, function, module, well, just, no. Again, IMHO --- small rant ---

Lastly, depending on what your target PoSH version is, DotNotation had issues in v2 - v3 days

Upvotes: 0

briantist
briantist

Reputation: 47792

Ben's solution from the comments will work but note that it calls Get-CimInstance once for each property you want, even though that's unnecessary (could take a while depending on the call you're making).

Let's look at it a few other ways. We'll start by storing the wanted property names in an array.

$properties = 'Manufacturer', 'Product'

Now we can do something similar to what Ben did:

$allValues = $properties |
    ForEach-Object -Begin {
        $bb = Get-CimInstance Win32_Baseboard
    } -Process {
        $bb.$_
    }

That keeps his approach but does the CIM call once.

If you want to do with dot notation purely, you can use the .ForEach() method and the .PSObject hidden property to get at the properties:

(Get-CimInstance Win32Baseboard).ForEach({$_.PSObject.Properties.Where({$_.Name -in $properties}).Value})

Upvotes: 1

Related Questions