PowerShell Queries and Square Brackets

Chiyo OdikaGENERAL, PowerShell, SCOM, UncategorizedLeave a Comment

I recently encountered an interesting issue while trying to query class instances in SCOM. The class instances for certain classes will return the parent class as well as associated classes in the following format: [Parent Class Name].attribute. For instance the class instance for Microsoft.Windows.Computer returns the following:

$SCOMClass = Get-SCOMClass -Name Microsoft.Windows.Computer

(Get-SCOMClassInstance -Class $SCOMClass)[0] | Select-Object *

 image

Ok so this returns expected values.

Now let’s return a specific object instead of all objects:

(Get-SCOMClassInstance -Class $SCOMClass)[0] | Select-Object [Microsoft.Windows.Computer].LogicalProcessors

image

This returns nothing, even though we clearly see values for the Logical Processors when we select all objects.

After some checking, re-checking, and second guessing, I found an article that’s as short as the solution is simple. Apparently double-escaping is required for the parser to properly handle square brackets in strings.

Note that the double-escapes are not single quotes. 🙂 This one got me initially.

(Get-SCOMClassInstance -Class $SCOMClass)[0] | Select-Object ``[Microsoft.Windows.Computer`].LogicalProcessors

image

Now when we type out the same command with double escapes, we get the expected output. Cheers!

The following two tabs change content below.
Strategist. Technologist. Skeptic. Friend.
Chiyo OdikaPowerShell Queries and Square Brackets