0

我正在使用 Citrix 提供的 PowerShell 模块来调用 Nitro REST API。调用该函数我可以成功地从负载中添加和删除负载均衡服务。但是,当我尝试使用 GET 方法来获取服务的状态时,我得到了错误:

Invoke-RestMethod :底层连接已关闭:发送时发生意外错误。

我尝试在不使用模块的情况下运行 Invoke-RestMethod 但得到相同的错误

Invoke-RestMethod -WebSession $myNSSession.WebSession -Method GET -Uri https://<NetScaler IP/nitro/v1/config/service/<Service Name>

当谷歌搜索这个错误时,一切似乎都指向证书问题。我最初甚至在 POST 方法上也有这个,直到我将以下内容添加到我的脚本中

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

因此,由于这适用于 POST,我不明白为什么它不适用于 GET !

另一个奇怪的事情是,如果我将 URL 直接放入浏览器然后输入我的凭据,我会得到原始文本的响应!所以看起来这是我在 PowerShell 而不是 NetScaler 或 NITRO API 中调用它的方式的问题!

有人请帮忙,因为这让我发疯!

4

2 回答 2

0

诚然,我是 Invoke-RestMethod 命令的新手,但试试这个:

$creds = Get-Credential

$service = Invoke-RestMethod -Uri https://<NetScaler IP/nitro/v1/config/service/<Service Name> -Credential $creds

你会得到类似这样的东西:

*errorcode* *message*       *serverity*           *service*
*        0   Done            NONE                  {@{name=<service name; n..

然后键入 $service.service ,您将看到更多信息。将列出任何可用的属性。然后只需遵循以下模式:$service.service。

于 2015-06-08T17:41:55.123 回答
0

我对 Nitro API(特别是 v10.5)也有同样的问题,发现设置证书策略、TLS 版本和信任设置没有效果。POST 有效,GET 失败。

我的解决方案是不使用 cmdlet,而是退回到原生 .Net 方法。下面我仍然使用带有内部证书的 HTTPS,因此仍然设置回调。

$NSProtocol = "https://"
$NSHostname = "netscaler"

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$WebRequest = [System.Net.WebRequest]::Create("$NsProtocol$NsHostname/nitro/v1/config/hanode")
$WebRequest.Method = "GET"
$WebRequest.ContentType = "application/json; charset=utf-8";
$WebRequest.Headers.Add("AUTHORIZATION","Basic $([System.Convert]::ToBase64String([System.Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($nsuser+":"+$nspass)))")
$Response = $WebRequest.GetResponse()
$ReadStream = New-Object System.IO.StreamReader $Response.GetResponseStream()
$HaState = ConvertFrom-Json $ReadStream.ReadToEnd()

希望有帮助。

于 2018-09-19T08:48:31.370 回答