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

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
Tim CallahanPrincipalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin SaundersDirector of OperationsCommented:
Does the account you're running the task as have permission to the path?  If it's set to the default SYSTEM, change the account to one with appropriate permissions.
0
Tim CallahanPrincipalAuthor Commented:
Yes the account has full permissions to the path. In fact I tried several properly permissioned accounts both from the server login from which I run powershell and for the task.
0
Dustin SaundersDirector of OperationsCommented:
Hmm, so you might try adding a -Credential block with credentials and see if that works for you.  Its odd though that even by using the UNC directly as the account you can't get a result.  What happens if you run it as the same user account with the net use command?

If you run a dummy task that just writes out a text file to the UNC, does it generate the file as expected?
0
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

Tim CallahanPrincipalAuthor Commented:
For NET USE the script is:

   net use X: /d
   net use X: \\myserver\apps
   $Env:Path+= ";X:\runtime"
   X:\myapp.exe
   net use X: /d

This works in PowerShell but in task scheduler the task starts but stays in the Running mode until I end it.

Copying a file works in PowerShell but not in the task. The script is:

    copy-item -path c:\temp\test.txt -Destination \\myserver\apps

There are no messages in the powershell event log.
0
Tim CallahanPrincipalAuthor Commented:
Also, for the first examples I am getting OpCodes of 2 for the action and task in the task history although they say the completed successfully with a return code of 0.
0
Tim CallahanPrincipalAuthor Commented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dustin SaundersDirector of OperationsCommented:
I'd ask that you reconsider awarding points to my initial comment suspecting a permission issue; I did not pursue that as an option only after you had confirmed the security options were properly permissioned.
0
Tim CallahanPrincipalAuthor Commented:
That's reasonable - sure.
1
Tim CallahanPrincipalAuthor Commented:
Found solution. Indicated assisted solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.