由于我们的监控是从Windows平台完成的,我们希望使用powershell从ambari-rest-api中检索信息。
在浏览器中可以探索api。首次登录,然后可以粘贴使用过的URL:https://someazurenode.westeurope.cloudapp.azure.com/ambari/api/v1/clusters它只是在浏览器中显示json响应。
卷曲:
curl --user myusr:mypwd --insecure -i -H 'X-Requested-By:ambari' -X GET https://someazurenode.westeurope.cloudapp.azure.com/ambari/api/v1/clusters
工作良好
在powershell中(在禁用ssl-verification **之后):
$cred = New-Object System.Management.Automation.PSCredential ("myusr", (ConvertTo-SecureString "mypwd" -AsPlainText -Force))
Invoke-WebRequest -Method Get `
-UseBasicParsing `
-Uri "https://someazurenode.westeurope.cloudapp.azure.com/api/v1/clusters" -Headers @{"X-Requested-By"="Ambari"} `
-Credential $cred
--> 404
这似乎是授权的东西,因此我尝试了下面的选项(**):
Invoke-WebRequest -Method GET `
-Uri "https://someazurenode.westeurope.cloudapp.azure.com/api/v1/clusters" `
-Headers @{Authorization =[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes('myusr:mypwd'))}
- > 404未找到
**无视ssl验证的灵感:Ignoring Self-Signed Certificates from Powershell Invoke-RestMethod doesn't work (it's changed again...)
**处理基本认证Use Invoke-WebRequest with a username and password for basic authentication on the GitHub API的灵感
找到问题,最后......在我们的案例中,我们需要通过3个步骤来修复连接:
从那里很容易检索信息。
function Disable-SslVerification
{
if (-not ([System.Management.Automation.PSTypeName]"TrustEverything").Type)
{
Add-Type -TypeDefinition @"
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public static class TrustEverything
{
private static bool ValidationCallback(object sender, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors) { return true; }
public static void SetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = ValidationCallback; }
public static void UnsetCallback() { System.Net.ServicePointManager.ServerCertificateValidationCallback = null; }
}
"@
}
[TrustEverything]::SetCallback()
}
function Enable-SslVerification
{
if (([System.Management.Automation.PSTypeName]"TrustEverything").Type)
{
[TrustEverything]::UnsetCallback()
}
}
$domain = "my-cluster.westeurope.cloudapp.azure.com/ambari"
$usernm = "myusr"
$userpwd = "mypwd"
Disable-SslVerification
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$uri = "https://{0}/api/v1/clusters" -f $domain
Write-Output $uri
$credstring = "{0}:{1}" -f $usernm, $userpwd
$credbytes = [System.Text.Encoding]::ASCII.GetBytes($credstring)
$credbase64 = [System.Convert]::ToBase64String($credbytes)
$credAuthValue = "Basic {0}" -f $credbase64
$headers = @{ Authorization = $credAuthValue}
$result = "-"
$result = Invoke-RestMethod -Method Get -UseBasicParsing -Uri $uri -Headers $headers
$result