Я пишу 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