调用静止方法:powershell脚本中出现基础连接关闭错误

kuhbmx9i  于 2023-01-02  发布在  Shell
关注(0)|答案(3)|浏览(401)

我已经在PowerShell中编写了代码,通过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)"

当我正在执行aboce脚本我得到下面的错误.

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

我已经尝试了下面的链接中提到的选项,但它没有帮助.
https://blog.darrenjrobinson.com/powershell-the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send/
有人能帮我解决这个错误吗?2提前谢谢!3!4!

vs91vp4v

vs91vp4v1#

您的脚本在我这边运行得很好。看起来这个问题与您的客户机环境有关。您可以检查this blog以查看解决方案是否有助于您:
问题是较旧版本的DOTNET框架(即3.5和4.0)本身不支持较新版本的TLS(如1.2和1.3)。您可以尝试安装较新版本的DOTNET。Microsoft提供的有关SSL/TLS的详细信息可在此处找到:
https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls

yqhsw0fo

yqhsw0fo2#

这通常是由于您的TLS版本。
尝试添加:

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

到脚本的顶部并重新运行它。

7gyucuyw

7gyucuyw3#

我最近解决了Windows Server 2022中PowerShell的类似问题。
在我的例子中,这是由于为实际运行脚本的用户关闭了代理。
请注意,我没有调用外部站点,因为我从PowerShell调用的站点托管在同一服务器的IIS中,但PowerShell命令仍然需要打开代理。
要打开代理:进入Internet Explorer浏览器〉设置符号〉“Internet选项”〉“连接”选项卡〉“局域网设置”
选中下面的两个复选框并输入有效的代理和端口。
请注意,这些选项是基于每个用户的-如果您为一个系统帐户设置了这些选项,则另一个帐户可能没有配置这些选项。
Proxy settings

相关问题