Jenkins supports Groovy script very well, so you can use Groovy to do a lot of tricky and interesting things.
First, if you have a Groovy script, and need to verify it if it works well in Jenkins, you can go to
Jenkins--> System management --> Script Command Line
And paste your script here, then click Run button to check it out.