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

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 on by doubleclicking Create on go on with the Create help and templates

Note this information on top of the class : 

 

Win32_Share Method : Create

OverloadDefinitions:
System.Management.ManagementBaseObject Create(System.String Path, System.String Name, System.UInt32 Type, System.UInt32 MaximumAllowed, System.String Description, System.String Password, System.Management.ManagementObject#Win32_SecurityDescriptor Access)

Static : True
A Static Method does not an Instance to act upon

 you can see here in the examples that it works a bit different as a static class needs no instance the template and example scripts will be different :

This time you do not have to change the example code (no instance name needed) you can just paste it in :

[PoSH]> # Sample Of Connecting to a WMI Class                                                                                               
[PoSH]>                                                                                                                                     
[PoSH]> $Computer = "."                                                                                                                     
[PoSH]> $Class = "Win32_Share"                                                                                                              
[PoSH]> $Method = "Create"                                                                                                                  
[PoSH]>                                                                                                                                     
[PoSH]> $MC = [WmiClass]"\\$Computer\ROOT\CIMV2:$Class"                                                                                     
[PoSH]>                                                                                                                                     
[PoSH]>                                                                                                                                     
[PoSH]> # Getting information about the methods                                                                                             
[PoSH]>                                                                                                                                     
[PoSH]> $mc                                                                                                                                 
                                                                                                                                            
                                                                                                                                            
Win32_Share                                                                                                                                 
                                                                                                                                            
                                                                                                                                            
[PoSH]> $mc | Get-Member -membertype Method                                                                                                 
                                                                                                                                            
                                                                                                                                            
   TypeName: System.Management.ManagementClass#ROOT\CIMV2\Win32_Share                                                                       
                                                                                                                                            
Name   MemberType Definition                                                                                                                
----   ---------- ----------                                                                                                                
Create Method     System.Management.ManagementBaseObject Create(System.String Path, System.String Name, System.UInt32 Type, System.UInt3... 
                                                                                                                                            
                                                                                                                                            
[PoSH]> $mc.Create                                                                                                                          
                                                                                                                                            
                                                                                                                                            
MemberType          : Method                                                                                                                
OverloadDefinitions : {System.Management.ManagementBaseObject Create(System.String Path, System.String Name, System.UInt32 Type, System.UIn 
                      t32 MaximumAllowed, System.String Description, System.String Password, System.Management.ManagementObject#Win32_Secur 
                      ityDescriptor Access)}                                                                                                
TypeNameOfValue     : System.Management.Automation.PSMethod                                                                                 
Value               : System.Management.ManagementBaseObject Create(System.String Path, System.String Name, System.UInt32 Type, System.UInt 
                      32 MaximumAllowed, System.String Description, System.String Password, System.Management.ManagementObject#Win32_Securi 
                      tyDescriptor Access)                                                                                                  
Name                : Create                                                                                                                
IsInstance          : True                                                                                                                  
                                                                                                                                            
                                                                                                                                            
                                                                                                                                            
[PoSH]>                                                                                                    

 

You can see that here it is not so easy to do it on the fly on the interactive console,

if we look at the end of the help information we find a template script again :

When we fill in the template we get a script like this :

# Win32_Share. Create-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 = "Create"

$MC = [WmiClass]"\\$Computer\ROOT\CIMV2:$Class"

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

$InParams.Access = $null
$InParams.Description = 'PowerShell'
$InParams.MaximumAllowed = $null
$InParams.Name = [string]'Posh'
$InParams.Password = $null
$InParams.Path = 'c:\powerShell'
$InParams.Type = [uint32]0

"Calling Win32_Share. : Create with Parameters :"
$inparams.PSBase.properties | select name,Value | format-Table

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

 And we can save it to a file or just past to the console like this :

[PoSH]> $Computer = "."                                                                                                                     
[PoSH]> $Class = "Win32_Share"                                                                                                              
[PoSH]> $Method = "Create"                                                                                                                  
[PoSH]>                                                                                                                                     
[PoSH]> $MC = [WmiClass]"\\$Computer\ROOT\CIMV2:$Class"                                                                                     
[PoSH]>                                                                                                                                     
[PoSH]>                                                                                                                                     
[PoSH]> $InParams = $mc.psbase.GetMethodParameters($Method)                                                                                 
[PoSH]>                                                                                                                                     
[PoSH]>                                                                                                                                     
[PoSH]> $InParams.Access = $null                                                                                                            
[PoSH]> $InParams.Description = 'PowerShell'                                                                                                
[PoSH]> $InParams.MaximumAllowed = $null                                                                                                    
[PoSH]> $InParams.Name = [string]'Posh'                                                                                                     
[PoSH]> $InParams.Password = $null                                                                                                          
[PoSH]> $InParams.Path = 'c:\powerShell'                                                                                                    
[PoSH]> $InParams.Type = [uint32]0                                                                                                          
[PoSH]>                                                                                                                                     
[PoSH]> "Calling Win32_Share. : Create with Parameters :"                                                                                   
Calling Win32_Share. : Create with Parameters :                                                                                             
[PoSH]> $inparams.PSBase.properties | select name,Value | format-Table                                                                      
                                                                                                                                            
Name                                                                  Value                                                                 
----                                                                  -----                                                                 
Access                                                                                                                                      
Description                                                           PowerShell                                                            
MaximumAllowed                                                                                                                              
Name                                                                  Posh                                                                  
Password                                                                                                                                    
Path                                                                  c:\powerShell                                                         
Type                                                                  0                                                                     
                                                                                                                                            
                                                                                                                                            
[PoSH]>                                                                                                                                     
[PoSH]> $R = $mc.PSBase.InvokeMethod($Method, $inParams, $Null)                                                                             
[PoSH]> "Result :"                                                                                                                          
Result :                                                                                                                                    
[PoSH]> $R | Format-list                                                                                                                    
                                                                                                                                            
                                                                                                                                            
__GENUS          : 2                                                                                                                        
__CLASS          : __PARAMETERS                                                                                                             
__SUPERCLASS     :                                                                                                                          
__DYNASTY        : __PARAMETERS                                                                                                             
__RELPATH        :                                                                                                                          
__PROPERTY_COUNT : 1                                                                                                                        
__DERIVATION     : {}                                                                                                                       
__SERVER         :                                                                                                                          
__NAMESPACE      :                                                                                                                          
__PATH           :                                                                                                                          
ReturnValue      : 0                                                                                                                        
                                                                                                                                            
                                                                                                                                            
                                                                                                                                            
[PoSH]>                                                                                                    

 When you have this template and want to use it for more computers you can change some vaiables in top of the script to Parameters

e.g.

PARAM ($Computer = ".")

$Class = "Win32_Share" 

 $Method = "Create"

so you can use it like this :

./New-PoSHshare.ps1 computer

so you can choose what to provide in the script and what to make configurable.

in the next parts of this series some more advanced topics, but this should allready be usefull for a lot of WMI classes and methods.

Enjoy,

Greetings /\/\o\/\/

Published Monday, March 26, 2007 3:00 PM by admin

Comments

# RE: some basic admin tasks

Creating a share on a remote machine using WMI is covered in the article [link] I can't find a link for

Friday, August 17, 2007 12:30 PM by Latest Newsgroup Posts

# re: PowerShell WMI Explorer Part 3

Nice. But I am a little confused.

How can this tool be used to generate a ps script that will retreive the WMI information like Scriptomatic does, as opposed to showing how to use methods like your examples show?

Thanks

Thursday, August 30, 2007 9:21 PM by Antknee

# re: PowerShell WMI Explorer Part 3

Hi Antknee,

About generating a script listing all properties,

the Wmi Explorer does not do this as this not needed this is to simple in PowerShell , see part 1

just use get-wmiObject [className]| fl *

Greetings /\/\o\/\/

Friday, August 31, 2007 10:46 AM by MoW
Anonymous comments are disabled