我想借助 Powershell 中的 Invoke-RestMethod - POST 命令在现有 JIRA 票证中附加一个小 txt 文件
在代码之后(文件 Test_JTR_post_transition.ps1):
param(
[Parameter(Mandatory,HelpMessage='JIRA ID')] [String]$jiraID
)
write-host jiraID = $jiraID
$base64AuthInfo = Get-Content ($psScriptRoot + "\config\s_asa_tech_base64.txt")
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$jiraRestUrl = "http://xxxxxxx/jira/rest/api/2"
function put-attachedfile(){
param(
[Parameter(Mandatory=$true)] [String]$jiraID
)
$attfile = "export.txt"
$result= Invoke-RestMethod -Method POST -ContentType "multipart/form-data" -infile $attfile -uri "$jiraRestUrl/issue/$jiraID/attachments" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
return $result
write-host result = $result
}
$status = put-attachedfile -jiraID $jiraID
return $status
我按如下方式运行 ps1 文件:
PS D:\Scripts\eventCreation> .\Test_JTR_post_transition.ps1 -jiraID OASSBISAS-10414
我收到以下消息:
Invoke-RestMethod : The remote server returned an error: (404) Not Found.
At D:\Scripts\eventCreation\Test_JTR_post_transition.ps1:41 char:12
+ $result= Invoke-RestMethod -Method POST -infile $attfile -ContentTy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
您使用的 URI 似乎解析为不存在的路径。 根据JIRA Service Desk REST API Reference,它看起来像用于将附件上传到票证的 URI,看起来有点像这样:
/rest/servicedeskapi/request/{issueIdOrKey}/attachment
根据环境的不同,它可能会有所不同,但这是我根据所提供的信息所能得到的最接近的结果。
建议您尝试编辑这两行:
$jiraRestUrl = "http://xxxxxxx/jira/rest/api/2"
"$jiraRestUrl/issue/$jiraID/attachments"
致:
$jiraRestUrl = "http://xxxxxxx/rest/servicedeskapi/request/"
"$jiraRestUrl/$jiraID/attachment/"
也许您需要尝试一下 API 来弄清楚什么有效,也许首先根据 Jira 票证 ID 检索票证?