[GROOVY] Run from console

Maciej Olszewski July 11, 2017

Hello,

I am using a lot of groovy scripts lately but i need to change date everytime, my question is can i run groovy scripts from console?
for example:
I have script deactivateUser where i need to change login of user, and i want to run it like that from console
/script/deactivateUser.groovy arg1

where arg1 is username :)
I hope this is possible! If yes can you show me how to use arg1 in script and how to run it from console?

Thanks in advance!
Maciej O.

6 answers

0 votes
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2017

The easiest thing to do is to run it using the Script Console under Admin -> Script Console.

For example if you place your DeativateUser.groovy script under your script root (by default <jira-home>/scripts) so if you have it under "<jira-home>/scripts/userscripts/DeativateUser.groovy"

Your DeactivateUser.groovy script should have at the top:

package userscripts

Then the script you run in the Script Console would be:

 

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def scriptRunner = ScriptRunnerImpl.getScriptRunner()

// this is relative to the script roots
def relativeFilePath = "userscripts/DeactivateUser.groovy"

// change to your username you want to deactivate
def username = "user1"

scriptRunner.runFileAsScript(new File(relativeFilePath), [username: username])

You could even expand your script to take a list of usernames rather than just one.

Please see the docs here for information on script roots to double check you have your script in the correct place.

Hope this helps,

Thanks,
Adam

0 votes
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2017

The easiest thing to do is to run it using the Script Console under Admin -> Script Console.

For example if you place your DeativateUser.groovy script under your script root (by default <jira-home>/scripts) so if you have it under "<jira-home>/scripts/userscripts/DeativateUser.groovy"

Your DeactivateUser.groovy script should have at the top:

package userscripts

Then the script you run in the Script Console would be:

 

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def scriptRunner = ScriptRunnerImpl.getScriptRunner()

// this is relative to the script roots
def relativeFilePath = "userscripts/DeactivateUser.groovy"

// change to your username you want to deactivate
def username = "user1"

scriptRunner.runFileAsScript(new File(relativeFilePath), [username: username])

You could even expand your script to take a list of usernames rather than just one.

Please see the docs here for information on script roots to double check you have your script in the correct place.

Hope this helps,

Thanks,
Adam

0 votes
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2017

The easiest thing to do is to run it using the Script Console under Admin -> Script Console.

For example if you place your DeativateUser.groovy script under your script root (by default <jira-home>/scripts) so if you have it under "<jira-home>/scripts/userscripts/DeativateUser.groovy"

Your DeactivateUser.groovy script should have at the top:

package userscripts

Then the script you run in the Script Console would be:

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def scriptRunner = ScriptRunnerImpl.getScriptRunner()

// this is relative to the script roots def relativeFilePath = "userscripts/DeactivateUser.groovy"
// change to your username you want to deactivate def username = "user1" scriptRunner.runFileAsScript(new File(relativeFilePath), [username: username])

You could even expand your script to take a list of usernames rather than just one.

Please see the docs here for information on script roots to double check you have your script in the correct place.

Hope this helps,

Thanks,
Adam

0 votes
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2017

Hi Maciej,

The easiest thing to do is to run it using the Script Console under Admin -> Script Console.

For example if you place your DeativateUser.groovy script under your script root (by default <jira-home>/scripts) so if you have it under "<jira-home>/scripts/userscripts/DeativateUser.groovy"

Your DeactivateUser.groovy script should have at the top:

package userscripts

Then the script you run in the Script Console would be:

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def scriptRunner = ScriptRunnerImpl.getScriptRunner()

// this is relative to the script roots def relativeFilePath = "userscripts/DeactivateUser.groovy"
// change to your username you want to deactivate def username = "user1" scriptRunner.runFileAsScript(new File(relativeFilePath), [username: username])

You could even expand your script to take a list of usernames rather than just one.

Please see the docs here for information on script roots to double check you have your script in the correct place.

Hope this helps,

Thanks,
Adam

0 votes
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2017

Hi Maciej,

The easiest thing to do is to run it using the Script Console under Admin -> Script Console.

For example if you place your DeativateUser.groovy script under your script root (by default <jira-home>/scripts) so if you have it under "<jira-home>/scripts/userscripts/DeativateUser.groovy"

Your DeactivateUser.groovy script should have at the top:

package userscripts

Then the script you run in the Script Console would be: 

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def scriptRunner = ScriptRunnerImpl.getScriptRunner()

// this is relative to the script roots def relativeFilePath = "userscripts/DeactivateUser.groovy"
// change to your username you want to deactivate def username = "user1" scriptRunner.runFileAsScript(new File(relativeFilePath), [username: username])

You could even expand your script to take a list of usernames rather than just one.

Please see the docs here for information on script roots to double check you have your script in the correct place.

Hope this helps,

Thanks,
Adam

0 votes
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 11, 2017

Hi Maciej,

The easiest thing to do is to run it using the Script Console under Admin -> Script Console.

For example if you place your DeativateUser.groovy script under your script root (by default <jira-home>/scripts) so if you have it under "<jira-home>/scripts/userscripts/DeativateUser.groovy"

Your DeactivateUser.groovy script should have at the top:

package userscripts

Then the script you run in the Script Console would be:

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def scriptRunner = ScriptRunnerImpl.getScriptRunner()

// this is relative to the script roots def relativeFilePath = "userscripts/DeactivateUser.groovy"
// change to your username you want to deactivate def username = "user1" scriptRunner.runFileAsScript(new File(relativeFilePath), [username: username])

You could even expand your script to take a list of usernames rather than just one.

Please see the docs here for information on script roots to double check you have your script in the correct place.

Hope this helps,

Thanks,
Adam

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events