This topic contains 4 replies, has 4 voices, and was last updated by
May 18, 2013 at 8:11 am #6670ParticipantPoints: 0Rank: Member
I have simple PS script that collects information and writes a file and I want to run this as a scheduled task.
I have execution policy set to remotesigned, and can run this with two different admin users from the powershell console.Â However, when I run this as a scheduled task, it returns back with no errors, but it never really runs (i.e. no file is written)
To debug this, my first step was to get this running simply from the command line (CMD).Â Â Based on numerous examplesÂ I ran this simple command:
Powershell.exe e:\scripts\myscript.ps1Â – which worked straight away!
Is set this up to run as a scheduled task again:
But again, although it runs to succes, it never produces any outuput as a scheduled task
Based on many examples I have tried changing the arugment to include an ampersand
powershell.exe -noexit &
Interestingly enough, when I tried that syntax from a CMD window I got this message –
Ampersand not allowed. The & operator is reserved for future use; use "&" to pa
ss ampersand as a string.
At line:1 char:25
+ powershell.exe -noexit & < <<<Â
Â Â Â + CategoryInfoÂ Â Â Â Â Â Â Â Â : ParserError: (:) , ParentContainsErrorRecordEx
Â Â ception
Â Â Â + FullyQualifiedErrorId : AmpersandNotAllowed
I have also experimented with numerous combinations preceeding my script aruguent such as -nologo -noninteractive â€“Command.
I have also tried the fully qualified path to powershell.exe, and pointing the start in argument to the powershell.ex folder (and even my scripts folder)
After all of this I gave up on running native PS as a scheduled task and saw examples of running it within a CMD, or bat file.Â So, I made a simple one that mimmicked my original working command.Â Simply "Powershell.exe e:\scripts\myscript.ps1".Â
When I ran this in the command window it worked.Â Unfortunately, setting that as a scheduled task – pointing to the CMD file directly (no arguments) it ran with no errros, but didn't write the file (again).
I can run scheduled tasks for othe cmd files, but seemingly not for powershell – either directly (i.e. powershell.exe with a .ps1 argument) or from a cmd file – both of which will run manually.
I am running Windows Servder 2008 R2
NameÂ Â Â Â Â Â Â Â Â Â Â Â : ConsoleHost
VersionÂ Â Â Â Â Â Â Â Â : 2.0
InstanceIdÂ Â Â Â Â Â : d21c127e-3dc9-40aa-b789-ff64703d0546
UIÂ Â Â Â Â Â Â Â Â Â Â Â Â Â : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCultureÂ Â : en-US
CurrentUICulture : en-US
PrivateDataÂ Â Â Â Â : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
RunspaceÂ Â Â Â Â Â Â Â : System.Management.Automation.Runspaces.LocalRunspace
At this point I am stumped and have run out of things to try.Â Any suggestions would be appreciated.
May 19, 2013 at 2:16 am #6682ParticipantPoints: 0Rank: Member
Your script runs if invoked manually but not if invoked as a scheduled task. That suggests that the scheduled task isn't configured correctly. I routinely run powershell scripts from scheduled tasks. I've found the task scheduler to be somewhat tempremental
First things to check
– is your scheduled tasks set to run with highest priority (Elevated)
– does the scheduled task actually start the powershell script? Put a simple test at the top of the script to indicate its started (write to event log is sufficient)
– does the script need to load any modules and is that included
– does the account running the task have the correct permissions.
If you can find those things out and come back with the info I'll see what else comes to mind
May 19, 2013 at 5:24 am #6687ParticipantPoints: 1Rank: Member
In the action dialog box I usually start the task as follows:
"Add arguments (optional)": -command "&'M:\SkyDriveBackup\SkyDrive\BackupScripts\Complete backup.ps1'"
See attached picture it works like aÂ charm for me.
Can't remember if the "run with highest privileges" is nesseary, IÂ just sets itÂ for my tasks.
August 8, 2013 at 1:53 pm #9241ParticipantPoints: 0Rank: Member
Hi, I am experiencing what Jeff described above..
I have simple PS script that collects information and writes a file and I want to run this as a scheduled task. Script is running on a Win2008 SP2 Server with Powershell v2 and the IIS Snap-in.
I have execution policy set to remotesigned, and can run this from the powershell console. However, when I run this as a scheduled task, it returns back with no errors, but it partially runs as only some of the output doesn't get produced.. The script starts, writes some lines to a file and then emails me the output.
The command that fails to write output (when scheduled) to file is from the results of a GET-PROCESS command.
Thanks for any suggestions or hints.
The topic ‘Unable to run PS script as Scheduled Task’ is closed to new replies.