Windows, Scheduled Tasks associated with batch files are failing

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
jdanaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

McKnifeCommented:
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
jdanaAuthor 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.
McKnifeCommented:
Describe the error, your task history screenshot does not show errors.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

David Johnson, CD, MVPRetiredCommented:
a return code of 2 is a file not found error. ergo add the full path to the executable.
jdanaAuthor 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
jdanaAuthor Commented:
Solved. Start In = Specifying the directory in which the batch file resides. Start In
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ben Personick (Previously QCubed)Lead SaaS Infrastructure EngineerCommented:
nevermind, I see you actually noticed the issue in the end, but didn't mark the item as closed.
jdanaAuthor Commented:
Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.