PowerShell script does not run program using UNC paths or mapped drive when running from windows 2016 task scheduler

scheduled-taskwindows-server-2016

I am running powershell scripts from the Windows 2016 task scheduler that execute a program using either UNC paths or a drive mapped in the script.

Both variants run fine in powershell but do not work when run from the task.

I know the scripts are executing because of the messages in the mapped drive variant.

I am running powershell on the server using the same user account used in the task.

The application requires DLLs that are located in the ..\runtime folder shown in the scripts.

The UNC script is:

$Env:Path+=";\\myserver\apps\runtime

\\myserver\apps\myapp.exe

This just fails silently.

The mapped drive script is:

If (!(Test-Path X:))

{

  New-PSDrive -Name X -PSProvider FileSystem –Root "\\myserver\apps"

}

$Env:Path+= ";X:\runtime"

X:\myapp.exe

This version generates warnings in the powershell event log of "Provider Health: Could not find the drive 'X:\'. The drive might not be ready or might not be mapped." for both lines where the X: drive location is referenced

Best Answer

Well, this turned out to be major user error. In the task General tab under Run whether user is logged in or not the Do not store password option was checked. This explicitly says that the task will only have access to local resource if this is checked. When I uncheck this and set the task up to run \myserver\apps\myapp.exe and starting in \myserver\apps\runtime\ it works as expected.