Jenkins -> powershell Invoke-WebRequest with UseDefaultCredentials



我尝试在Jenkins从属服务器上执行PowerShell脚本。它看起来像:

stage ("Get") {
steps {
powershell( 
script: '''
$srcCommonParams = @{
Method = 'GET'
Uri = $getUri
}
Invoke-WebRequest @srcCommonParams -UseDefaultCredentials -Verbose

问题是默认凭据来自哪里?我以为他们是我的Jenkins slave windows服务运行的代表。问题是我的Jennkins slave以用户A的身份运行,但Invoke-WebRequest抱怨的用户B没有足够的权限进行请求。

您必须从Jenkins那里获得凭据。你必须添加"withCredentials",并在powershell中类似于此:

stage ("Get") {
steps {  withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'your-credentials-id',usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']])
powershell( 
script: '''
$encryptedPassword = ConvertTo-SecureString "%PASSWORD%" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("%USERNAME%", $encryptedPassword)
$srcCommonParams = @{
Method = 'GET'
Uri = $getUri
}
Invoke-WebRequest @srcCommonParams -Credential $mycreds -Verbose

最新更新