PowerShell for Windows Admins

Feb 25 2012   10:11AM GMT

Using [wmiclass] accelerator and string substitution

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tripped over an interesting problem

I want to use the [wmiclass] accelerator because I need to find the key of a WMI class (code borrowed from PowerShell team blog – to be returned when I’ve finished with it)

 

$t = [WMIClass]$class

$t.properties |

select @{Name="PName";Expression={$_.name}} -ExpandProperty Qualifiers |

where {$_.Name -eq "key"} |

foreach {"The key for the $class class is $($_.Pname)"}

 

Now I wanted to add the namespace so I tried this

PS> $namespace="root\cimv2"
PS> $class="Win32_Process"
PS> [wmiclass]"\\.\$namespace:$class"
Cannot convert value "\\.\Win32_Process" to type "System.Management.ManagementClass". Error: "Invalid namespace "
At line:1 char:11
+ [wmiclass] <<<< "\\.\$namespace:$class"
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

 

Huh – of course root\cimv2 is valid

But its not a WMI problem its a string substitution problem

because

PS> $target = "$namespace:$class"
PS> $target
Win32_Process

Oh – no namespace

Its because of the colon

The way we get round it is to escape the : using a backtick `

[wmiclass]\\.\$namespace`:$class

Which works

Just a little WMI quirk to be aware of

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: