title: Invoke-RestMethod : The underlying connection was closed error in powershell script
body: <p>I had written code in PowerShell to get the workitems of an area of a project by wiql.</p>
  <pre><code>$token = &quot;PAT&quot;
  
  $url=&quot;https://dev.azure.com/Organizationname/_apis/wit/wiql?api-version=5.1&quot;
  
  $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(&quot;:$($token)&quot;))
  
  $JSON = @'
  {
     &quot;query&quot;: &quot;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] &lt;&gt; '' ORDER BY [System.ChangedDate] DESC&quot;
  }
  '@
  
  $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = &quot;Basic $token&quot;} -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=&quot;https://dev.azure.com/Organizationname/ProjectName/_apis/wit/workitems?ids=$listOfTasks&quot; +&quot;&amp;&quot; + &quot;`$expand&quot; + &quot;=all&amp;api-version=5.1&quot;
  
  $response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = &quot;Basic $token&quot;} -Method Get 
  
  Write-Host &quot;result = $($response1 | ConvertTo-Json -Depth 100)&quot;
  </code></pre>
  <p>when i am executing the aboce script am getting below error.</p>
  <pre><code>Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive.
  At ....
  + ... response1 = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = &quot; ...
  +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
  </code></pre>
  <p>I have tried the options mentioned in the below link, but it didn't help.</p>
  <p><a href="https://blog.darrenjrobinson.com/powershell-the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send/" rel="nofollow noreferrer">https://blog.darrenjrobinson.com/powershell-the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send/</a></p>
  <p>can someone please help me in resolving this error.
  Many more thanks in advance!!!</p>