powershell - how to check if transcript is running?
Asked Answered
P

7

17

I get this message everytime my script doesn't end properly and stop-transcript is not executed:

Start-Transcript : Transcription has already been started. Use the stop-transcr
ipt command to stop transcription.
At C:\ps\003desifrovanie.ps1:4 char:17
+ start-transcript <<<<  -path c:\_LOG\sfrbdesifrovanie.log -append
+ CategoryInfo          : NotSpecified: (:) [Start-Transcript], InvalidOpe
rationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Power
Shell.Commands.StartTranscriptCommand

Is it possible to check if transcript is running and stop it with if-then at start of the script? Or how to reliably stop it at the end? Thank you

Permeability answered 16/4, 2012 at 7:35 Comment(0)
V
16

What about an empty try-catch block at the beginning of your powershell script to stop transcribing?

try{
  stop-transcript|out-null
}
catch [System.InvalidOperationException]{}
Vladamir answered 16/4, 2012 at 8:2 Comment(1)
Thanks for the tip about Out-Null! It's a good alternative to $nullTreasonable
B
11

Wouldn't something like this work?

try
{
    Start-Transcript
    # Do my stuff
}

finally
{
    Stop-Transcript
}

From the documentation: The Finally block statements run regardless of whether the Try block encounters a terminating error. Windows PowerShell runs the Finally block before the script terminates or before the current block goes out of scope. A Finally block runs even if you use CTRL+C to stop the script. A Finally block also runs if an Exit keyword stops the script from within a Catch block.

Buell answered 16/4, 2012 at 8:10 Comment(2)
Thank you for picking out this cmdlet. I'm pretty sure it'll help in the future.Permeability
Thank you for the clear explanation. I was particularly curious about the CTRL + C issue. I find that this even works when in the midst of debugging in an IDE like VSCode when you either stop the debugger or it encounters an exception.Corollaceous
N
3

Explanation as provided by Rob Cutmore:

Start-Transcript will throw an error if it has already been started.

The code first attempts to start transcription. If transcription has already been started it catches the error, stops transcription, then starts it again.

This ensures output is being sent to the location specified by $myOutLog. It's probably better practice to catch the specific error instead of all errors though.

The } catch { line would then become } catch [System.InvalidOperationException]

try { 
  Start-Transcript -path $myOutLog
} catch {
  stop-transcript
  Start-Transcript -path $myOutLog 
} 

Nasty answered 1/12, 2014 at 18:11 Comment(3)
Can you provide some explanation as to what this code is doing?Flyleaf
Since the original poster never answered I'll provide an explanation. Start-Transcript will throw an error if it has already been started. The code above first attempts to start transcription. If transcription has already been started it catches the error, stops transcription, then starts it again. This ensures output is being sent to the location specified by $myOutLog. It's probably better practice to catch the specific error instead of all errors though. The } catch { line would then become } catch [System.InvalidOperationException] {.Erwinery
Thanks @RobCutmore - the original link is long dead, so your comment should be the actual answer...Nurse
T
1

Try the Test-Transcribing function available here: http://poshcode.org/1500

 if(Test-Transcribing) {Stop-Transcript}
Thusly answered 16/4, 2012 at 8:5 Comment(2)
This doesn't work in Powershell 4 (maybe earlier) the exception must have changed.Kaleena
The function doesn't work with newer versions of PowerShell, but the code is still available at WaybackMachine web.archive.org/web/20170419154624/http://poshcode.org/1500Jumbuck
J
0

In newer versions of PowerShell you don't get this error message as you can start additional transcripts in the same session.

The issue then would be to stop all transcripts, if more then one has been started by mistake.

So let's stop them all until there are no left to stop.

while ($true) {
  try {stop-transcript} catch {return}
}

Jumbuck answered 9/2 at 11:50 Comment(0)
D
-1

The following my issue of getting error message when I try to stop the transcript:

try {
  stop-transcript | out-null
}
catch [System.InvalidOperationException]{}
Difference answered 4/2 at 2:4 Comment(1)
Yes, but the issue was avoiding the error message if the transcript is already running,Jumbuck
C
-3
$erroractionpreference="Silentlycontinue"
start-transcript c:\file.txt
$transcriptrunning=$?
$erroractionpreference="Continue"
if($transcriptrunning) {stop-transcript}
Cox answered 12/2, 2020 at 17:58 Comment(1)
Side stepping the error message and using $? to catch the error when PowerShell already provides the method of try - catch is a bad practice.Jumbuck

© 2022 - 2024 — McMap. All rights reserved.