r/PowerShell • u/iBloodWorks • 1d 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..
0
u/LongTatas 1d 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 1d 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)
0
u/MonkeyNin 1d ago
Powershell has non-terminating errors, and terminating errors.
- For details check out: deep dives: Everything you wanted to know about exceptions and terminating errors
If you want to force a terminating error you can use:
throw "message"or
"some error" | Write-Error -ErrorAction stopYou can force errors with
-ErrorAction Stopon common commands. likefunction DoStuff { param( [string] $Path ) $Required = Get-Item -ea 'stop' $Path # if file is gone, this line is never ran! Get-Content $Path } DoStuff 'some-fake-file.log'
2
u/dodexahedron 1d 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?