Mari yah
Mari yah

Reputation: 203

Executing a .bat file from PowerShell is not working

I have a working PowerShell script that generates JSON file and converts them to CSV. Running well when executed manually via ISE. However, I noticed that when I execute the script via Task Scheduler, the generation of JSON files are working correctly but somehow skips the part where it converts to CSV. I'm using a .bat file that was given to me to convert JSON to CSV and I just call that .bat on my PS script.

I have read somewhere that this could be because I'm trying to start a process without an interactive session and that this could be fixed by checking "Run only when the user is logged on" but I'm still getting the same result.

$path = "C:\Apps\ActiveMQanalysis\ConvertJsonFiletoCSV.bat"

Start-Process -FilePath $path

Start-Sleep -Seconds 5

Edit: This is the content of the batch file:

"C:\Program Files\Java\jdk1.8.0_181\bin\java" -cp lib* de.znt.activeMqAnalysis.BrokerStatistics C:\Apps\brokerResults\

Edit: As pointed out by most users here, I did add some logging on the .bat file to see where the error is coming from. As it turned out, it is giving me an error that it cant find or load main class de.znt.activeMqAnalysis.BrokerStatistics.

Upvotes: 0

Views: 747

Answers (2)

js2010
js2010

Reputation: 27418

Start-Process runs things in the background by default. Why don't you run it directly?

C:\Apps\ActiveMQanalysis\ConvertJsonFiletoCSV.bat

Or with a variable you can use the call operator:

& $path

Upvotes: 1

Moerwald
Moerwald

Reputation: 11254

Maybe the bat file takes longer than 5 seconds. Your call to Start-Process immediately returns. You can add the -Wait switch to add blocking behaviour to Start-Process. Change the code to:

$process = Start-Process -FilePath 'C:\Apps\ActiveMQanalysis\ConvertJsonFiletoCSV.bat' -Wait -Passthru
$process.ExitCode

-PassThru returns a process object that will give you detailed information like the exit code of the process. Further information can be found in the Start-Process documentation.

Upvotes: 0

Related Questions