0

I have a batch file that calls a few commands and ultimately runs a powershell script. It runs fine manually, however in task scheduler, I can see that it completes some of the actions but doesn't actually run the powershell script (last command in the bat file) The script is created/overwritten by a java file where it sends out emails. Everything updates and necessary but ultimately the emails are never sent. When I click on the bat file in Win explorer, it works as expected.

@echo off
set Pathname="C:\Users\administrator\Documents\EmailNotification\EmailNotificationJava"
cd %Pathname%
REM ECHO Directory: C:\Users\administrator\Documents\Notification
CALL IndividualCMDCommands.bat
set Pathname="C:\Users\administrator\Documents\EmailNotification\EmailNotificationJava\src"
cd %Pathname%
XCOPY C:\Users\administrator\Documents\EmailNotification\EmailNotificationJava\*.txt C:\Users\administrator\Documents\EmailNotification\EmailNotificationJava\src /Y
javac -cp .;C:\Users\administrator\Documents\EmailNotification\EmailNotificationJava\joda-time-2.9.3\joda-time-2.9.3.jar ParseInfo.java
java -cp .;C:\Users\administrator\Documents\EmailNotification\EmailNotificationJava\joda-time-2.9.3\joda-time-2.9.3.jar ParseInfo 
ECHO ParseInfo
REM set Pathname="C:\Users\administrator\Documents\Notification"
REM cd %Pathname%
ECHO Powershell
REM SET ThisScriptsDirectory="C:\Users\administrator\Documents\EmailNotification\EmailNotificationJava\src"
SET PowerShellScriptPath=%TC:\Users\administrator\Documents\EmailNotification\EmailNotificationJava\PSCMD.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";
ECHO Complete
  • 3
    Why use a batch file at all? Just use PowerShell for the entire script. – Bill_Stewart Jun 06 '16 at 17:31
  • Each time this script is run, the Powershell Script is overwritten. The reason for using the powershell script is to keep the email commands in a separate file that I could alter if need be. So I would like to keep that part in its own file. – Peyton Puckett Jun 07 '16 at 14:00
  • I would encourage an alternative approach that doesn't require this -- you don't need a batch file. – Bill_Stewart Jun 07 '16 at 15:18

2 Answers2

0

It has likely something to do with the executing user account. If you can configure that in your task scheduler, have your bat file executed under your own account or "the interactive user". Then it should work.

How are the emails sent? Using Outlook perhaps? Outlook has some security constraints when automated. If it is opened by user A it will not allow automation by user B.

Martin Maat
  • 714
  • 4
  • 23
  • The emails are being sent by outlook. Although I'm under the same user account when I created the files and the task. In task scheduler, I set the task run with highest privileges and execute it under the same user. – Peyton Puckett Jun 07 '16 at 13:58
  • Elevated privileges are still seen as a different user by Outlook. If you close outlook before running the script it should not be an issue though. Try running PowerShell ISE from the sheduler instead and then debug your script to see where it fails. – Martin Maat Jun 07 '16 at 14:33
-1

Please try below command to run powershell script. C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Bypass -file "& '%PowerShellScriptPath%'"