Windows, Scheduled Tasks associated with batch files are failing

jdana
jdana used Ask the Experts™
on
I configured a Scheduled Task that resides on my Mac (Parallels v10.3, Windows 8), to run a batch file that calls iPerf. The batch file works fine when I call it manually, but fails when I call it with a scheduled task. Here's the batch file.

iperf3 -c 10.20.2.2 -P 1 -p 5201 -f K -V -J -t 10 --logfile "C:\DSD\iPerf\log\COURTROOM_5TH_FL_%computername%_%date:~10%%date:~4,2%%date:~7,2%-iperf.txt"

Open in new window

In an effort to isolate the issue, I created a simpler batch file. Again, the batch file works fine when I call it manually, but fails when I call it from the scheduled task.

dir /b > dirlist.txt

Open in new window

Finally, I created this batch file. This works when I call it manually and when I call it with the scheduled task.

Notepad.exe

Open in new window

So, the scheduled tasks fail based on the functionality of the batch file. Perhaps it's a Windows security issue. If I change the task to Run whether user is logged in or not, the scheduled task simply hangs.

Security SettingsI activated history in scheduled tasks. History doesn't appear to show me much of use. Here, for example I've configured the scheduled task to run a batch file that doesn't exist, the results look no different.

Scheduled Task History
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018

Commented:
Shouldn't be complicated. Write your command into a batch file, create a new task with that batch file as action. Works here with dir /b > dirlist.txt

Author

Commented:
That's exactly what I did. Doesn't work here with dir /b > dirlist.txt.
Run Scheduled Tasks as Administrator.
My user account is local admin.
Tried dropping tasks in other folders in Task Scheduler.

Other FoldersI'm scratching my head.
Distinguished Expert 2018

Commented:
Describe the error, your task history screenshot does not show errors.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Top Expert 2016

Commented:
a return code of 2 is a file not found error. ergo add the full path to the executable.

Author

Commented:
Progress. I added a PAUSE to the batch file. As before, I can successfully call it from the task scheduler, but check out the extra 1 immediately in front of > dirlist.

Extra "1"Here's the batch file itself. The 1 isn't there. Again, this works properly if I call it manually.

Batch File

Author

Commented:
Solved. Start In = Specifying the directory in which the batch file resides. Start In
Lead SaaS Infrastructure Engineer
Commented:
Change your batch file to run the following:

As command:
"C:\DSD\iPerf\iperf3.exe" -c 10.20.2.2 -P 1 -p 5201 -f K -V -J -t 10 --logfile "C:\DSD\iPerf\log\COURTROOM_5TH_FL_%computername%_%date:~10%%date:~4,2%%date:~7,2%-iperf.txt"

Open in new window


Although, i'd always prefer to run this as a batch so I had a little more control:

:: Name: iperf.cmd
:: Version: 0.1.0
:: Description: Runs iPerf
:: Date: 2016-03-24
@(
	SETLOCAL
	ECHO OFF
	SET "Cmd.Path=%~dp0iperf3.exe"
	SET "Cmd.Options=-c 10.20.2.2 -P 1 -p 5201 -f K -V -J -t 10"
	CALL :GetDateTime
	SET "Log.Folder=%~dp0Log"
)

IF NOT EXIST "%Log.Folder%" (
	MD "%Log.Folder%"
)
SET "Log.File=%Log.Folder%\COURTROOM_5TH_FL_%computername%_%IsoDate%_%IsoTime%_%~n0.log"

CALL :Main

(
	ENDLOCAL
	Exit /b %eLvl%
)


:Main
	
	"%Cmd.Path%" %Cmd.Options% --logfile "%Log.File%"
	
GOTO :EOF


:GetDateTime
	FOR /F "Tokens=1-7 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN ("%DATE% %TIME: =0%") DO (
		FOR /F "Tokens=2-4 Skip=1 Delims=(-)" %%a IN ('ECHO.^| DATE') DO (
			SET "%%~a=%%~A"
			SET "%%~b=%%~B"
			SET "%%~c=%%~C"
			SET /a "#%%~a=1%%~A - (2%%~A-1%%~A)"
			SET /a "#%%~b=1%%~B - (2%%~B-1%%~B)"
			SET /a "#%%~c=1%%~C - (2%%~C-1%%~C)"
			SET "HH=%%~D"
			SET "Mn=%%~E"
			SET "SS=%%~F"
			SET "Ms=%%~G"
			SET /a "#HH=1%%~D - (2%%~D-1%%~D)"
			SET /a "#Mn=1%%~E - (2%%~E-1%%~E)"
			SET /a "#SS=1%%~F - (2%%~F-1%%~F)"
			SET /a "#Ms=1%%~G - (2%%~G-1%%~G)"
			SET /a "#TMinutes=((1%%~D - (2%%~D-1%%~D))*60)+(1%%~E - (2%%~E-1%%~E))"
			SET /a "#TSeconds=((((1%%~D - (2%%~D-1%%~D))*60)+(1%%~E - (2%%~E-1%%~E)))*60)+(1%%~F - (2%%~F-1%%~F))"
		)
	)
	SET "TTime=%HH%.%Mn%.%SS%"
	SET "IsoTime=%HH%.%Mn%.%SS%.%Ms%"
	SET "TDate=%yy%-%mm%-%dd%"
	SET "IsoDate=%yy%-%mm%-%dd%"
GOTO :EOF

Open in new window


Note:

 I assume you will put the iperf command int he same directory as the batch file, cmd allows you to reference the original batch file using %0, and allows the full set of sub options for this so %~dp0 = the drive and path to the batch file.
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer

Commented:
nevermind, I see you actually noticed the issue in the end, but didn't mark the item as closed.

Author

Commented:
Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial