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.