Created
May 25, 2017 00:29
-
-
Save MiguelTVMS/02c85fb95181d01ca2997346e768bbbe to your computer and use it in GitHub Desktop.
Groovy script to execute powershell commands on Jenkins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def exec(Map map = [:], command){ | |
def debug = map.debug ?: false | |
if (debug) echo "[DEBUG] powershell method called with parameter: \n $command" | |
def returnFileName = new Date().format("yyyyMMddHHmmssSSS") | |
if (debug) echo "[DEBUG] The return file name will is $returnFileName" | |
def pwCommand = "powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -Command \"$command\" > $returnFileName" | |
if(debug) echo "[DEBUG] The following command will be executed: \n $pwCommand" | |
bat pwCommand | |
if(debug) echo "[DEBUG] Reading the file $returnFileName" | |
def pwret = readFile "$returnFileName" | |
pwret = pwret.trim() | |
if(debug) echo "[DEBUG] Deleting the file $returnFileName" | |
bat "del $returnFileName" | |
return pwret | |
} | |
return this |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instructions
Executing
To run this script you can load the external file and call the exec method.
To debug, you can use
To pass double quotes (") to the method you must change (") to (\")