Link to home
Start Free TrialLog in
Avatar of Roberto Madro R.
Roberto Madro R.Flag for United States of America

asked on

Windows schedule unable to run a ps1 script

I got a PS1 script that's inside a batch job, that PS1 script runs a sql script, I've scheduled a batch job to run that PS1 using Windows server schedule, the scheduler runs and gives the timestamp as having run, when in actuality the the batch (whether it ran or not) doesn't execute the PS1 script, thus not executing the SQL script.

Now, if I was to double click the batch job manually, it runs the PS1 script with no problem, so the problem is something to do with running this particular batch job via the scheduler.

Please advise.
Thx
Avatar of Mauro Cazabonnet
Mauro Cazabonnet
Flag of United States of America image

Looks like your script run fine interactively. What are the settings for the scheduled task. Are you specifying an account to run the code non-interactively?
Avatar of Roberto Madro R.

ASKER

The account by which this task runs under, is an elevated / super user Account.
Ok without seeing the code. Not all code can run as expected in non-interactive mode
Try setting to 'Run only when user is logged on' and test
This is the batch that runs the PS1,

@echo off
start /wait %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\Region\Customer\CustomerActivities.ps1"
Since you kicking off a powershell script no need for cmd batch call

Under actions
Program/script use the following

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Next
Add arguments (optional) use the following
C:\Region\Customer\CustomerActivities.ps1

Next
Start in (optional)
use: C:\Region\Customer<--- not need unless logging is occuring and need to grab current workdir
I just tried your suggestion, changed the job to "Run with the highest privileges" and "Run only when user is logged on", ran it, it gave the impression of doing something, it launched couple of command windows in the background, they stayed open for couple of seconds, the scheduler's  "Next Run Time" & "Last Run Time" changed accordingly, but in actuality nothing happened.
Ok, I would need to see what the powershell script is doing
task scheduler has limits such as

1. There is no active Shell (explorer.exe)
2. If a process/service tries to display a message box, the task will not complete

You can read further here

https://blogs.technet.microsoft.com/askperf/2015/02/18/help-my-scheduled-task-does-not-run/

M
Shaun; I set it up this way;

Program/scripts: PowerShell.exe
Add arguments (optional): -ExecutionPolicy Bypass -File "%Region%\Customer\CustomerActivities.ps1"


The PS1 script directory:  C:\Region\Customer\CustomerActivities.ps1

To no avail
Mauro;

The issue is, if I was to double click the batch file, it runs the PS1 script and generates the data I need, so the only problematic area is that I can't do the same using the scheduler.
correct, the powershell code your running, are there interactive items that the scheduler would not like?
drive mappings can be problematic
Add -NoExit to the PowerShell call, change the user to your current logged in user, and run the task. You should see the PS console window now. Hopefully with some error messages.
ASKER CERTIFIED SOLUTION
Avatar of Roberto Madro R.
Roberto Madro R.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So in fact you didn't change anything but re-set the task action. Doesn't explain anything, sadly.