2
votes

I am running a powershell script from a batch file:

try {
  Start-Transcript -path ("C:\PS\Logs\XXXX_Session_QA_" + (Get-Date).tostring("yyyyMMdd-hhmmss-tt") + ".txt")

  <Rest of the Code>
}
catch {
  stop-transcript
}

Every time I run the script, I see the error

Error: Transcription has not been started. Use the start-transcript command to start transcription.

I have gone through some or most of the previous posts and the code should work but it isn't triggering at all. The initial log file isn't being created either. Any idea why this could be happening or any help here?

1
I would recommend that you don't put a single try{} around all of your code. Limit try{} catch{} to specific commands and put handling in the catch{} block for just that command. Otherwise you can't tell which part of your code is failing and triggering the catch{} logic. - Charlie Joynt

1 Answers

3
votes

Does the folder structure exist for where you are trying to write the transcript to? According to the documentation, it is required (see italics below):

-Path

Specifies a location for the transcript file. Enter a path to a .txt file. Wildcards are not permitted. If you do not specify a path, Start-Transcript uses the path in the value of the $Transcript global variable. If you have not created this variable, Start-Transcript stores the transcripts in the $Home\My Documents directory as \PowerShell_transcript..txt files.

If any of the directories in the path do not exist, the command fails.

https://technet.microsoft.com/library/05b8f72c-ae3b-45d5-95e0-86aa1ca1908a(v=wps.630).aspx

You will also need permission to write to that path of course.

So... if the Start-Transcript cmdlet is throwing an error, you are then catching it in the catch{} block (invisibly) which then executes the Stop-Transcript.

This presumably is what is actually causing the error message: the net result is that you are trying to stop transcription when it never started in the first place.