【问题标题】:Can't connect to vmware cluster with powershell无法使用 powershell 连接到 vmware 集群
【发布时间】:2018-10-25 11:19:39
【问题描述】:

我需要从集群中获取统计信息,例如内存或 CPU 使用情况。 我正在尝试使用 Connect-VIServer 命令进行连接,但无法通过它。 我可以毫无问题地使用 vSphere 客户端访问 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

【问题讨论】:

    标签: powershell vmware


    【解决方案1】:

    您还应该将“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()
    }  
    

    【讨论】:

    • 有什么理由你这样做而不是:Set-PowerCLIConfiguration -InvalidCertificateAction Ignore?
    • 不——我根本没有意识到这一点。不过谢谢!
    【解决方案2】:

    您可以做几件事。

    首先,升级您的 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。如果输出没有意义,请在此处复制/粘贴。

    【讨论】:

    • 它给了我 2 个错误,但我不明白哪里出错了:- Connect-VIServer 底层连接已关闭:发送时发生意外错误。 - 无法从传输连接读取数据:现有连接被远程主机强行关闭。
    猜你喜欢
    • 2021-11-23
    • 2018-11-01
    • 1970-01-01
    • 2022-11-13
    • 2020-02-08
    • 2020-04-28
    • 2019-06-10
    • 2014-11-05
    • 2021-09-18
    相关资源
    最近更新 更多