2

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.

https://blog.darrenjrobinson.com/powershell-the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send/

can someone please help me in resolving this error. Many more thanks in advance!!!

Jennie
  • 225
  • 2
  • 13

3 Answers3

4

This is often due to your TLS version.

Try adding:

​[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

To the top of your script and rerunning it.

Max Morrow
  • 1,206
  • 4
  • 13
2

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

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
0

I resolved recently similar issue with PowerShell in Windows server 2022.

In my case it was caused by a proxy being turned off for that one user who actually is running the script.

Note that I was not calling external site as the site I was calling from PowerShell was hosted in IIS in the same server, but still that PowerShell command requires proxy to be turned on.

To turn on proxy: Go to Internet Explorer browser > Settings symbol > "Internet Options" > "Connections" tab > "Lan settings"

Mark two below check boxes and enter valid proxy and port.

Note that these options are per user basis - if you set it for one system account another one may not have it configured.

Proxy settings