Search code examples
powershell

Start multiple transcripts in Powershell


I have a script that loops through a CSV file and create an AD user for each line in the file. I want to have a first transcript for the entire master job and also a separate second transcript for each loop that creates a user.

It could look something like this:

start transcript MASTER
    import CSV file
    foreach line in CSV file
    start transcript UserX
        create the user
    stop transcript userX
stop transcript MASTER

Is this behavior ("nest" a transcript within a transcript) even possible in Powershell? The Start-Transcript command allows you to save the file to a specific -Path but the Stop-Transcript doesn't have a switch that allows you to stop a specific one.

Maybe there is a different approach to this?


Solution

  • Just did a little testing, you can't stop a specific one, but you can start multiple transcripts with Start-Transcript. Stop-Transcript will stop the most recently started transcript. I tested with this bit of code here:

    Start-Transcript -Path log1.log
    "cool"
    Start-Transcript -Path log2.log
    'hello
    neato'
    Stop-Transcript -?
    Stop-Transcript # stops the log2.log transcript
    Stop-Transcript # stops the log1.log transcript
    

    It's probably not what you want to hear, but I was able to determine from this test that any "parent" transcripts will capture the output from the child transcripts. So log2.log only contains "hello neato" and the help dialog for Stop-Transcript, but log1.log also contains this as well as its own outputs.


    If what you want is to output results of different operations to both files and the pipeline, read up on how redirection works and make use of Tee-Object to both output to the pipeline and a file at the same time.