我需要从群集获取统计信息,例如内存或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
答案 0 :(得分:0)
您还应该在以下位置将“ 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()
}
答案 1 :(得分:0)
您可以做几件事。
首先,升级您的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
。如果输出没有意义,请在此处复制/粘贴。