PS V3 ISE add-on for splatting – Updated

I went back a did a little cleanup on the idea of using an add-on to create snippets for splatting. I added a check of the number of parametersets, and if there is only one, it goes straight to the parameters for that property set instead of making you select it.

I also did some cleanup of the parameter grid display properties, and added some bits try and order the parameters a little more logically in the grid, so that the ones most likely to be chosen will be near the top. Mandatory parameters are listed first, then positional parameters that are not mandatory, then non-positional non-mandatory parameters, and finally common parameters. I also had it add the parameter type to created hash table snippet as a reminder of what kind of value it’s wanting. That may be something that needs to be switchable if it gets to be annoying, but may be a good thing to have as a default.

The code now looks like this:

function Make-ParamHash {
$common = 'Verbose','Debug','WarningAction','WarningVariable','ErrorAction','ErrorVariable','OutVariable','OutBuffer','WhatIf','Confirm'

$cmdlet = read-host "Enter cmdlet for parameter hash"
if ((get-command $cmdlet).capability -match "Cmdlet"){
    $varname = read-host "Enter variable name for hash"
        if ($varname.trim('$') -match '\w+'){

$param_hash = @("`$$varname = `@`{")
if ((get-command $cmdlet).parametersets.count -gt 1){
    $parameter_set = (get-command $cmdlet).parametersets |
    ogv -OutputMode Single -Title "Select Parameter Set"
    }
else {$parameter_set = (get-command $cmdlet).parametersets[0]}

   $parameter_set.parameters |
        select Name,@{l='Type';e={$_.parametertype.name}},IsMandatory,Position,order |
        % {
            if ($_.ismandatory ){$_.order = 0}
            elseif ($_.position -match '^\d+'){$_.order = 1}
            elseif ($_.position -match '^\-\d+'){$_.order = 2}
            if ($common -contains $_.name){$_.order = 3}
            $_
            } |
        sort order,name|
            select Name,Type,IsMandatory |
         ogv -OutputMode Multiple -Title "Select Properties for hash" |
        %{$param_hash += "`t$($_.name) = $($_.type)"
          }
    $param_hash += "`t`}"
    $param_hash += "`n $cmdlet `@$varname"
    $param_hash = $param_hash -join "`n"
    $ise_current_file = $psise.CurrentFile
    $ise_current_file.Editor.InsertText($param_hash)
    }
}

}

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Param Hash",{Make-ParamHash},"Ctrl+Shift+M")

Using it looks like this:

Launch the add-on, and enter the cmdlet name and variable name in the command window. Here, I’m selecting Send-MailMessage, and using a variable name of Email:

Send-MailMessage only has one parameterset, so it will go directly to the parameters for that parameterset. When the grid view is displayed, select the parameters you want to splat, and click OK or just hit Enter.

A hash table snippet is created in the ISE at the current cursor postition, using the selected parameters.

Advertisements

2 responses to “PS V3 ISE add-on for splatting – Updated

  1. Pingback: Tinkering with V3 ISE – an add-on for splatters | The Powershell Workbench

  2. Pingback: Passing named parameters to Powershell jobs | The Powershell Workbench

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