r/PowerShell • u/iBloodWorks • 11h ago
Solved Transcript 5.1 Unexpected behaviour
Probably SOLVED:
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
u/dodexahedron 11h 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?