2

I am new to Gradle, so bear with me.. I am simply trying to call a .ps1 file to execute with gradle. How would I go about setting up a build.gradle file to execute the .ps1 file within the same directory? Thanks in advance!

Stacker
  • 137
  • 3
  • 12

1 Answers1

0

You can use gradle Exec

Example:

task execPs(type:Exec) {
   commandLine 'cmd', '/c', 'Powershell  -File  sample.ps1'
}
// add this task to your build.gradle file and execute gradle execPs
// You can use a different name for this task 
// You can add a dependency to include this task as part of your normal build. (like below) 
// build.finalizedBy(execPs)
// with the above command gradle build will call your task at the end of build
miskender
  • 7,460
  • 1
  • 19
  • 23
  • This works great! How could I run the powershell script as admin? – Stacker Oct 24 '18 at 13:06
  • @Stacker create another ps1, which calls your ps script as admin as described here. https://stackoverflow.com/questions/7690994/powershell-running-a-command-as-administrator And call this new ps1 from gradle. This would bring up a adminastator popup depending on your windows config. – miskender Oct 24 '18 at 16:06
  • One more question.. How can I pass an argument to my powershell script from within gradle. "Powershell -file sample.ps1 argument" does not work – Stacker Oct 29 '18 at 12:51
  • But when called with -File option, powershell always returns 0 as exit code even though the script failed. – Safa Kadir Oct 31 '22 at 13:11