Tagged: scheduled task
May 18, 2013 at 8:11 am #6670
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 #6682
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 #6687
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.
May 20, 2013 at 4:00 pm #6721
Thanks for your helpful suggestions.
I wasn't thinking it was a problem with the script itself since it ran from powershell console with same ID that I was using for the scheduled task.Â Plus, I did see in the event logs and task history that it kept returning with error code zero.
But on your advice, I tried running a different PS script – a simple one to touch the date/timestamp of a file.Â It worked splendidly as a scheduled task.Â I was somewhat happy to see that because it meant things were in place.
On the other hand, it did mean I would have to look more closely at my script.Â The simple idea was to write all the scheduled tasks to a file -Â the attributes,Â last / next run...etc.
We have tended toÂ go crazy with these over time and weÂ wanted toÂ do some cleanup, and stay on top of it going forward.Â So, theÂ ps script does just that – gets the tasks from the "\" folder and writes all the info to a file thatÂ gets processed into a larger data source through some other process.Â Â All well and good – except thatÂ it didn't work.
As I mentioned, on this particular server where I was testing this, I noticedÂ I was running PS version 2.Â So, I went to a different serer where I hadÂ PS 3 andÂ same script worked as a scheduled task!Â So the difference was between PS 2 and PS 3.
Looking more closely at the output fromÂ I got from the PS 3 server I sawÂ what isÂ likely the problem.Â Â
Essentially, I am running a scheduled task to report back on scheduled tasks.Â Somewhere in the middle of that, oneÂ of the tasks it reports back on is "itself".Â I noticed that in output run on a PS 3 machine, it wouldÂ write a statusÂ (message number) that indicated it was currently running.Â I am wondering if maybeÂ the PSÂ 2 environment the idea of reporting on itself was the problem.
I have since been able to replicate this on a few more servers – where they are PS2, the script "runs" but give no output.Â On the PS 3 servers it runs every time.
So yes, thanks to your encouragemen, I put my pride aside and properly looked at my script and found it was the culprit.
I have obviously since rethought how to collect scheduled task info – but that is beyond this post.
August 8, 2013 at 1:53 pm #9241
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.
You must be logged in to reply to this topic.