Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell and Jenkins - Execute Powershell Script as another user

I have an environment formed by a Jenkins Server, a couple of Jenkins Slaves (windows) and several remote windows computers all part of the same domain.

I need to run a jenkins job, which executes a powershell snippet consisted of several functions (part of a custom modules loaded on the Jenkins Slave) against other windows remote computers in the same domain (which do not have the modules installed).

These modules needs to run under a specific domain account with permissions to access the remote computers.

If I logon with on any of the jenkins slave (with that specific domain account) everything works fine.

By default, Jenkins executes the job on the slave using the NT authority\system account which, of course, returns me denied access errors.

Question: is there a way to tell Jenkins to execute the Job on the windows slave with another specific Domain Account and not the NT Authority\System one?

Tried already:

  • Invoke-command using credentials: this is not an option since the remote computers do not have the modules

  • Impersonation: tried a couple of functions found on the PS gallery but do not work

like image 755
PSfan Avatar asked Oct 25 '25 06:10

PSfan


1 Answers

You can run powershell as a process in a script and call that from Jenkins. In the script also you can use like:

$username = 'username'
$password = 'password'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username,$securePassword

Start-Process Powershell.exe -Credential $credential

Hope it helps.

like image 171
Ranadip Dutta Avatar answered Oct 26 '25 21:10

Ranadip Dutta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!