[英]Can't connect to vmware cluster with powershell
我需要從群集獲取統計信息,例如內存或CPU使用率。 我正在嘗試使用Connect-VIServer命令連接,但無法通過它。 我可以使用vSphere Client訪問vcenter,沒有任何問題。
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer -server 'ServerIP' -user 'Username' -password 'Password'
$clusterName = 'ServerIP'
$stat = 'cpu.usagemhz.average','mem.usage.average'
$entity = Get-Cluster -Name $clusterName
$start = (Get-Date).AddDays(-2)
Get-Stat -Entity $clusterName -Stat $stat -Start $start |
Group-Object -Property Timestamp |
Sort-Object -Property Name |
Select @{N='Cluster';E={$entity.Name}},
@{N='Time';E={$_.Group[0].Timestamp}},
@{N='CPU GHz Capacity';E={$script:capacity = [int]($entity.ExtensionData.Summary.TotalCPU/1000); $script:capacity}},
@{N='CPU GHz Used';E={$script:used = [int](($_.Group | where{$_.MetricId -eq 'cpu.usagemhz.average'} | select -ExpandProperty Value)/1000); $script:used}},
@{N='CPU % Free';E={[int](100 - $script:used/$script:capacity*100)}},
@{N='Mem Capacity GB';E={$script:mcapacity = [int]($entity.ExtensionData.Summary.TotalMemory/1GB); $script:mcapacity}},
@{N='Mem Used GB';E={$script:mused = [int](($_.Group | where{$_.MetricId -eq 'mem.usage.average'} | select -ExpandProperty Value) * $script:mcapacity/100); $script:mused}},
@{N='Mem % Free';E={[int](100 - $script:mused/$script:mcapacity*100)}} |
Export-csv -Path C:\cluster-stats.csv
該腳本運行了幾分鍾,但最后我得到的只是一個錯誤,提示:
Connect-VIServer : 25/10/2018 12:54:46 Connect-VIServer The underlying connection was closed: An unexpected error occurred on a send.
At line:3 char:1
+ Connect-VIServer -server 'ServerIP' -user 'Username' -password 'Password'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Connect-VIServer], ViError
+ FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_WebException,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer
您還應該在下面指定“ https”作為協議:
Connect-ViServer
所以像這樣:
Connect-ViServer -Server $myServer -Protocol https -Credential $myCreds
我以前遇到過這樣的問題(與SSL / TLS有關)。最終解決方案中存在一些差異,因此其中之一應該有所幫助:
::主要用於vCloud ::
function Set-VmWareTls
{
try {
# Grab current ciphers, convert their names to strings, add to an array
$esp = [System.Net.ServicePointManager]::SecurityProtocol.ToString().Split(',') | Foreach-Object {
$($_.TrimStart(' ').TrimEnd(' '))
}
# See if gathered ciphers contains the needed ciphers for vCloud/vCenter to connect without issue
if (($esp -notcontains 'Tls11') -or ($esp -notcontains 'Tls12')) {
# if they're not found, add them
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12;
}
# if we were able to process evertying above, return true
$true
} catch {
# If we are unable to set the ciphers return false
$false
}
}
:: ::的vCenter
function Set-IgnoreCertCheck
{
if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore()
}
您可以做幾件事。
首先,升級您的PowerCLI版本。 PowerCLI幾年(〜2015年)未使用PowerShell管理單元,可能無法在vSphere 6.0及更高版本上使用。
其次,使用名稱為Resolve-Error
的函數。 這將調出最后一個錯誤對象,並輸出一個包含一些其他信息的屬性,這些信息可以幫助您進一步進行故障排除。
function Resolve-Error ($ErrorRecord=$Error[0])
{
$ErrorRecord | Format-List * -Force
$ErrorRecord.InvocationInfo | Format-List *
$Exception = $ErrorRecord.Exception
for ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException))
{ “$i” * 80
$Exception | Format-List * -Force
}
}
運行以上代碼行,再次運行腳本,然后運行Resolve-Error
。 如果輸出沒有意義,請在此處復制/粘貼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.