PowerShell 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 = -1for($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.
Karl Prosser
http://www.karlprosser.com/coder