PowerShell Analyzer like execution hotkeys for PS CTP3 ISE.
January 3rd, 2009 by KarlPowerShell CTP3 ISE – Integrated Scripting environment has inherited many ideas and features from PowerShell analyzer including multiple runspaces,editors, a smaller immediate input area and output pane, however it doesn’t have the output visualizers of PSA nor the super fast RTS like execution control of PSA.
However Microsoft in their wisdom has made ISE rather extensible through the $PSISE variable, and many people already have added some very cool functionality to ISE through these.
When I first demo’d what was then MSH analyzer to Microsoft back in the first few months of 2006, the feature that seemed to stand out the most to the team was the ability to select an area of code and just run that. Thankfully that level of execution control is now in ISE as F6, but I wanted more, so i’m going to share with you a script that build a few months ago to add a couple of features.
F7 run the current physical line.
this basically will run the line where the caret current is at.
Real Time Strategy like control.. CTRL 1 , CTRL 2 etc
This here allows you to use comments to create regions, then EXECUTE those regions with a simple hotkey. I use this extensively. Often i am working on building a function, so I edit the function, press Ctrl 1 to apply the function, then if that was successful, press Ctrl 2 , then maybe Ctrl 3 to run some tests to make sure my changes to the function are what i am expecting.
This gives me a great AGILITY , putting what Jeffery Snover calls the Admin Development Model , but which is really REPL(Repeat Evaluate, Print , Loop ) rediscovered, on agile steroids.
function invoke-caretline { invoke-expression $([Regex]::Split($psISE.CurrentOpenedFile.Editor.text,"`r`n" )[$psISE.CurrentOpenedFile.Editor.caretline-1]) } $psISE.CustomMenu.Submenus.Add("Run single line", {invoke-caretline} , 'f7') function invoke-region([int] $num) { $ed = $psISE.CurrentOpenedFile.Editor $lines = [Regex]::Split($ed.text,"`r`n" ) $foundfirst = -1 $foundlast = -1 for($count = 0;$count -le $lines.length-1;$count++) { if ($lines[$count].startswith("#region") -and $lines[$count].contains("@$num")) { $foundfirst = $count;break} } if($foundfirst -gt -1) { for ($count = $foundfirst; $count -le $lines.length-1;$count++) { if ($lines[$count].startswith("#endregion") ) { $foundlast = $count;break} } if ($foundlast -gt -1) { $torun = "" $lines[$foundfirst..$foundlast] | % { $torun+=$_ + "`r`n"} invoke-expression $torun } } } $psISE.CustomMenu.Submenus.Add("run region 1", {invoke-region 1 }, 'ctrl+1') $psISE.CustomMenu.Submenus.Add("run region 2", {invoke-region 2 }, 'ctrl+2') $psISE.CustomMenu.Submenus.Add("run region 3", {invoke-region 3 }, 'ctrl+3') $psISE.CustomMenu.Submenus.Add("run region 4", {invoke-region 4 }, 'ctrl+4') $psISE.CustomMenu.Submenus.Add("run region 5", {invoke-region 5 }, 'ctrl+5')
Script @ PoshCode: View Script | Download
Sometime I’ll wrap all this up into a module, and add other PowerShell Analyzer-like functionality such as running the current PARAGRAPH. So often in an earlier PSA, and in SQL Query Analyzer, i’m highlighting again and again the SAME paragraph query and running it over and over again. Being able to just run the current paragraph saves that time.
Technorati Tags: powershell,shelltools,ISE,powershell analyzer
Posted in Powershell, pscom, PSV2 | 6 Comments »

January 4th, 2009 at 12:54 am
[...] You can read Karl’s blog and get his code HERE. [...]
January 4th, 2009 at 6:08 am
Hello Karl,
great work.
Concerning invoke-caretline: good that we have block-comments this days. One idea for improvement – multiline commands that end in ` could be processed as well.
And as currently I do not know any way to determine wheteher the focus is in an editor pane or the output pane, it inspires me for a function to copy the contents of the output-pane into a fresh editor pane.
Bernd
January 4th, 2009 at 6:44 pm
And here my solution:
function invoke-caretline
{
$text = $psISE.CurrentOpenedFile.Editor.text -split “`r`n”
$current = $psISE.CurrentOpenedFile.Editor.caretline-1
$cmd = ”
while ($true)
{
if ($text[$current][-1] -eq ‘´’) {
$cmd += $text[$current].substring(0,($text[$current].length – 1)) + ‘ ‘
$current += 1
}
else {
break
}
}
$cmd += $text[$current]
invoke-expression $cmd
}
try it on:
get-help ´
get-childitem
pwd
February 5th, 2009 at 6:11 pm
[...] while back I threw together a few scripts to enhance PowerShell V2 ISE experience that added some PowerShell Analyzer like features . They [...]
February 12th, 2009 at 12:35 am
[...] while back I threw together a few scripts to enhance PowerShell V2 ISE experience that added some PowerShell Analyzer like features . They [...]
March 30th, 2009 at 10:22 pm
[...] other custom commands are from Karl Prosser. I highly recommend you take a look at them as well. Very [...]