Have PowerShell script in resource folder and want to use this script in shared library .groovy script which is under vars folder. Is it there a way to do so??
Asked
Active
Viewed 2,348 times
2 Answers
5
To run a PS script from the resources dir of your shared library, assuming psScript1.ps1
is in your-shared-library/resources/
:
powershell "${libraryResource 'psScript1.ps1'}"
This works great, however, it doesn't let you pass in parameters. I can't seem to find a one-line way to do it.
Building on @christopher's answer, here's a two-line solution:
writeFile file: 'tempFile.ps1', text: "${libraryResource 'psScript1.ps1'}"
powershell './tempFile.ps1 -yourParam $yourParam'
In my testing, I found the ./
to start the temp file path to be required.

Max Cascone
- 648
- 9
- 25
-
2To add to this, if you need to dot-source any scripts in your other scripts, I wasn't able to do it directly. I have to put them in the workspace first, even though they're in the `/resources` dir. ```helperFiles = ['list', 'of', 'files'] helperFiles.each { writeFile file: it, text: "${libraryResource it}" } ``` – Max Cascone Feb 09 '21 at 20:46
-
Works under Linux systems too - just replace `.ps1` with `.sh`, `powershell` with `sh` and skip the `-yourParam` switch (`sh "./tempFile.sh ${yourParam}"`). – mirekphd Aug 12 '23 at 19:20
3
You can use libraryResource(file-in-resources)
(docs) to load a file from the library resources as a text. That you can write to your workspace with writeFile
(docs) and execute.
As the library is checked out somewhere next to your workspace you can maybe execute it directly.

Christopher
- 1,103
- 1
- 6
- 18
-
`you can maybe execute it directly` It's theoretically possible to do this if you know where the library files are, but it's intentionally obfuscated by Jenkins. I could be wrong, but I don't think it's reliable to try to access them directly in code. They are physically there if you go to the `workspaces` dir where all the jobs live, but I don't think it's guaranteed to be the same structure every time. Hence, the native functionality of `libraryResource` is guaranteed to give you the file. – Max Cascone Jun 15 '22 at 15:45