/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" More on Cryptograpy and MSH "," Get-credential and Decrypting a SecureString in MS... "," Atari Laptop (no MSH content) "," Sending Mail from MSH "," MSH More replacement script update (HotFix) "," Enhanced More Function for MSH "," Get driveletters for All drives (Providers) in MSH... "," Math "," Exploring .NET types, classes and Enums from MSH "," Wipe the screen in MSH "," ")

Friday, November 18, 2005

 


MSH get-WmiMethodHelp (GWM) Update + format tips.



MSH get-WmiMethodHelp (GWM) function Update + format tips for the generated output.

In 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.

I promised an update with better errorhandling,
, so now it looks like this :

MSH>get-WMIMethodHelp
No WMIClass given
MSH>get-WMIMethodHelp win32_share
No WMIMethod given
MSH>gwm win32_share foo
Method not found, Methods are :
Create
SetShareInfo
GetAccessMask
Delete
MSH>gwm win32_shareCreate
No WMIMethod given
MSH>gwm win32_share Create
win32_share : Create :


The Create method initiates sharing for a server resource. Only members of the Administrators or Account Operators local group or those wit
h Communication, Print, or Server operator group membership can successfully execute Create. The Print operator can add only printer queues
. The Communication operator can add only communication device queues. The method returns an integer value that can be interpretted as foll
ows:
0 - Successful completion.
2 - The user does not have access to the requested information.


..........

Much better I think ;-)

Also I added the -Computer Option to the Static Method Sample Script (it was there for the Non static but not for static),
and I added A Comment To make it more Clear what the Key Values you have to Provide are.

so the Generated script for Win32_share Delete will look like this :

# win32_share delete-Method Sample Script
# Created by Get-WmiMethodHelp
# /\/\o\/\/ 2005
# Fill InParams values before Executing
# InParams that are Remarked (#) are Optional

$Class = "win32_share"
$Method = "delete"
$Computer = "."
#win32_share Key Properties
$Name = [string]

$filter = "Name = '$Name'"
$MC = get-WMIObject $class -computer $Computer -filter $filter

$InParams = $mc.GetMethodParameters($Method)

"Calling win32_share : delete "

$R = $mc.InvokeMethod($Method,$Null)
"Result : "
$R


(As explained in the original post the $inparms line stays even if there are no imparms, but as I was looking on errorhandling this Time, I did not want to change the flow,for this

Also I got some Formatting Tips for the Help-layout, of the Help part (of all 3 scripts) I did not change the output in the script,
but will paste the formatting tips below the script so you can pick what you like ;-)

gr /\/\o\/\/


--- The Updated script : ----------

# GetWmiMethodInfo.MSH (V2)
# Gets Method Parameters with descriptions from WMI class 
# it also Creates an Example script. 
# /\/\o\/\/ 2005 

Function get-WMIMethodHelp{ 
  Param ([string] $WmiClass = "",[string] $WmiMethod = "")   

  #Check Params 

  if ($wmiClass -eq "") {"No WMIClass given";Break
  if ($wmiMethod -eq "") {"No WMIMethod given";Break

  # Set Options 

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

  # Connect to Class

  $MS = new-object system.management.ManagementScope 
  $mc = $false
  If ($true) {
    Trap {Continue}
    $MC = new-object system.management.ManagementClass($MS,$WMIClass,$opt) 
  }
  if ($mc -eq $false) {"WMI Class not found";Break}

  # Check if Method does Exist

  $M = $mc.Get_Methods() | foreach {$_.name}
  if ($m -ne $WMIMethod) {"Method not found, Methods are :";$m;Break}

  #make a stringBuilder to make a Template Script 

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

  # if Static Qualifier is not present then Method Needs Instance 

  $Q =  $mc.Get_Methods()[$WMIMethod].get_Qualifiers() | foreach {$_.name}
  $static = $Q -eq "Static"

  If ($static -eq $True) { 
     
    # MP is used to make it possible to choose a Computer in the generated Script   
 
    $MP = new-object system.management.ManagementPath($MS.Path.path) 

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

  }Else
  
    [void]$SB.AppendLine('$Computer = "."'
    [void]$SB.AppendLine("`n#$wmiClass Key Properties :"
    # IF not static We need to provide The Key Properties to get the instance. 

    $Filter = "" 
    $mc.get_Properties() | foreach { 
      $Q = $_.get_Qualifiers() | foreach {$_.name}
      $key = $Q -eq "key"
 
      If ($key -eq $True) { 
        $CIMType = $_.get_Qualifiers()["Cimtype"].Value 
        [void]$SB.AppendLine("`$$($_.Name) = [$CIMType]"
        $Filter += "$($_.name) = `'`$$($_.name)`'"  
      } 
    } 

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

  } 

  [void]$SB.AppendLine('$InParams = $mc.GetMethodParameters($Method)`n'

  # back to the Normal output  

  "$WMIClass : $WMIMethod :`n" 

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

  "`n$WMIMethod Parameters :" 
  # get the Parameters 
  
  $inParam = $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 -eq "Optional"

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

      # write Parameters to Example script 

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

  # Call diferent Overload as Method has No Parameters 

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

  [void]$SB.AppendLine('`"Result : `"'
  [void]$SB.AppendLine('$R'

  # Write the Sample Script : 

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

  $SB.ToString() 


set-alias gwm get-WMIMethodHelp



----- Formatting tip's for all 3 scripts :

Very nice indeed! BTW the output is a bit hard to follow because there is so much of it. I added a writeHighlightedText function which helps although is somewhat garish. :-) Note: Be sure to use only the limited set of colors found in System.ConsoleColor.

--
Keith

function writeHighlightedText {
param ([string] $text, [string] $bgColor = "DarkBlue", [string] $fgColor = "White")

$origBgColor = $host.ui.rawui.BackgroundColor
$origFgColor = $host.ui.rawui.ForegroundColor
trap [Exception] {
$host.ui.rawui.BackgroundColor = $origBgColor
$host.ui.rawui.ForegroundColor = $origFgColor
break
}

$host.ui.rawui.BackgroundColor = $bgColor
$host.ui.rawui.ForegroundColor = $fgColor
$text
$host.ui.rawui.BackgroundColor = $origBgColor
$host.ui.rawui.ForegroundColor = $origFgColor
}

writeHighlightedText "`n$WMIClass :"


The other thing you could do is:

@($MO.get_properties()) |
format-table @{label="Name";expression={$_.Name}; Width=30 }, @{Label="Description"; expression={$_.qualifiers["Description"].Value + "`n"}} -wrap

This produces a reasonable table. Jim Truher has a nice WRAP-STRING function (which wraps on word boundaries) which would make this even nice.

--
Jeffrey Snover [MSFT]

the wordwrapping script Mentioned :

James Truher [MSFT] wrote:

> This is a little word wrapping script that I often use - no magic here, but I thought you might like it.
>
>
> # a text wrapper, it takes a width and string and an indent
> param ( [int]$width = 75, [string]$string, [int]$shift = 0)
>
> $words = $string.split(" ")
> $space = " "
> $space *= $shift
> $lw = 0
> $ww = $space
> foreach($w in $words)
> {
> $lw += $w.length + 1
> if($lw -gt $width)
> {
> $ww # write the object to the result stream
> $ww = $space
> $lw = 0 + $w.length + 1
> }
> $ww += "$w "
> }
> # be sure not to leave anything else behind
> $ww

A tip from /\/\o\/\/ on this ;-)

tip, if you want to fill-up the window, use $witdh like this

$width = $host.ui.rawui.buffersize.width - $shift

or

$width = $host.ui.rawui.WindowSize.width - $shift
114

as you don't know what the users settings are.


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?