Process runs in Powershell Prompt but not as a scheduled task?

2
$taskTrigger1 = New-ScheduledTaskTrigger -Once -At 2:50PM

$taskAction = New-ScheduledTaskAction `
   -Execute "powershell.exe" `
   -Argument "-ExecutionPolicy ByPass -File `"<path>\tiddlywikiIIS_schtask_rsync\backup.ps1`""

$taskName = "Backup Stuff"
$description = "Rsync Backup Stuff"

Register-ScheduledTask `
   -TaskName $taskName `
   -Action $taskAction `
   -Trigger $taskTrigger1 `
   -Description $description

$taskPrinciple = New-ScheduledTaskPrincipal -UserId "Domain\someadmin" -RunLevel Highest
$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8
Set-ScheduledTask -TaskName $taskName -Principal $taskPrinciple -Settings $taskSettings
Set-ScheduledTask -TaskName $taskName -User $taskPrinciple.UserID -Password '******'
# Pull the creds for a user account that has WSL installed on it.
$cred = Get-StoredCredential -Target "some-non-admin-credz"

$psi = New-Object System.Diagnostics.ProcessStartInfo -Property @{
 RedirectStandardError = $true
 RedirectStandardOutput = $true
 UseShellExecute = $False
 UserName = $cred.GetNetworkCredential().Username
 Domain = $cred.GetNetworkCredential().Domain
 Password = $cred.Password
 WorkingDirectory = "C:\windows\"
 FileName = "wsl"
 Arguments = "-d Ubuntu-18.04 -e rsync -av --delete /mnt/c/stuff/ /mnt/o/stuff_bk/"
 WindowStyle = "Hidden"
}

$p = New-Object System.Diagnostics.Process
$p.StartInfo = $psi
$p.WaitForExit()
$p.ExitCode

backup.ps1

I have the code above running in a scheduled task that is running with an (domain?) administrator account.

When it returns from the scheduled task it's $p.ExitCode is -1073741502 but if I run the same code in an administrative powershell prompt run as the same (domain?) administrative account it runs fine.

It makes me thing that it has something to do with the Local Security Policy as I remember there being some sort of "Allow Batch Execution" or "Delegate" or some such privilege in there that prevents any group or user not included from running something as a batch job.

I also found the following log in the Event Viewer:

Okay, so I found a log entry in the System Log with a Source of Application Popup, Event ID 26 which reads:

EventData -> Caption: `wsl.exe - Application Error`

EventData -> Message: `The application was unable to start correctly (0xc0000142).  Click OK to close the application.`
windows
powershell
windows-subsystem-for-linux
privileges
windows-task-scheduler
asked on Stack Overflow Mar 20, 2021 by leeand00 • edited Mar 21, 2021 by leeand00

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0