Forum Discussion
KarinKamome
Apr 21, 2020Copper Contributor
ErrorActionPrefernce doesn't work when running PowerShell from batch.
When the following script is executed from batch, even if an exception occurs in "Disable-ScheduledTask", it will not be caught and the process will continue.
[Script file]
$ErrorActionPreference = "Stop"
function DoMain
{
$taskName = "test"
try
{
Disable-ScheduledTask -TaskName $taskName
}
catch[Exception]
{
[Console]::WriteLine("$taskName is not found.")
}
}
DoMain
exit
[Batch file]
powershell -NoProfile -ExecutionPolicy Unrestricted <script file name>
However, if you delete the description of "$ErrorActionPreference = "Stop"" and add "Disable-ScheduledTask" to the option of "-ErrorAction Stop", the Catch clause will be processed.
Also, if you run the script directly in Powershell ISE, the Catch clause will be processed.
If you know anything, please help me.
Best regards.
No RepliesBe the first to reply