Powershell Windows

As some know, I’m working on a book.  The subject involves working with over 120 PowerShell cmdlets.  Many of these cmdlets have help files that are incomplete, so I have been blazing some pretty new territory.  I constantly have to look up the parameters for these cmdlets so that I can be sure that what I’m writing is accurate.  Rather than view the detailed help (which might not even be helpful), I wrote a script to pull out the parameters into a list.  The script was embarrassing, really, but it was a quick hack that I needed and it worked.  Then, just the other day while browsing the VMware Community forums, another user who goes by the name LucD spouted a snippet of code that for some reason I had not happened upon before.  It was a much better way to do what I had been doing with my previous Get-Parameter function.  I took his technique and added some BSonPosh-style custom object goodness and a few other features and hark!  The script is ready for all to see.  :)

function Get-Parameter ($Cmdlet) {
    foreach ($paramset in (Get-Command $Cmdlet).ParameterSets){
        $Output = @()
        foreach ($param in $paramset.Parameters) {
            $process = "" | Select-Object Name, ParameterSet, Aliases, IsMandatory, CommonParameter
            $process.Name = $param.Name
            if ( $paramset.name -eq "__AllParameterSets" ) { $process.ParameterSet = "Default" }
            else { $process.ParameterSet = $paramset.Name }
            $process.Aliases = $param.aliases
            $process.IsMandatory = $param.IsMandatory 
            if ($param.aliases -match "vb|db|ea|wa|ev|wv|ov|ob") { $process.CommonParameter = $TRUE }
            else { $process.CommonParameter = $FALSE }
            $output += $process
        }
        Write-Output $Output
        Write-Host "`n"
    }
}

As input it takes a single cmdlet name.  The output looks like the below.  Note the blank line, that’s to help separate when you have multiple ParameterSets.  I added a CommonParameter field so that you could filter out the boring stuff if you want.

PS > Get-Parameter New-Runspace | ft

Name               ParameterSet       Aliases                   IsMandatory    CommonParameter
----               ------------       -------                   -----------    ---------------
ComputerName       ComputerName       {Cp}                            False              False
Authentication     ComputerName       {}                              False              False
Credential         ComputerName       {}                              False              False
Port               ComputerName       {}                              False              False
UseSSL             ComputerName       {}                              False              False
Shell              ComputerName       {}                              False              False
ApplicationName    ComputerName       {}                              False              False
ThrottleLimit      ComputerName       {}                              False              False
Verbose            ComputerName       {vb}                            False               True
Debug              ComputerName       {db}                            False               True
ErrorAction        ComputerName       {ea}                            False               True
WarningAction      ComputerName       {wa}                            False               True
ErrorVariable      ComputerName       {ev}                            False               True
WarningVariable    ComputerName       {wv}                            False               True
OutVariable        ComputerName       {ov}                            False               True
OutBuffer          ComputerName       {ob}                            False               True

Authentication     Uri                {}                              False              False
Credential         Uri                {}                              False              False
Shell              Uri                {}                              False              False
ThrottleLimit      Uri                {}                              False              False
ConnectionUri      Uri                {URI, CU}                       False              False
Verbose            Uri                {vb}                            False               True
Debug              Uri                {db}                            False               True
ErrorAction        Uri                {ea}                            False               True
WarningAction      Uri                {wa}                            False               True
ErrorVariable      Uri                {ev}                            False               True
WarningVariable    Uri                {wv}                            False               True
OutVariable        Uri                {ov}                            False               True
OutBuffer          Uri                {ob}                            False               True

Runspace           Runspace           {RunspaceInfo}                  False              False
ThrottleLimit      Runspace           {}                              False              False
Verbose            Runspace           {vb}                            False               True
Debug              Runspace           {db}                            False               True
ErrorAction        Runspace           {ea}                            False               True
WarningAction      Runspace           {wa}                            False               True
ErrorVariable      Runspace           {ev}                            False               True
WarningVariable    Runspace           {wv}                            False               True
OutVariable        Runspace           {ov}                            False               True
OutBuffer          Runspace           {ob}                            False               True

 

file attachment: get-parameter.ps1

: http://halr9000.com/article/482

Post a comment now » Sorry, the comments are closed.

No comments yet.

Sorry, the comment form is closed at this time.