Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to run a simple PS1 powershell script from the Task Scheduler. The task's Actions settings are Program: powershell and Add arguments: -executionpolicy bypass -file C:\Users\Robin\Documents\script.PS1.

The objective script.PS1 is accessed ok but a line within it (a call to another script: Rscript Z:\rscript.R) fails with the message:

Fatal error: cannot open file Rscript Z:\rscript.R
'rscript.R': no such file or directory

If I run the same line manually in Powershell it works ok. Moreover the following runs fine as a Run command: powershell -executionpolicy bypass -file C:\Users\Robin\Documents\script.PS1, which suggests the problem is in Task Scheduler. The task is set to run with highest privileges. I'm stumped.

Very grateful for assistance.

share|improve this question
    
I wonder if its something to do with the 2nd script residing on a networked drive.. –  geotheory Aug 8 '13 at 12:38
    
Try creating a batch script to run powershell and call your script, then schedule the batch file. Let me know if it behaves the same way. –  Moses Aug 8 '13 at 12:59
    
OK I've tried it using powershell.exe -file "C:\Users\Robin\Documents\script.ps1 and with Rscript Z:\rscript.R. In both cases the batch file works ok run manually but fails when called from Task Scheduler (same error message). –  geotheory Aug 8 '13 at 13:47

4 Answers 4

up vote 2 down vote accepted

I'm assuming that Z: is a network drive.

Scheduled jobs are (usually) run under a System account. Network drives aren't usually mapped for System. Even if you change it to run under your credentials (or any specific user credential), you just can't count on network shares being accessible. You'll need to either:

  1. Run the second script using a UNC path (and ensure that the system account has access to the UNC path)
  2. Map the network drive early on in your script (in PowerShell 3 you could use New-PSDrive, but), the simplest way is to run the NET USE Z: \\SERVER\SHARE command. Of course, you might need to add user credentials: password /USER:domain\user and you could also try adding /SAVECRED /PERSISTENT:YES and running that command once and hoping it sticks (but I'd still test-path and remap it if that's an option).
  3. Safest option: make a copy of the file locally.
share|improve this answer
    
Thanks @Jaykul that cracked it :) –  geotheory Aug 8 '13 at 15:09

Please check this one out from stackoverflow, seems pretty similar case: How can I convince powershell (run through task scheduler) to find my network drive?

share|improve this answer
    
The accepted answer on that page fails for me. When mapping the drive I get '$net' is not recognized as an internal or external command, operable program or batch file. And the environmental variables seem ok re system32 folder.. –  geotheory Aug 8 '13 at 14:44
    
Can i just say: $net.mappWHATTHEHECK? Just use net.exe –  Jaykul Aug 8 '13 at 14:46
    
So what would the correct syntax? I'm not familiar with NET stuff. @zagrimsan is right about the network drive - the scheduler works fine on local files. –  geotheory Aug 8 '13 at 15:01
    
Too bad I'm not familiar with PS but I'd have assumed it to work. The obvious alternate way would be to use UNC paths directly without mapping the drive, ie. using \\server\share\dir as proposed in the 2nd answer in the SO topic I linked. –  zagrimsan Aug 8 '13 at 15:22

The command you tried from a batch file was wrong.

Try this command, and remember to keep the quotations.

powershell.exe -file ". 'C:\Users\Robin\Documents\script.ps1'" 
share|improve this answer

It seems that Task Scheduler doesn't manage correctly domain users validation... When you try to execute script manually user is identified as "user@domain.com" When you try to execute it by Task Scheduler(only offline mode) user is identified as "user@" without domain. So in case script tries to copy or make any network operations that requires user validation results in no such file or not legal path errors etc...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.