5

I pass in credentials to the script via the env injector (note this works for me with Invoke-Command) and try to run Start-Job but jenkins doesn't like it:

$user = $ENV:user
$pass = $ENV:pass

write-output (cat env:username)
write-output (cat env:user)
write-output (cat env:pass)

$pass  = $pass | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($user), $pass

Start-Job -Credential $cred -ScriptBlock {'test'}

write-output (get-job | Receive-Job)
get-job | remove-job

This is the error I get (confirmed username and password are correct, when I run this script from the console with the same creds it works)

Started by user ME

[EnvInject] - Loading node environment variables.
Building in workspace C:\Program Files (x86)\Jenkins\jobs\myjob\workspace
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Windows\TEMP\hudson1723222179976241861.ps1'"
MYJENKINSSRV$
correctdomain\correctuser
correctPassword

Id     Name            PSJobTypeName   State         HasMoreData     Location  
--     ----            -------------   -----         -----------     --------  
1      Job1            BackgroundJob   Failed        False           localhost 
[localhost] An error occurred while starting the background process. Error 
reported: Access is denied.
    + CategoryInfo          : OpenError: (localhost:String) [], PSRemotingTran 
   sportException
    + FullyQualifiedErrorId : -2147467259,PSSessionStateBroken


Finished: SUCCESS
Moerwald
  • 10,448
  • 9
  • 43
  • 83
red888
  • 27,709
  • 55
  • 204
  • 392

1 Answers1

0

i've had issues with credentials at times with PowerShell, i can usually fix it by using this:

$username = Username
$password = Password

$cred = New-Object -TypeName System.Management.Automation.PSCredential ($username, $password)
$Credentials = Get-Credential $cred

Basically entering the credentials into Get-credentials, then using that for credentials.

legoscia
  • 39,593
  • 22
  • 116
  • 167