我得到的错误是这个......请帮助调试它。
Invoke-RestMethod : {"count":1,"value":{"Message":"Bad JSON escape sequence: \\A. Path 'query', line 2, position
260.\r\nUnexpected character encountered while parsing value: g. Path 'query', line 2, position 260.\r\n"}}
At C:carryover.ps1:29 char:16
+ ... eryresult = Invoke-RestMethod -Uri $uri -Method POST -Body $json -Con ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
剧本是这样的:
`Param(
[string]$baseurl = "https://dev.azure.com/<org>/<project>",
[string]$projectName = "<project>",
[string]$user = "<email address> ",
[string]$token = "<PAT Token>"
)
start-sleep -Seconds 10
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
# Query the work items with wiql
$uri = "$baseurl/$($projectName)/_apis/wit/wiql?api-version=5.1"
pause
function CreateJsonBody
{
$value = @"
{
"query": "Select [System.Id], [System.Title], [System.State],[System.Tags] From WorkItems Where [System.WorkItemType] = 'User Story' AND [System.State] <> 'Closed' AND [System.State] <> 'Removed' AND [System.IterationPath] = @currentIteration('[Agile-0219]\Agile-0219 Team') order by [System.CreatedDate] desc"
}
"@
return $value
}
$json = CreateJsonBody
#Get the urls for WIs
$queryresult = Invoke-RestMethod -Uri $uri -Method POST -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$wiurls = $queryresult.workItems.url
pause
#write-host $uri
Write-Host ($queryresult | convertto-json -depth 99)
pause
#Filter the work items which carried from other iterations to current iteration
$wis = @()
cls
foreach($wiurl in $wiurls){
#Set the work item revision URL
$revurl = "$wiurl/revisions"
#write-host "revurl:"$revurl
$wi = (Invoke-RestMethod -Uri $revurl -Method GET -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
#write-host ($wi.value.fields.'System.IterationPath'|select -Unique)
#Detect the Unique iterations which the work item ever been involved
$iterationcount = ($wi.value.fields.'System.IterationPath'|select -Unique).count
#write-host $iterationcount
if ($iterationcount -gt 1) # Filter the work items which have more than one iteration value
{
# Select the latest revision
$wilatest = ($wi.value | Select -last 1)
#Write-Host ($wilatest | convertto-json -depth 99)
$customObject = new-object PSObject -property @{
"WitID" = $wilatest.id
"Title" = $wilatest.fields.'System.Title'
"AssignedTo" = $wilatest.fields.'System.AssignedTo'.displayName
"ChangedDate" = $wilatest.fields.'System.ChangedDate'
"ChangedBy" = $wilatest.fields.'System.ChangedBy'.displayName
"WorkItemType" = $wilatest.fields.'System.WorkItemType'
"State" = $wilatest.fields.'System.State'
"URL" = $wilatest.url
}
$wis += $customObject
}
}
$wis | Select-Object `
WitID,
Title,
AssignedTo,
ChangedDate,
ChangedBy,
WorkItemType,
State,
URL #|export-csv -Path C:\Users\msingleton\Downloads\sample.csv -NoTypeInformation
Invoke-RestMethod : {"count":1,"value":{"Message":"Bad JSON escape sequence: \\A. Path 'query', line 2, position
260.\r\nUnexpected character encountered while parsing value: g. Path 'query', line 2, position 260.\r\n"}}
At C:\Users\msingleton\Downloads\carryover.ps1:29 char:16
+ ... eryresult = Invoke-RestMethod -Uri $uri -Method POST -Body $json -Con ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand`