First PowerShell Blog   1 comment

I keep getting hounded to blog so here you go.  I’m a PM on the PowerShell team and i’m largely responsible for the PowerShell language (Bruce Payette shares the blame with me in equal portion).  I thought that one of the things that I could do here would be to keep track of all the scripts that I use on a somewhat daily basis.  They’ve been posted in other venues before, but I can post them here too. I have more on my mind than just PowerShell, but I’ll post that under a different category
 
This first script is a script that I use all the time. I only remember the parameters of cmdlets that I use all the time, and while I can get the parameter information via (get-command cmdlet).definition, or from help I don’t care for the formatting. I wrote this script to help me with that, it provides me the information in a way that I like. You might like it too.
# The Command to retrieve information, Verbose output, show Ubiquitous Parameter info
# get-paraminfo
param ( [string]$cmd, [bool]$raw = $false, [bool]$ubp = $false )

# Formatting directives used in out-special
$type = @{ label = "Type";      e = { $_.Parametertype.name }}
$man  = @{ label = "Mandatory"; e = { $_.IsMandatory}}
$pos  = @{ label = "Pos";       e = { $_.Positional}; Align = "right" }
$vp   = @{ label = "PipeValue"; e = { $_.ValueFromPipeline } ; width = 10}
$vppn = @{ label = "PipeName";  e = { $_.ValueFromPipelineByPropertyName } ; 
           width = 10 }
$parms = "name",$type,$man,$pos,$vp,$vppn

# Use the special formatting built to output the results in a 
# palatable way
function out-special ()
{
    if ( $raw ) { $input }
    else { $input | format-table $parms -auto }
}

#
# Start Here
#
if ( ! $cmd -or (get-command $cmd ).count )
{
    get-command $cmd 
    Throw "
    -cmd -raw -ubp
    Need single Cmdlet
    "
}

# show the name of the command
$cmd
# and the DLL
get-command $cmd|format-list dll,helpfile | out-host

# here are the ubiquitous parameters
$ub = "ErrorVariable","ErrorAction","OutBuffer","Verbose","OutBuffer","Debug"
# Get the parameter sets and display the interesting info
foreach ( $pset in (get-command $cmd ).parametersets )
{
    "Parameter Set: " + $pset.name 
    # create a custom "object" so we can print it nicely
    $pset.parameters | foreach-object { 
        # optionally toss the ubiquitous parameters
        if ( $_.Position -lt 0 ) { $p = "named" } else { $p = $_.Position } 
        if ( !($ub -contains $_.name) -or $ubp )
        {
            $_ | add-member NoteProperty Positional $p -pass |
                 add-member NoteProperty ParameterSetName $pset.name -pass  
        }
    } | sort-object Positional | out-special
}
I think that the interesting bits are the additional properties that I add, which extends the object where I want. Also, this script emits either objects or text, depending on whether you use the -raw parameter. You can also get the ubiquitious parameters (-ubp), which I usually suppress. Most of the time, I just use the text output. Here’s how it looks against the get-command cmdlet.
PS> get-paraminfo get-command
get-command

DLL      : C:\WINDOWS\assembly\GAC_MSIL\System.Management.Automation\1.0.9567.0
           __31bf3856ad364e35\System.Management.Automation.dll
HelpFile : System.Management.Automation.dll-Help.xml

Parameter Set: CmdletSet

Name         Type            Mandatory   Pos PipeValue PipeName
----         ----            ---------   --- --------- --------
ArgumentList Object[]            False     1     False    False
Usage        SwitchParameter     False named     False     True
OutVariable  String              False named     False    False
TotalCount   Int32               False named     False     True
Verb         String[]            False named     False     True
Noun         String[]            False named     False     True
PSSnapin     String[]            False named     False     True


Parameter Set: AllCommandSet

Name         Type            Mandatory   Pos PipeValue PipeName
----         ----            ---------   --- --------- --------
Name         String[]            False     0      True     True
ArgumentList Object[]            False     1     False    False
OutVariable  String              False named     False    False
Usage        SwitchParameter     False named     False     True
CommandType  CommandTypes        False named     False     True
TotalCount   Int32               False named     False     True
So this tells me where the positional parameters are and whether they take pipeline input which is what I want, most of the time.
jim
Advertisements

Posted May 1, 2006 by jtruher3 in PowerShell

One response to “First PowerShell Blog

Subscribe to comments with RSS.

  1. Very useful script.  I extended it to handle alias names by inserting a few extra lines before the "# show the name of the command" comment:
     
    if ((get-command $cmd).CommandType.ToString() -eq "Alias"){  $cmd = (get-command $cmd).Definition}
     
    Now I can type:
     
    > get-paraminfo gc

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: