Finding WMI Class Static Methods

by Feb 1, 2013

In a previous tip, we illustrated how to use Invoke-WmiMethod to create a network share locally and remotely. Today, you dive a bit deeper and find out which WMI classes actually provide methods, and what their arguments and argument orders are.

Note that WMI methods can be very powerful but are a low-level interface. This tip can only serve as a starter. Once you find an interesting class and method, you will probably have to do additional research until all works as expected. Also note that some WMI classes may not be implemented in all operating system versions.

To find all WMI classes that support static methods, try this:

$exclude = 'SetPowerState', 'Reset', 'Invoke'

Get-WmiObject -List -Class Win32_* | 
  Where-Object { $_.Methods } |
  ForEach-Object {
    $result = $_ | Select-Object -Property Name, Methods
    [Object[]]$result.Methods = $_.Methods | 
      Where-Object { ($_.Qualifiers | Select-Object -ExpandProperty Name) -contains 'Static' } |
      ForEach-Object { if($exclude -notcontains $_.Name) { $_.Name } }
    $result
  } |
  Where-Object { $_.Methods }  

The code lists all WMI classes that start with “Win32_” and have at least one method that is not listed in $exclude. The result looks something like this:

Name                              Methods                                                                        
----                              -------                                                                        
Win32_LogicalDisk                 {ScheduleAutoChk, ExcludeFromAutochk}                                          
Win32_Volume                      {ScheduleAutoChk, ExcludeFromAutoChk}                                          
Win32_Printer                     {AddPrinterConnection}                                                         
Win32_Process                     {Create}                                                                       
Win32_BaseService                 {Create}                                                                       
Win32_Service                     {Create}                                                                       
Win32_TerminalService             {Create}                                                                       
Win32_SystemDriver                {Create}                                                                       
Win32_PrinterDriver               {AddPrinterDriver}                                                             
Win32_ScheduledJob                {Create}                                                                       
Win32_DfsNode                     {Create}                                                                       
Win32_Share                       {Create}                                                                       
Win32_ClusterShare                {Create}                                                                       
Win32_ShadowCopy                  {Create}                                                                       
Win32_SecurityDescriptorHelper    {Win32SDToSDDL, Win32SDToBinarySD, SDDLToWin32SD, SDDLToBinarySD...}           
Win32_NetworkAdapterConfiguration {RenewDHCPLeaseAll, ReleaseDHCPLeaseAll, EnableDNS, SetDNSSuffixSearchOrder...}
Win32_ShadowStorage               {Create}                                                                       
Win32_ReliabilityStabilityMetrics {GetRecordCount}                                                               
Win32_ReliabilityRecords          {GetRecordCount}                                                               
Win32_OfflineFilesCache           {Enable, RenameItem, Synchronize, Pin...}                                      
Win32_Product                     {Install, Admin, Advertise}

If you wanted to try Win32_Share with its method Create(), you then need to know the correct order of arguments for the method:

$class = [wmiclass]'Win32_Share'
$methodname = 'Create'

$class.psbase.GetMethodParameters($methodname).Properties | 
  Select-Object -Property Name, Type |
  Format-Table -AutoSize   

The result looks something like this:

Name             Type
----             ----
Access         Object
Description    String
MaximumAllowed UInt32
Name           String
Password       String
Path           String
Type           UInt32

Next, you can create new shares locally and remotely like this:

$class = 'Win32_Share'
$methodname = 'Create'
$Access = $null
$Description = 'A new share'
$MaximumAllowed = 10
$Name = 'myNewShare'
$Password = $null
$Path = 'C:\windows'
$Type = 0
Invoke-WmiMethod -Path $class -Name $methodname -ArgumentList $Access, $Description, $MaximumAllowed, $Name, $Password, $Path, $Type 

To create the share remotely, add the parameter(s) -ComputerName and -Credential. Note that you will need local Administrator privileges to create a new share. Invoke-WmiMethod returns numeric return values. To decipher these and find out more about WMI methods, navigate to a search engine and enter the WMI class name (such as “Win32_Share”). Most WMI classes and their methods are well documented on MSDN.

ReTweet this Tip!