/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" out-dataGrid function for Monad "," Windows "Monad" Shell (MSH) Beta 3.1 "," Windows "Monad" Shell (MSH) Beta 3.1 "," scripting games Last Day MSH answers for event 7 a... "," Scripting Games almost over "," The Scripting Games Event 6 in MSH "," Scripting Games week 2 "," Monad Links "," MSH Concentration (scripting games part 4) "," scripting games part 3 "," ")

Monday, March 06, 2006

 


MSH get-WmiMethodHelp function Update 3



Another update on the get-WmiMethodHelp function, to get help on WMI methods and to generate template scripts for calling them.

the last version is broken in Beta 3 of Monad.

Reason for this is the change of behavior of -EQ - NE on an array, in Beta 2 this would return $true or $false in beta 3 they will return the members of the array that
do match. e.g. :

MSH>1,2,3,4 -eq 2
2
MSH>1,2,3,4 -ne 2
1
3
4

so I replaced them on a couple of places by -contains statements.
I also had to redo some other constructions for this and the overloads for the $de object seem to have changed in .NET RTM, I fixed that also. And did some more small changes, e.g. use of the $mp (I first planned a compleet rewrite but I will leave at this for now) , so that everything works again and a little but more cleaning is done.

after playing with the generated scripts (see also the example about filling a generated template in the WMI links), it is also nice to look into the workings of the get-WmiMethodHelp function how you can use the GetMethodParameters to get information about what parameters to pass to the WMI method and generate the script.

and here the revisited script :

# GetWmiMethodInfo.MSH (V3) 
# Gets Method Parameters with descriptions from WMI class  
# it also Creates an Example script.  
# /\/\o\/\/ 2006  
# http://mow001.blogspot.com 

Function get-WMIMethodHelp{  
  Param ([string] $WmiClass = "",[string] $WmiMethod = "")    
  if ($wmiClass -eq "") {"No WMIClass given";Return}  

  # Set Options  

  $opt = new-object management.ObjectGetOptions  
  $opt.UseAmendedQualifiers = $true  

  # Connect to Class 

  $MS = new-object management.ManagementScope
  $MP = $MS.Path  
  $MC = $Null 
  &  {  
       Trap {Continue
       $script:MC = new-object management.ManagementClass($MP,$WMIClass,$opt)  
     }
  if (! $script:mc) {"WMI Class not found";Return

  # Check if Method does Exist 
  $M = $script:mc.Get_Methods() | foreach {$_.name} 
  if (!($M -contains $WMIMethod)) {"Method not found, Methods are :";$m;return

  # Make a stringBuilder to make a Template Script  

  $SB = new-Object text.stringbuilder  
  $SB = $SB.Append('$Class = "') ; $SB = $SB.AppendLine("$WMIClass`"")  
  $SB = $SB.Append('$Method = "') ; $SB = $SB.AppendLine("$WMIMethod`"")  

  # Check for Static Qualifier (If not present then Method Needs Instance)  

  $Qualifiers=@()
  $script:mc.Get_Methods()[$wmiMethod].get_Qualifiers() | foreach {$qualifiers += $_.name}
  $static = $Qualifiers -Contains "Static" 

  If ($static) {  

    $SB = $SB.AppendLine('$MP = new-object system.management.ManagementPath')  
    $SB = $SB.Append('$MP.Server = "') ; $SB = $SB.AppendLine("$($MP.Server)`"")  
    $SB = $SB.Append('$MP.NamespacePath = "') ; $SB = $SB.AppendLine("$($MP.NamespacePath)`"")  
    $SB = $SB.AppendLine('$MP.ClassName = $Class`n')  
    $SB = $SB.AppendLine('$MC = new-object system.management.ManagementClass($MP)')  

  }Else{  
    # IF not static We need to provide The Key Properties to get the instance.  

    $SB = $SB.AppendLine('$Computer = "."')  
    $SB = $SB.AppendLine("`n#$wmiClass Key Properties :")  

    $Filter = ""  
    $script:mc.get_Properties() | foreach {  
      $Q = @()
      $_.get_Qualifiers() | foreach {$Q += $_.name} 

      $key = $Q -Contains "key" 
      If ($key) {  
        $CIMType = $_.get_Qualifiers()["Cimtype"].Value  
        $SB = $SB.AppendLine("`$$($_.Name) = [$CIMType]")  
        $Filter += "$($_.name) = `'`$$($_.name)`'"   
      }  
    }  

    $SB = $SB.Append('`n$filter = ');$SB = $SB.AppendLine("`"$filter`"")  
    $SB = $SB.AppendLine('$MC = get-WMIObject $class -computer $Computer -filter $filter`n')  

  }  
  $SB = $SB.AppendLine('$InParams = $mc.GetMethodParameters($Method)`n')  

  # output the Helptext   


  "$WMIClass : $WMIMethod :`n"  

   $q = $script:mc.Get_Methods()[$WMIMethod].get_Qualifiers() | foreach {$_.name} 
   if ($q -eq "Description") {$script:mc.Get_Methods()[$WMIMethod].get_Qualifiers()["Description"].Value}  
  

  "`n$WMIMethod Parameters :"  
  # get the Parameters  
   
  $inParam = $script:mc.Get_Methods()[$WMIMethod].get_InParameters()  

  $HasParams = $False  
  if ($true) {  
    trap{'[None]';continue}   

    $inParam.get_Properties() | foreach {  
      $HasParams = $true  
      $Q = $_.get_Qualifiers() | foreach {$_.name} 

      # if Optional Qualifier is not present then Parameter is Mandatory  
      $Optional = $q -contains "Optional" 

      $CIMType = $_.get_Qualifiers()["Cimtype"].Value  
      "`nName = $($_.Name) `nType = $CIMType `nOptional = $Optional"  

      # write Parameters to Example script  

      if ($Optional -eq $TRUE) {$SB = $SB.Append('# ')}  
      $SB = $SB.Append('$InParams["');$SB = $SB.Append("$($_.Name)`"] = ");$SB = $SB.AppendLine("[$CIMType]")  
      if ($q -eq "Description") {$_.get_Qualifiers()["Description"].Value}  
    }  
  }  

  # Call diferent Overload as Method has No Parameters  

  If ($HasParams -eq $True) {  
    $SB = $SB.AppendLine("`n`"Calling $WMIClass : $WMIMethod with Parameters :`"")  
    $SB = $SB.AppendLine('$inparams.get_properties() | select name,Value')  
    $SB = $SB.AppendLine('`n$R = $mc.InvokeMethod($Method, $inParams, $Null)')  
  }Else{  
    $SB = $SB.AppendLine("`n`"Calling $WMIClass : $WMIMethod `"")  
    $SB = $SB.AppendLine('`n$R = $mc.InvokeMethod($Method,$Null)')  
  }  

  $SB = $SB.AppendLine('`"Result : `"')  
  $SB = $SB.AppendLine('$R')  

  # Write the Sample Script :  

  "`nSample Script :`n"  
  "# $WMIclass $WMIMethod-Method Sample Script"  
  "# Created by Get-WmiMethodHelp (v3)"  
  "# /\/\o\/\/ 2006"  
  "# Fill InParams values before Executing"  
  "# InParams that are Remarked (#) are Optional`n"  

  $SB.ToString()  
}  

set-alias gwm get-WMIMethodHelp 




(load the function "dotsourced" like this :

. .\get-WmiMethodhelp.msh

after that the function is loaded and you can use it on the Command prompt, see also examples in the former posts in the series, links are below)

you can past the generated script to notepad, edit it and then past it back to the console or save it a script, and then call it

for more info about using WMI from MSH :

The 3 part series About Discovering WMI in MSH and Getting Help from WMI :

" Wmi-Help Part 1 " Getting All the WMI Classes and Descriptions in a HashTable (getWmiClasses.MSH)"
WMI help Part 2 ",Getting Help on WMI classes, ( get-WmiHelp (GWH) Function)"
WMI help Part 3 (Methods) ", Getting Help on Methods and generating Sample Scripts. (updated by the script posted now)

and :

WMI viewer script for MSH (GUI) (here you can also use this : out-dataGrid function for Monad , that will output almost any MSH commands output to a datagrid, there you also can find another change in Beta 3, the $form.topmost = $true does not work anymore, I did find another workaround for this here.this would work for all GUI scripts.)
Lets Query WMI from MSH (WMI queries on more (related) classes)
Passing (not so) mandatory Parameters to WMI methods in MSH (example use of generated script-template)
Saving a WMI instance to a XML-file from MSH (save a WMI instance for later use)

greetings /\/\o\/\/
Tags :

PS if you do find more scripts that do not work (anymore) , please leave a comment.


Comments: Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?