Jenkins不会使用Credential参数运行Start-Job

时间:2016-09-12 05:20:58

标签: windows powershell jenkins powershell-v3.0 start-job

我通过env注入器将凭据传递给脚本(注意这适用于我Invoke-Command)并尝试运行Start-Job,但jenkins并不喜欢它:

$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

这是我得到的错误(确认用户名和密码是正确的,当我从控制台运行此脚本时,它具有相同的功能)

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

1 个答案:

答案 0 :(得分:0)

我在使用PowerShell时遇到过凭证问题,我通常可以使用以下方法解决问题:

$username = Username
$password = Password

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

基本上将凭据输入Get-credentials,然后将其用于凭据。