Sonntag, 31. Januar 2010

Out-ISEFile (V 0.9)

Just read the following tweet:

@MaxTrinidad Need the path to the various profile scripts on a system? Try $profile.psextended | Format-List (Try it in #PowerShell ISE too)

from @alexandair.

As I'm using ISE all day long, I tried it immedeatly and here is my result:
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_pro
file.ps1
CurrentUserAllHosts : C:\Users\berndk.MMEDVNT\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\berndk.MMEDVNT\Documents\WindowsPowerShell\Microsoft.PowerShe
llISE_profile.ps1



Oops again the PowerShells f***ing wrapping behavior. It's time to write a work around:

function Out-IseFile            
{

[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $True, ValueFromPipeline = $True)]
$msg,
[Parameter(Position = 1, Mandatory = $False, ValueFromPipeline = $False)]
$path = $null,
[switch]$fl
)
if ($fl)
{
$msg = $msg | Format-list | out-string -width 2000
}

$count = $psise.CurrentPowerShellTab.Files.count
$null = $psIse.CurrentPowerShellTab.Files.Add()
$Newfile = $psIse.CurrentPowerShellTab.Files[$count]
if ($path)
{
$NewFile.SaveAs($path)
$NewFile.Save([Text.Encoding]::default)
}
$Editor = $Newfile.Editor
#$Editor.SetCaretPosition($Editor.LineCount, 1)

$ar = $msg -split "`r`n" | %{ $_ -replace '\s*$', '' }

$msg = $ar -join "`r`n"

$msg = $msg + "`r`n"
$Editor.InsertText($msg)
$Newfile

}


$null = $profile.psextended | out-IseFile -fl



To use it just type:
$null = $profile.psextended | out-IseFile -fl


Please note: this is work in progress.
The main trick is to use Out-String with a long width parameter, that suppresses the unwanted wrapping.
For some reason (I guess it's just what we call in German Gedankenlosigkeit) Out-String emits trailing blanks. I included code to get rid of it.

Because Format-List seams rather frequent, I added the fl switch.

Remeber when ISE-Output pane behaves naughty ( and that is quite often) just send your output to a fresh editor. That makes it easy to edit and save it, if you want.

Have fun extending ISE.

Bernd

PS: Thanks to JS who made it extendable.

Keine Kommentare:

Kommentar veröffentlichen