Calling WMI Methods with CIM Cmdlets

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

It can be very useful to call WMI methods, for example to create new shares, but in PowerShell v2 you had to know the names and exact order of arguments to submit:

$rv = Invoke-WmiMethod -Path 'Win32_Share' -ComputerName $ComputerName -Name Create -ArgumentList $null, $Description, $MaximumAllowed, $Name, $null, $Path, $Type

In PowerShell v3, you can use Get-CimClass to discover methods and arguments, and use Invoke-CimMethod instead. It takes the arguments as a hash table, so order is no longer important:

PS> $class = Get-CimClass -ClassName Win32_Share
PS> $class.CimClassMethods

Name                               ReturnType Parameters            Qualifiers
----                               ---------- ----------            ----------
Create                                 UInt32 {Access, Descripti... {Constructor, Impl..
SetShareInfo                           UInt32 {Access, Descripti... {Implemented, Mapp..
GetAccessMask                          UInt32 {}                    {Implemented, Mapp..
Delete                                 UInt32 {}                    {Destructor, Imple..

PS> $class.CimClassMethods['Create']

Name                               ReturnType Parameters            Qualifiers
----                               ---------- ----------            ----------
Create                                 UInt32 {Access, Descripti... {Constructor, Impl..

PS> $class.CimClassMethods['Create'].Parameters

Name                                  CimType Qualifiers            ReferenceClassName
----                                  ------- ----------            ------------------
Access                               Instance {EmbeddedInstance,...
Description                            String {ID, In, MappingSt...
MaximumAllowed                         UInt32 {ID, In, MappingSt...
Name                                   String {ID, In, MappingSt...
Password                               String {ID, In, MappingSt...
Path                                   String {ID, In, MappingSt...
Type                                   UInt32 {ID, In, MappingSt...

# create new share
PS> Invoke-CimMethod -ClassName Win32_Share -MethodName Create -Arguments @{Name='Testshare'; Path='c:\'; MaximumAllowed=[UInt32]4; Type=[UInt32]0}

Twitter This Tip! ReTweet this Tip!


Posted Mar 07 2013, 06:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.