Dwarfsoft [GPA]


by on Jul.15, 2014, under Scripting, Tweet, Work

Print This Post Print This Post

I finally got around to fixing one of my biggest bugbears in PowerShell ISE 2.0, and when I did so I found on our new management server that it was broken for PowerShell ISE 4.0. I’m talking about Command Transcription. One of the most useful things I use in my day to day work is Start-Transcript for the standard PowerShell Shell. I have it automatically start in my PowerShell Profile so I can always go back and find how I achieved a particular goal.

I don’t use PowerShell ISE often, but when I do it’s usually because I am researching how to get something to work, or doing a colorized copy of the code for documentation purposes. For a long time I was happy enough with the suggested “Start-iseTranscript” provided by the Scripting Guy Blog.

Over the last couple of days I have been attempting to refine my answer to a similar Stack Exchange question, thinking that there must be a way to achieve something much more similar to Start-Transcript, without having it run as the last command of your ISE session, or script.

So, here is what I’ve come up with to handle PowerShell ISE 2.0 and 4.0.

Function Start-iseTranscript
   [string]$logname = (Get-logNameFromDate -path "C:\fso" -postfix " $(hostname)" -Create)
 $transcriptHeader = @"
Windows PowerShell ISE Transcript Start
Start Time: $((get-date).ToString('yyyyMMddhhmmss'))
UserName: $env:username
UserDomain: $env:USERDNSDOMAIN
ComputerName: $env:COMPUTERNAME
Windows version: $((Get-WmiObject win32_operatingsystem).version)
Transcript started. Output file is $logname
 $transcriptHeader >> $logname
 $psISE.CurrentPowerShellTab.Output.Text >> $logname
  #Keep current Prompt
  if ($Global:__promptDef -eq $null)
    $Global:__promptDef =  (gci Function:Prompt).Definition
    $promptDef = (gci Function:Prompt).Definition
  } else
    $promptDef = $Global:__promptDef
  $newPromptDef = @'
if ($Host.Version.Major -eq 2)
  if ($Global:_LastText -ne $psISE.CurrentPowerShellTab.Output.Text)
    Compare-Object -ReferenceObject ($Global:_LastText.Split("`n")) -DifferenceObject ($psISE.CurrentPowerShellTab.Output.Text.Split("`n"))|?{$_.SideIndicator -eq "=>"}|%{ 
$_.InputObject.TrimEnd()}|Out-File -FilePath ($Global:_DSTranscript) -Append
    $Global:_LastText = $psISE.CurrentPowerShellTab.Output.Text
} elseif ($Host.Version.Major -eq 4)
  if ($Global:_LastText -ne $psISE.CurrentPowerShellTab.ConsolePane.Text)
    Compare-Object -ReferenceObject ($Global:_LastText.Split("`n")) -DifferenceObject ($psISE.CurrentPowerShellTab.ConsolePane.Text.Split("`n"))|?{$_.SideIndicator -eq "=>"}|%{ 
$_.InputObject.TrimEnd()}|Out-File -FilePath ($Global:_DSTranscript) -Append
    $Global:_LastText = $psISE.CurrentPowerShellTab.ConsolePane.Text
'@ + $promptDef
  $Global:_LastText = $psISE.CurrentPowerShellTab.Output.Text
  New-Item -Path Function: -Name "Global:Prompt" -Value ([ScriptBlock]::Create($newPromptDef)) -Force|Out-Null

I hope this helps someone, as it’s been a long time issue for PowerShell ISE as an environment.

Cheers, Chris.

:, , , , , , ,

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!