【发布时间】:2014-09-22 16:10:55
【问题描述】:
我有一个 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 也很陌生。我确信这是非常简单的事情,希望能提供任何帮助。如果您需要更多信息,我将非常乐意提供。 顶!
编辑:这是附加的错误信息:
+ 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
【问题讨论】:
-
您是否尝试过以提升的权限运行您的 Powershell 控制台?没有它,它可能无法正确获取 $VMHosts。
-
是的,我最初不是,它返回一个错误,指出我需要提升权限。谢谢。
-
关于为什么这不起作用的任何其他想法?我觉得奇怪的是原始脚本工作正常。我正在做一些测试,$VMHost 为集群返回了正确的主机!
-
嗯...不幸的是,我没有明确地使用集群(或有一个要测试),所以我没有任何直接的想法。您是否能够成功让所有集群填充您的
$clusters变量?如果是这样,当您使用第一个foreach循环时,$cluster.name是否正确填充$ClusterName?此外,也许修改您的代码以使用一个集群作为测试可能会揭示是否存在与您修改的脚本不同的另一个潜在问题。 -
嗨,克里斯,感谢您回复我。是的
$cluster和$clusterName变量都正确填充。我通过修改代码以仅接受一个静态设置的集群来尝试您的建议。将$clusters = Get-Cluster更改为$clusters = @("server_name"),它运行良好!不幸的是,我仍然无法弄清楚为什么会发生这种情况。感谢您的建议。
标签: powershell vmware powercli