我正在编写jenkins pipelinescript,通过powershell调用休息服务。
username = "myusername"
passwor = "mypwd"
uri="myresturl"
fileName="myfilePath"
withEnv(["resturi=${uri}","filePath=${fileName}", "uid=${username}", "passwordd=${passwor}"])
{
powershell '''
$pwd = $Env:passwordd | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Env:uid, $pwd
Invoke-RestMethod -Uri $Env:resturi -Method Post -InFile $Env:filePath -Credential $credential
'''
}
当我运行上面的脚本时,它会因以下错误而失败:
powershell.exe : Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.At At C:\Tools\Jenkins\workspace\ApigeeNew@tmp\durable-8134c670\powershellWrapper.ps1:5 char:3
+ & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Fi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Invoke-RestMeth...nauthorized.At :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
C:\Tools\Jenkins\workspace\ApigeeNew@tmp\durable-8134c670\powershellScript.ps1:
17 char:7
+ Invoke-RestMethod -Uri $Env:resturi -Method Post -InFile ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:Htt
pWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShe
ll.Commands.InvokeRestMethodCommand
我正在使用正确的用户名和密码,并且相同的功能通过cUrl脚本正常工作。 Powershell版本为5.0。
如果我在上述脚本中做错了,请告诉我。
我的cUrl脚本:
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'prasad', usernameVariable:'pcfUserName', passwordVariable: 'pcfPassword']])
{
createProxyURL = env.APIGEE_ENV_URL + '/apis?action=import&name=' + proxyName + '&validate=true'
scriptString = "curl -X POST -u ${pcfUserName}:${pcfPassword} -F file=@" + apiProxyZip + " \"" + createProxyURL + "\" "
}
credentialsId:'prasad' - > 'prasad'指向Jenkins-> Credentials