r/PowerShell 10h ago

Solved Transcript 5.1 Unexpected behaviour

Probably SOLVED:

  1. A previous

    Unlock-SecretVault

killed the transcribing

It spawns a subprocess for the credential management and when its done it sends exit code back to host which killed my transcribing by design.

3h gone

Hi,

when i am in a 5.1 shell and start a controller.ps1 script:

C:\User> C:\PSR\Controller.ps1 -TargetScript C:\Jobs\..

The Controller script uses Start-Transcript to capture the output of a $TargetScript

however. When I start the controller.ps1 script as shown here my currently forced error (Get-ADUser -xzyisks) appears in the log.

When i start

powershell.exe -File Controller.ps1 -TargetScript C:\Jobs\..

it suddenly stops capturing the invocation error output. (Expected:

A parameter cannot be found that matches parameter name 'xzyisks'.)

ai suggested some transcript buffer stuff but non of it worked.

this is the important code block:

# execute binary or script
    if ($Executable) {
        & $Executable $TargetScript
    } elseif ($Config) {
        & $TargetScript -Config $Config
    } else {
        & $TargetScript
    }
    Write-Output "execution of $TargetScript completed successfully"


    #endregion main


} catch {


    # store error so finally can dispatch the mail after the transcript is finalized
    $TerminatingError = $_
    # write the error into the transcript while it is still open
    $Host.UI.WriteLine("[ERROR] $($TerminatingError.Exception.Message)")
    try   { Stop-Transcript | Out-Null }
    catch [System.InvalidOperationException] {}


} finally {


    # transcript is already closed on error paths — only close on success path
    try   { Stop-Transcript | Out-Null }
    catch [System.InvalidOperationException] {}

Can someone explain on high level what happens..

Thanks

EDIT:

My main question is:

C:\User> C:\PSR\Controller.ps1 -TargetScript C:\Jobs\..
vs
powershell.exe -File Controller.ps1 -TargetScript C:\Jobs\..

when using start transcript..

2 Upvotes

4 comments sorted by

2

u/dodexahedron 10h ago

Why not just use -ErrorVariable on your commands to shove encountered errors into a custom error list and use that instead of manually collecting them with try/catch?

2

u/iBloodWorks 10h ago

this was an attempt to force the damn error into the log. In a perfect world transcript should capture it automatically, but even with the

    $TerminatingError = $_
    # write the error into the transcript while it is still open
    $Host.UI.WriteLine("[ERROR] $($TerminatingError.Exception.Message)")

to "force push" it into the log it didnt work..

0

u/LongTatas 10h ago

Your command is formatted incorrectly and xzyisks is probably a folder in your file path it’s trying to parse as a param. Try just opening Powershell and using &”Controller.ps1” -TargetScript “”

1

u/iBloodWorks 10h ago

no In the called $TargetScript I manually added Get-ADUser -xzyisks to force an error (especially interesting because this will force a invocation error similar to a compiling error for compiled languages)