I had written code in PowerShell to get the workitems of an area of a project by wiql.
$token = "PAT"
$url="https://dev.azure.com/Organizationname/_apis/wit/wiql?api-version=5.1"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = @'
{
"query": "SELECT [System.Id], [System.WorkItemType], [System.State],[System.AreaPath],[System.Tags],[System.CommentCount],[System.ChangedDate] FROM workitems WHERE[System.Id] IN(@follows) AND [System.TeamProject] = 'Azure' AND [System.State] <> '' ORDER BY [System.ChangedDate] DESC"
}
'@
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
$listOfTasks = New-Object Collections.Generic.List[String]
ForEach( $workitem in $response.workItems ) {
$listOfTasks.Add($workitem.id)
}
$listOfTasks = $listOfTasks -join ','
$listOfTasks
$url1="https://dev.azure.com/Organizationname/ProjectName/_apis/wit/workitems?ids=$listOfTasks" +"&" + "`$expand" + "=all&api-version=5.1"
$response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get
Write-Host "result = $($response1 | ConvertTo-Json -Depth 100)"
when i am executing the aboce script am getting below error.
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive.
At ....
+ ... response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I have tried the options mentioned in the below link, but it didn't help.
can someone please help me in resolving this error. Many more thanks in advance!!!
Your script worked well on my side. It seems the issue is related to your client machine environment. You could check this blog to see whether the solution helps you:
The issue is that older versions of DOTNET framework (i.e. 3.5 and 4.0) do not inherently support the newer versions of TLS like 1.2 and 1.3. You could try to install newer versions of DOTNET. More information from Microsoft regarding SSL/TLS can be found here:
https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls