0

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?

codewario
  • 19,553
  • 20
  • 90
  • 159
jackhammer
  • 87
  • 10

2 Answers2

3

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.

codewario
  • 19,553
  • 20
  • 90
  • 159
  • This is actually EXACTLY what i needed. I want the higher transcript to capture the whole run (even the loops inside) and the lower transcript to capture only the user creation loop. Thank you very much. this was very helpful. – jackhammer Nov 20 '19 at 07:42
0

Just for future reference I am leaving a more complete code to demonstrate logging before, during and after the users loops. Thanks for Bender the Greatest for the solution.

Start-Transcript -Path "C:\logs\master_job.txt"
Write-Host "this is the maine program"
Write-Host "starting to work on users..."

foreach ($user in (1..10))
{
Write-Host "`n`n"
Start-Transcript -Path "C:\logs\user$user.txt"
Write-Host "working on user number $user"
Write-Host "adding more values to User $user"
Stop-Transcript
}

Write-Host "`n`ndoing some work after working on all users"
Write-Host "even more work"

Stop-Transcript
jackhammer
  • 87
  • 10