I am having a problem with portions of powershell scripts not executing when the script runs from a scheduled task. When manually executed, it operates just fine.
For example, I have the following in a script:
add-content c:\some_folder\output.txt "howdy"
c:\windows\system32\notepad.exe
When I manually run the script, both commands execute. When the scheudled task runs, the first command executes, but the second does not. This is an over-simplified example, but I have more complex scripts where I get similar behavior...the script does what it is supposed to when run manually but parts do not run when run from a scheduled task.
The scheduled task is set to run as local administrator with the "Run with highest privileges" option selected.
Any ideas?
The problem with the example you give is that
notepad.exe
requires an interactive session in order to display the UI. When you run a scheduled task there is no interactive session to host the UI.Are you working in a domain?
Try amending the security permissions of \system32\cmd.exe and add the builtin account BATCH with full control.
Does this script use any network resources? Local admin won't do the business if you are calling stuff across the network.
When the scheduled task runs, whats the exit code? 0x0 is an apparent success. 0x1 and 0x2 are common. Have a look at c:\windows\Schedlgu.txt, which will give you some more information.
Locate the CMD.EXE, right click and 'Run As' the account that you are scheduling under. Navigate to the script location in this command prompt and run the script from there. Make sure you've stripped @ECHO OFF from the top(if you have used it) so you can see everything.
That should allow you to see where it fails.
EDIT : Also - adding an echo line at each stage of the script can help. For example I often put an 'Echo Starting the script >>checking.txt' at the beginning when I'm having problems, then something similar further down, at each stage or after any major operation. You can then see where the last execution occured, it can also be helpful, if you are relying on variables to put them in the output too, so add a 'var is currently %var%' in there.
Can you paste up the full script?