'Powershell script fails after invoking 'Start-Transcript'
I have a simple powershell script that checks network availability by making routine checks that bbc.co.uk is available.
The script has been working fine (and logging without issue), for many weeks, but has recently stopped working. Basically, the script gets to Start-Transcript, creates the logfile, makes an initial entry into the log file, and then quits (i.e. the powershell console blinks out of existence, no errors, no messages, no warnings, and nothing further in the log file.)
If I comment out the Start-Transcript line then it works fine (minus the logging). the only difference I'm aware of is that last time it was working on I was using PSVersion: 5.1.19041.1320 which has since been upgraded to PSVersion: 5.1.22000.653.
Here it is.
Set-PSDebug -Strict
$ErrorActionPreference = "stop"
$DebugPreference = "Continue"
Write-Debug "Debug is set on"
Write-Debug "switching on logging to $($($PSCommandPath).Replace(“ps1“,“$pid“)+".log")...."
Start-Transcript -Append $($($PSCommandPath).Replace(“ps1“,“$pid“)+".log") # Use the name of this script, plus the pid as the name of the file to log to
Write-Debug "logging on"
[bool]$result=$true
[bool]$prevResult=$false
function fnTestForCtrlC
{
## Stops Ctrl+C from exiting this function
[console]::TreatControlCAsInput = $true
if ($Host.UI.RawUI.KeyAvailable -and (3 -eq [int]$Host.UI.RawUI.ReadKey("AllowCtrlC,IncludeKeyUp,NoEcho").Character))
{
Write-Debug ""
Write-Debug "fnTestForCtrlC() - @<$(get-date -Format "HH:mm:ssL") ($((get-date).ToUniversalTime().ToString("HH:mm:ssZ")))> Ctrl+C Pressed! Ending procesing. Attempting a tidy shutdown"
Stop-Transcript # Stop logging
exit # quit
}
}
while($true)
{
fnTestForCtrlC
try
{
$result = $(Test-Connection -ComputerName www.bbc.co.uk -Quiet)
}
catch
{
$ErrMess = $_.Exception.Message
Write-Debug "Test-connecion raised following following error=<$ErrMess>. "
}
fnTestForCtrlC
if ($result -ne $prevResult)
{
if ($result -eq $true)
{
Write-Host -NoNewline " @<$(get-date -Format 'HH:mm:ss')> network Up ^"
}
else
{
Write-Host -NoNewline " @<$(get-date -Format 'HH:mm:ss')> network Down v"
}
}
else
{
if ($result -eq $true)
{
Write-Host -NoNewline "^"
}
else
{
Write-Host -NoNewline "v"
}
}
$prevResult = $result
Start-Sleep 1
fnTestForCtrlC
Start-Sleep 1
}
The OS is windows 11, the log file looks like this....
**********************
Windows PowerShell transcript start
Start time: 20220516163045
Username: PRIVATESERVER\Anon
RunAs User: PRIVATESERVER\Anon
Configuration Name:
Machine: PRIVATESERVER (Microsoft Windows NT 10.0.22000.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -command &c:\pwrShl\netTest.ps1
Process ID: 4676
PSVersion: 5.1.22000.653
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.22000.653
BuildVersion: 10.0.22000.653
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Ok, think I've fixed it. For historic reasons (i.e. I never got round to changing it) the shortcut I was using to run this script had this target
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -command "&c:\pwrShl\netTest.ps1"
If I change it to this it works...
"%SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe " -NoExit -command "&c:\pwrShl\netTest.ps1"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
