Powershell 附加文件并创建新工作项
我希望使用API在azure devops中创建一个工作项。我能够创建带有标题、描述和区域路径、迭代路径的工作项。现在我想创建一个包含标题、描述、区域路径、迭代路径的工作项,并附加一个文件并创建一个新的工作项 创建工作项后,有API可用于附加文件,但我想先附加一个文件并创建一个工作项Powershell 附加文件并创建新工作项,powershell,azure-devops,azure-devops-rest-api,Powershell,Azure Devops,Azure Devops Rest Api,我希望使用API在azure devops中创建一个工作项。我能够创建带有标题、描述和区域路径、迭代路径的工作项。现在我想创建一个包含标题、描述、区域路径、迭代路径的工作项,并附加一个文件并创建一个新的工作项 创建工作项后,有API可用于附加文件,但我想先附加一个文件并创建一个工作项 $Header = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($
$Header = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
$uri = "https://dev.azure.com/$organization/$project/_apis/wit/workitems/$"+"$WorkItemType"+"?api-version=6.0"
$body="[
{
`"op`": `"add`",
`"path`": `"/fields/System.Title`",
`"value`": `"$($WorkItemTitle)`"
},
{
`"op`": `"add`",
`"path`": `"/fields/System.Description`",
`"value`": `"This is for workitme testing`"
},
{
`"op`": `"add`",
`"path`": `"/fields/System.AssignedTo`",
`"value`": `"$($AssignUser)`"
},
{
`"op`": `"add`",
`"path`": `"/fields/System.AreaPath`",
`"value`": `"$($AreaPath)`"
},
{
`"op`": `"add`",
`"path`": `"/fields/System.IterationPath`",
`"value`": `"$($IterationPath)`"
},
{
`"op`": `"add`",
`"path`": `"/fields/System.AttachedFiles`",
`"value`": `"spec.txt`"
}
]"
Invoke-RestMethod -Uri $uri -Method POST -Headers $Header -ContentType "application/json-patch+json" -Body $body
我找到了这个链接,有人可以把它转到powershell。我不明白附件url=attachment.url的主体 基于上述链接,我试图附加一个文件,但出现错误您必须:
$connectionToken="{PAT token}" \\Put PAT token
$file = Get-ChildItem -Path "C:\Users\xxx\Downloads\AusSouthEast.txt" \\Specify the file location
$url="https://dev.azure.com/{org name}/_apis/wit/attachments?fileName=AusSouthEast.txt&api-version=6.1-preview.3"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -InFile $file -ContentType application/octet-stream
$AttachURL = $($response.url | ConvertTo-Json -Depth 100)
$CreateUrl="https://dev.azure.com/{org name}/{project name}/_apis/wit/workitems/`${wit type}?api-version=6.1-preview.3"
$WITBody=@"
[
{
"op": "add",
"path": "/fields/System.Title", \\WIT title
"from": null,
"value": "From Powershell"
},
{
"op": "add",
"path": "/relations/-", \\WIT attachment
"value": {
"rel": "AttachedFile",
"url": $AttachURL,
"attributes": {
"comment": "Spec for the task"
}
}
}
]
"@
Invoke-RestMethod -Uri $CreateUrl -Headers @{Authorization = "Basic $token"} -Method Post -Body $WITBody -ContentType application/json-patch+json
提示:
Invoke RestMethod
cmdlet中使用-infle
指向本地文件,然后指定
-ContentType应用程序/octet流
,指示
上载文件包含二进制数据
$
+类型名称
指向工作项类型。
但是$
是powershell的特殊字符之一,您需要使用`
要退出$
假设我创建了一个新的附件,然后我可以创建一个带有参数的新工作项,比如工作项标题、描述和附件吗?我说得对吗?我想是的。您可以创建附件并在创建新工作项时使用其url。我发现此链接可以让其他人将其连接到powershell。我不理解附件url=attachment.url@Sar的主体。当你创建一个附件时,你会收到一个带有id和url的响应。您可以在新的工作项正文中使用此URL。
$connectionToken="{PAT token}" \\Put PAT token
$file = Get-ChildItem -Path "C:\Users\xxx\Downloads\AusSouthEast.txt" \\Specify the file location
$url="https://dev.azure.com/{org name}/_apis/wit/attachments?fileName=AusSouthEast.txt&api-version=6.1-preview.3"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -InFile $file -ContentType application/octet-stream
$AttachURL = $($response.url | ConvertTo-Json -Depth 100)
$CreateUrl="https://dev.azure.com/{org name}/{project name}/_apis/wit/workitems/`${wit type}?api-version=6.1-preview.3"
$WITBody=@"
[
{
"op": "add",
"path": "/fields/System.Title", \\WIT title
"from": null,
"value": "From Powershell"
},
{
"op": "add",
"path": "/relations/-", \\WIT attachment
"value": {
"rel": "AttachedFile",
"url": $AttachURL,
"attributes": {
"comment": "Spec for the task"
}
}
}
]
"@
Invoke-RestMethod -Uri $CreateUrl -Headers @{Authorization = "Basic $token"} -Method Post -Body $WITBody -ContentType application/json-patch+json