Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a Powershell script that runs manually using the Powershell ISE; However, when run as a scheduled task using an administrator's credentials the task does not run with the expected results.

The script:

$request=new-object System.Net.WebClient $request.DownloadFile("...url...", "C:\path\to\file.csv")

The administrator user has Full Control of both the script and the folder it is writing to. The url exists and responds in a reasonable time (<1s).

If I run the task manually the status is 0x41301 ("Currently Running") until I eventually end it. I have set the task up using both of these methods:

  1. Start a Program: C:\path\to\PS.PS1
  2. Start a Program: C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe with additional options -noninteractive -command "C:\path\to\PS.PS1"

Using option 1, the task history shows it has opened an instance of notepad.exe but never terminates it. Using option 2 it completes the task but doesn't download / create the file.

I have used Set-ExecutionPolicy Unrestricted as this is not a signed script.

Any ideas?

share|improve this question
2  
Option 1 opens notepad.exe as that's the default action for the .PS1 file type -- that's what happens when you double-click a .PS1. Try these settings with option 2: "Start a program" "C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" and "Add arguments" "C:\path\to\PS.PS1". You shouldn't need to worry about -noninteractive when running from a Scheduled Task unless you need it to interact with the desktop. –  jscott May 4 '13 at 10:27
1  
and you dont want the -command flag you just specify the file: technet.microsoft.com/en-us/library/ee176949.aspx –  Brock Hensley May 5 '13 at 19:15
    
If you want to put your comment as an answer I'll accept it. I didn't use the -command flag, but still had issues. Turns out the script's path, even in quotes, can NOT have spaces. Scheduled Tasks wasn't reporting that as the fault, I ran it from the command line and it returned that as the error. –  Aeisor May 9 '13 at 18:57
add comment

3 Answers

As you are running a script file instead of a script block, the parameter that you should use is -File.

Additionally, setting the execution policy using the Set-ExecutionPolicy cmdlet does not guarantee that the script is executed in that context. The effective execution policy might be overwritten by group policy (GPO). To force this on on the execution of the file use the -ExecutionPolicy parameter

The command that you are looking for is as follows

powershell.exe -ExecutionPolicy Unrestricted -NonInteractive -NoProfile -File c:\path\ps.ps1

More information of these parameters can be found here.

share|improve this answer
add comment

Can you confirm your execution policy for all scopes, by:

Get-ExecutionPolicy -List

If you set the LocalMachine policy to RemoteSigned, you should be able to have a task definition like:

Start In: C:\Windows\System32\WindowsPowerShell\v1.0

Run: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -File c:\path\to\ps.ps1

share|improve this answer
add comment

The script file you are trying to run is not a natively trusted file. Use an ampersand before the file path to run the command.

Powershell.exe -ExecutionPolicy Unrestricted -Command { & 'C:\path\to\PS.PS1' }

Other Powershell startup params can be found here: http://technet.microsoft.com/en-us/library/hh847736.aspx

share|improve this answer
    
"here"? Where? Please edit your answer to include the right link. –  Deer Hunter Jun 21 '13 at 19:00
    
Sorry about that, corrected it with a link. –  jnich91 Jun 25 '13 at 19:54
add comment

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.