Elevated PS script in Jenkins

Question:

I have been trying to run a script from a Windows Jenkins (slave) server. The script is written in PowerShell and requires elevated privileges (such as if one right-clicked on PS and selected run-as-administrator).

Jenkins launches its scripts the following way:

My script fails because it requires elevated privileges. How can I spawn a new elevated-privileged PS process (that does not require clicking because Jenkins can’t do that) that could run my script?

Cheers!

Answer:

The snippet below checks if current process is elevated and if not, it spawns a new, privileged process. It is little tricky to get output of the child powershell process, so I’m using transcript command to capture it. Below you can find my pipeline definition step:

Source:

Elevated PS script in Jenkins by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply