无法使用PowerCLI在PowerShell中的多个群集中的主机上返回信息

时间:2014-09-22 16:10:56

标签: powershell vmware powercli

我有一个PowerShell脚本,用于返回用户指定群集上每个主机的信息。用户以参数形式提供vCenter和群集,脚本按预期工作。

我正在尝试修改此脚本,因此用户只需要将vCenter作为参数传入,它将返回所有群集上所有主机的信息。

以下是我的原始剧本:

Param(
    $vc,
    $ClusterName
)

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer $vc

$VMHosts = Get-Cluster $ClusterName | Get-VMHost  | ? { $_.ConnectionState -eq "Connected" } | Sort-Object -Property Name

foreach ($VMHost in $VMHosts) {

 Get-VMHostStorage -RescanAllHba -VMHost $VMHost | Out-Null
 $esx = Get-VMHost $VMHost
 foreach($hba in (Get-VMHostHba -VMHost $esx -Type "FibreChannel")){
     $target = ((Get-View $hba.VMhost).Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -eq $hba.Key}).Target
     $luns = Get-ScsiLun -Hba $hba  -LunType "disk"
     $nrPaths = ($target | %{$_.Lun.Count} | Measure-Object -Sum).Sum
     $deadPaths = $luns | Get-ScsiLunPath | Group-Object -Property state | ? { $_.Name -eq "Dead"}
     $hbaDevice = $hba.Device
     $targetCount = $target.Count
     $lunsCount = $luns.Count
     $deadPathCount = $deadPaths.Count

     "vmhost=$VMHost;hba=$hbaDevice;targets=$targetCount;devices=$lunsCount;paths=$nrPaths;deadpaths=$deadPathsCount|"

 }
}

Disconnect-VIServer -Confirm:$False

这是我的修改版本:

Param(
    $vc
)

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer $vc
$clusters = Get-Cluster

foreach ($cluster in $clusters) {

    $clusterName = $cluster.name
    $VMHosts = Get-Cluster $clusterName | Get-VMHost  | ? { $_.ConnectionState -eq "Connected" } | Sort-Object -Property Name


    foreach ($VMHost in $VMHosts) {


        Get-VMHostStorage -RescanAllHba -VMHost $VMHost | Out-Null
        $esx = Get-VMHost $VMHost

        foreach($hba in (Get-VMHostHba -VMHost $esx -Type "FibreChannel")){
            $target = ((Get-View $hba.VMhost).Config.StorageDevice.ScsiTopology.Adapter | where {$_.Adapter -eq $hba.Key}).Target
            $luns = Get-ScsiLun -Hba $hba  -LunType "disk"
            $nrPaths = ($target | %{$_.Lun.Count} | Measure-Object -Sum).Sum
            $deadPaths = $luns | Get-ScsiLunPath | Group-Object -Property state | ? { $_.Name -eq "Dead"}
            $hbaDevice = $hba.Device
            $targetCount = $target.Count
            $lunsCount = $luns.Count
            $deadPathCount = $deadPaths.Count
            "vmhost=$VMHost;hba=$hbaDevice;targets=$targetCount;devices=$lunsCount;paths=$nrPaths;deadpaths=$deadPathsCount|"
        }
    }
}



Disconnect-VIServer -Confirm:$False

我得到的错误是:

Could not execute powershell command.
At \\xx\xxx\xxxx\scripts\vmwarePathCheckAllClusters.ps1:35 char:26
+         Get-VMHostStorage <<<<  -RescanAllHba -VMHost $VMHost | Out-Null

似乎$VMHost正在返回null,但我找不到原因!

这是我第一次使用PowerCLI CommandLets,我也是PowerShell的新手。我确信这是非常简单的事情,并希望得到任何帮助。如果您需要更多信息,我将非常乐意提供。 BUMP!

编辑:这是其他错误信息:

+         Get-VMHostStorage <<<<  -RescanAllHba -VMHost $VMHost | Out-Null ---> VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.VimException: 9/24/2014 5:58:57 AM Get-VMHostStorage       Value cannot be found for the mandatory parameter VMHost     ---> System.Management.Automation.ParameterBindingException: Value cannot be found for the mandatory parameter VMHost

1 个答案:

答案 0 :(得分:2)

您是否在没有Get-VMHostStorage的情况下测试了运行-RescanAllHBA?为什么你认为$VMHost被返回为null?也许它找到了主机,但没有重新扫描?只是一个猜测。

不确定这可能会导致您的问题,但您不需要运行Get-Cluster两次。相反,

$VMHosts = $cluster | Get-VMHost  | ? <etc>

编辑 - 对于给定的脚本运行,完整错误文本出现多少次?在那次运行中发现了多少个vmhost?你有没有主机的集群吗?没有存储空间的主机?如果来自一个循环迭代的值似乎破坏了下一次迭代,则可以设置$vmhost = $null$cluster = $null。 (对不起,有一个更好的方式来表达这个...希望你能抓住我的漂移。)你总是可以将有问题的代码行放在[try]块中,并用{{处理错误3}}

再次编辑 - 显然catch已在维护模式下为主机修复了它:if (!$VMHost) {continue}