Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell WMI Explorer Part 2

In the first part of this series : PowerShell WMI Explorer Part 1 we used the browser to explore the classes in this second post I will show how we can generate and use template scripts and help for calling WMI methods and how to use them.

For this example I go on from the win32_share class I also used as an example in the fist part, and select a method, in this case delete.

Note . this example will asume a share exists named PoSH with the directory c:\PowerShell, you can create any share to test, in the second part of this post I will show how to create it with the WMI explorer, but I wanted to start with the delete example first as that is more handy ,  so you can also make it by hand to follow the example.

 

You can see if we doubleclick the Method we get more help about the selected method.

but I also give a bit more information on top and an example top connect, (at the end of the help also an complete template script is given).

If you select the script and paste it in notepad you can add the name :

$Name = [string]'PoSH'

and then you can just paste it on the console :

[PoSH]> $Computer = "."                                                                                                                     
[PoSH]> $Class = "Win32_Share"                                                                                                              
[PoSH]> $Method = "Delete"                                                                                                                  
[PoSH]> # Win32_Share. Key Properties :                                                                                                     
[PoSH]> $Name = [string]'PoSH'                                                                                                              
[PoSH]> $filter="Name = '$Name'"                                                                                                            
[PoSH]> $MC = get-WMIObject $class -computer $Computer -Namespace "ROOT\CIMV2" -filter $filter                                              
[PoSH]> $mc                                                                                                                                 
                                                                                                                                            
                                                                                                                                            
__GENUS          : 2                                                                                                                        
__CLASS          : Win32_Share                                                                                                              
__SUPERCLASS     : CIM_LogicalElement                                                                                                       
__DYNASTY        : CIM_ManagedSystemElement                                                                                                 
__RELPATH        : Win32_Share.Name="posh"                                                                                                  
__PROPERTY_COUNT : 10                                                                                                                       
__DERIVATION     : {CIM_LogicalElement, CIM_ManagedSystemElement}                                                                           
__SERVER         : NL-37YHW1J                                                                                                               
__NAMESPACE      : ROOT\CIMV2                                                                                                               
__PATH           : \\NL-37YHW1J\ROOT\CIMV2:Win32_Share.Name="posh"                                                                          
AccessMask       :                                                                                                                          
AllowMaximum     : True                                                                                                                     
Caption          : Posh                                                                                                                     
Description      : Posh                                                                                                                     
InstallDate      :                                                                                                                          
MaximumAllowed   :                                                                                                                          
Name             : posh                                                                                                                     
Path             : c:\powershell                                                                                                            
Status           : OK                                                                                                                       
Type             : 0                                                                                                                        
                                                                                                                                            
                                                                                                                                            
                                                                                                                                            
[PoSH]> [wmi]'\\NL-37YHW1J\ROOT\CIMV2:Win32_Share.Name="posh"'                                                                              
                                                                                                                                            
                                                                                                                                            
__GENUS          : 2                                                                                                                        
__CLASS          : Win32_Share                                                                                                              
__SUPERCLASS     : CIM_LogicalElement                                                                                                       
__DYNASTY        : CIM_ManagedSystemElement                                                                                                 
__RELPATH        : Win32_Share.Name="posh"                                                                                                  
__PROPERTY_COUNT : 10                                                                                                                       
__DERIVATION     : {CIM_LogicalElement, CIM_ManagedSystemElement}                                                                           
__SERVER         : NL-37YHW1J                                                                                                               
__NAMESPACE      : ROOT\CIMV2                                                                                                               
__PATH           : \\NL-37YHW1J\ROOT\CIMV2:Win32_Share.Name="posh"                                                                          
AccessMask       :                                                                                                                          
AllowMaximum     : True                                                                                                                     
Caption          : Posh                                                                                                                     
Description      : Posh                                                                                                                     
InstallDate      :                                                                                                                          
MaximumAllowed   :                                                                                                                          
Name             : posh                                                                                                                     
Path             : c:\powershell                                                                                                            
Status           : OK                                                                                                                       
Type             : 0                                                                                                                        
                                                                                                                                            
                                                                                                                                            
                                                                                                                                            
[PoSH]>                                                                                                     

 As you can see in the second example with the WMI adapter and the path

[wmi]'\\NL-37YHW1J\ROOT\CIMV2:Win32_Share.Name="posh"'

you can also connect directly (note that the instanceinformation shows the path and you can turn it on in the listview also by checking it in the propertieslist (not checked by default),

 also for the Template script generated (Scroll to end of help)

# Win32_Share. Delete-Method Template Script"  
# Created by PowerShell WmiExplorer 
# /\/\o\/\/ 2006 
# www.ThePowerShellGuy.com
#
# Fill InParams values before Executing  
# InParams that are Remarked (#) are Optional

$Computer = "."
$Class = "Win32_Share"
$Method = "Delete"

# Win32_Share. Key Properties :
$Name = [string]'PoSH'

$filter="Name = '$Name'"
$MC = get-WMIObject $class -computer $Computer -Namespace "ROOT\CIMV2" -filter $filter

$InParams = $mc.psbase.GetMethodParameters($Method)

"Calling Win32_Share. : Delete "

$R = $mc.PSBase.InvokeMethod($Method,$Null)
"Result :"
$R | Format-list

 

 (after I did fill it in in PowerShell analyzer )

You can say that this is a bit much as you can do it like this in PowerShell directly :

([wmi]'\\COMPUTER\ROOT\CIMV2:Win32_Share.Name="posh"').delete()

[PoSH]> [wmi]'\\NL-37YHW1J\ROOT\CIMV2:Win32_Share.Name="posh"' | gm                                                                         
                                                                                                                                            
                                                                                                                                            
   TypeName: System.Management.ManagementObject#ROOT\CIMV2\Win32_Share                                                                      
                                                                                                                                            
Name                MemberType   Definition                                                                                                 
----                ----------   ----------                                                                                                 
GetAccessMask       Method       System.Management.ManagementBaseObject GetAccessMask()                                                     
SetShareInfo        Method       System.Management.ManagementBaseObject SetShareInfo(System.UInt32 MaximumAllowed, System.String Descrip... 
AccessMask          Property     System.UInt32 AccessMask {get;set;}                                                                        
AllowMaximum        Property     System.Boolean AllowMaximum {get;set;}                                                                     
Caption             Property     System.String Caption {get;set;}                                                                           
Description         Property     System.String Description {get;set;}                                                                       
InstallDate         Property     System.String InstallDate {get;set;}                                                                       
MaximumAllowed      Property     System.UInt32 MaximumAllowed {get;set;}                                                                    
Name                Property     System.String Name {get;set;}                                                                              
Path                Property     System.String Path {get;set;}                                                                              
Status              Property     System.String Status {get;set;}                                                                            
Type                Property     System.UInt32 Type {get;set;}                                                                              
__CLASS             Property     System.String __CLASS {get;set;}                                                                           
__DERIVATION        Property     System.String[] __DERIVATION {get;set;}                                                                    
__DYNASTY           Property     System.String __DYNASTY {get;set;}                                                                         
__GENUS             Property     System.Int32 __GENUS {get;set;}                                                                            
__NAMESPACE         Property     System.String __NAMESPACE {get;set;}                                                                       
__PATH              Property     System.String __PATH {get;set;}                                                                            
__PROPERTY_COUNT    Property     System.Int32 __PROPERTY_COUNT {get;set;}                                                                   
__RELPATH           Property     System.String __RELPATH {get;set;}                                                                         
__SERVER            Property     System.String __SERVER {get;set;}                                                                          
__SUPERCLASS        Property     System.String __SUPERCLASS {get;set;}                                                                      
ConvertFromDateTime ScriptMethod System.Object ConvertFromDateTime();                                                                       
ConvertToDateTime   ScriptMethod System.Object ConvertToDateTime();                                                                         
Delete              ScriptMethod System.Object Delete();                                                                                    
GetType             ScriptMethod System.Object GetType();                                                                                   
Put                 ScriptMethod System.Object Put();                                                                                       
                                                                                                                                            
                                                                                                                                            
[PoSH]> ([wmi]'\\NL-37YHW1J\ROOT\CIMV2:Win32_Share.Name="posh"').delete                                                                     
                                                                                                                                            
                                                                                                                                            
Script              : $this.PSBase.Delete()                                                                                                 
OverloadDefinitions : {System.Object Delete();}                                                                                             
MemberType          : ScriptMethod                                                                                                          
TypeNameOfValue     : System.Object                                                                                                         
Value               : System.Object Delete();                                                                                               
Name                : Delete                                                                                                                
IsInstance          : False                                                                                                                 
                                                                                                                                            
                                                                                                                                            
                                                                                                                                            
[PoSH]> ([wmi]'\\NL-37YHW1J\ROOT\CIMV2:Win32_Share.Name="posh"').delete()                                                                   
[PoSH]> [wmi]'\\NL-37YHW1J\ROOT\CIMV2:Win32_Share.Name="posh"'                                                                              
Cannot convert value "\\NL-37YHW1J\ROOT\CIMV2:Win32_Share.Name="posh"" to type "System.Management.ManagementObject". Error: "Not found "    
At line:1 char:6                                                                                                                            
+ [wmi]' <<<< \\NL-37YHW1J\ROOT\CIMV2:Win32_Share.Name="posh"'                                                                              
[PoSH]>                                                                                                     

 But as you can see in the following example with creating a share if the Method gets more difficult this way of working will pay off:

 

Continued in next post (live writer problems with big posts and raw HTML)

Enjoy,

Greetings /\/\o\/\/

Published Monday, March 26, 2007 2:29 PM by admin

Comments

# PowerShell WMI Explorer Part 3

We will go on here from the Second post about deleting a share : PowerShell WMI Explorer Part 2 we go

Monday, March 26, 2007 4:00 PM by The PowerShell Guy
Anonymous comments are disabled