简体   繁体   English

Citrix NetScaler的Powershell和NITRO API的GET方法错误

[英]Powershell and NITRO API for Citrix NetScaler error on GET method

I am using a PowerShell module provided by Citrix to invoke the Nitro REST API. 我正在使用Citrix提供的PowerShell模块来调用Nitro REST API。 Calling the function I can successfully add and remove load balanced services from the load. 调用该函数,我可以成功地从负载中添加和删除负载平衡的服务。 However when I try to do a GET method to get the status of a service I get the error: 但是,当我尝试执行GET方法以获取服务状态时,出现错误:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send. Invoke-RestMethod:基础连接已关闭:发送时发生意外错误。

I have tried running Invoke-RestMethod without using the module but get the same error 我曾尝试在不使用模块的情况下运行Invoke-RestMethod,但收到相同的错误

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

When googling this error everything seems to point to certificate issues. 谷歌搜索此错误时,所有内容似乎都指向证书问题。 I had this initially even on POST method until i added the below to my script 我最初甚至在POST方法上都有这个,直到我将以下内容添加到脚本中

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

So since this works for doing POST i cant see why it wouldn't for a GET!! 因此,由于这适用于POST,所以我看不到为什么它不适合GET!

another weird thing is, if I put the URL directly into the browser then enter my credentials i get a response in raw text! 另一个怪异的事情是,如果我将URL直接放入浏览器中,然后输入我的凭据,则会收到原始文本的响应! so it looks like this is an issue with the way i am calling it in PowerShell rather than the NetScaler or the NITRO API! 所以看起来这是我在PowerShell中而不是NetScaler或NITRO API中调用它的方式的问题!

Someone please help as this is driving me crazy!! 有人请帮忙,这使我发疯了!!

Admitedly i am new to Invoke-RestMethod commands, but try this: 诚然,我是Invoke-RestMethod命令的新手,但是请尝试以下操作:

$creds = Get-Credential

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

What you will get is something similar to this: 您将获得与此类似的东西:

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

then type $service.service and you will see more information. 然后输入$ service.service,您将看到更多信息。 whatever attributes are availible will be listed. 列出任何可用的属性。 then just follow the pattern: $service.service. 然后只需遵循以下模式:$ service.service。

I had the same problem with Nitro API (specifically v10.5), and found that setting certificate policies, TLS versions and trust settings had no effect. 我在Nitro API(特别是v10.5)上遇到了同样的问题,发现设置证书策略,TLS版本和信任设置无效。 POST works, GET fails. POST有效,GET失败。

The solution for me was to not use the cmdlets and instead drop back to a native .Net method. 对我来说,解决方案是不使用cmdlet,而是改回本机.Net方法。 Below I am still using HTTPS with an internal certificate, hence still setting the callback. 下面,我仍然使用带有内部证书的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()

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM