我有一个PowerShell脚本,用于返回用户指定集群中每个主机的信息。用户在as参数中提供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发布于 2014-09-23 15:12:45
您测试过运行没有Get-VMHostStorage的-RescanAllHBA吗?为什么您认为$VMHost被返回为null?也许它找到了主机,但没有什么可以重新扫描的?只是猜一下。
不确定这是否会导致您的问题,但是您不需要运行Get-Cluster两次。相反,
$VMHosts = $cluster | Get-VMHost | ? <etc>编辑-对于给定的脚本运行,完整的错误文本会出现多少次?在那个运行过程中发现了多少个vmhost?你有没有主机的集群吗?没有存储空间的主机?如果来自一个循环迭代的值似乎正在破坏下一个迭代,则可以设置$vmhost = $null或$cluster = $null。(对不起,肯定有更好的说法.希望你能明白我的意思。)您可以始终将违规的代码行放在[try]块中,并使用捕捉处理错误。
再次编辑-显然是这在维护模式下为主机修复了它:if (!$VMHost) {continue}
https://stackoverflow.com/questions/25978523
复制相似问题