0

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.

11
  • 1
    powershell can convert JSON files to CSV. have you tried looking into the CSV & JSON cmdlets in PoSh? Commented Jul 20, 2019 at 10:31
  • 1
    Can you paste the content of the bat file? Commented Jul 20, 2019 at 12:27
  • @Lee_Dailey i think the JSON file converter I'm using is specifically formatted for our needs. This is the reason why I needed to run that .bat file to convert such. Commented Jul 21, 2019 at 0:14
  • @Moerwald I have updated the question to include the contents of the bat file. Commented Jul 21, 2019 at 0:15
  • 1
    Sounds like a permissions issue (check what user is setup in Task Scheduler to run the process)... but it seems really weird, you're using PS to run a BAT, to call a Java library, to accomplish something PS already does natively. Commented Jul 21, 2019 at 0:19

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

1 Comment

I tried doing this but same results. For some reason, the bat file was not executed when I do this in Task Scheduler.
0

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.

1 Comment

This isn't the case, @Moerwald. The task completed successfully based on its Status and yet I can't see the generated CSV. I was under the same assumption as you at first but I'm still getting the same result.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.