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:
Program:= Powershell.exe
Argument:Â e:\scripts\myscript.ps1Â
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 & e:\scripts\myscript.ps1Â
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 & <<<<Â
e:\scripts\taskquery.ps1
   + 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
PS Version:
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.
Thanks,
Jeff Henke