I'm running Windows 10 Pro. I have a python script that calls a PowerShell script like this:
p = subprocess.Popen(
[
"C:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe",
"D:\\src\\powershell\\Script-Name.ps1",
"-ScriptParam1",
] + additional_script_params,
stdout=log_file,
stderr=log_file,
)
p.communicate()
The PowerShell script takes filename parameters for images and converts them to JPG. Inside there is a chunk of code like this:
...
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$runtimeMethods = [System.WindowsRuntimeSystemExtensions].GetMethods()
$asTaskGeneric = ($runtimeMethods | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function AwaitOperation ($WinRtTask, $ResultType)
{
$asTaskSpecific = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTaskSpecific.Invoke($null, @($WinRtTask))
$netTask.Wait() | Out-Null
$netTask.Result
}
$decoder = AwaitOperation ([Windows.Graphics.Imaging.BitmapDecoder]::CreateAsync($inputStream)) ([Windows.Graphics.Imaging.BitmapDecoder])
$bitmap = AwaitOperation ($decoder.GetSoftwareBitmapAsync()) ([Windows.Graphics.Imaging.SoftwareBitmap])
...
If I run the Script-Name.ps1 script manually from a PowerShell console, it works fine. It also works if I run the python script manually from a PowerShell console. However, if I run the above python script as a Windows scheduled task via Task Scheduler, the last line in the PowerShell snippet above fails with the following error:
System.Management.Automation.MethodInvocationException: Exception calling "Wait" with "0" argument(s): "One or more errors occurred." ---> System.AggregateException: One or more errors occurred. ---> System.Exception: A specified logon session does not exist. It may already have been terminated. (Exception from HRESULT: 0x80070520)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at CallSite.Target(Closure , CallSite , Object )
--- End of inner exception stack trace ---
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
The wait call throws because the underlying async task has failed with a "specified logon session does not exist" error. Strange that the script gets this far before failing, but OK.
I've searched all over for how to resolve this error, but any answer I've found is either related to remote auth (does not apply here), or references this old scheduled task issue (I already have that security setting disabled, and the scheduled task runs fine other than this PowerShell script).
I'm wondering how I can get around this PowerShell auth issue in a scheduled task.
User contributions licensed under CC BY-SA 3.0