Question:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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 |
Answer:
i’ve had issues with credentials at times with PowerShell, i can usually fix it by using this:
1 2 3 4 5 6 |
$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.