如何在 powershell 中编写语法以重试 Foreach 循环

问题描述

我是 VMware Powercli 的新手,但我正在尝试编写脚本,以便在资源可用的集群主机中对虚拟机进行 vMotion。在我的情况下,我有多个集群,其中启用了 HA A 并且手动配置了 DRS,并且每个主机都需要使用较新版本的 Esxi 版本进行更新。因此,要将 Esxi 主机置于维护模式,需要先迁移 VM,迁移完成后,可以将没有 VM 的主机置于维护模式。因此,在这种情况下,我正在尝试编写将 VM 迁移到其他主机的脚本,但如果出现故障,此代码是否有效?

param(
    [String] $vcenter_fqdn,[String] $vcenter_username,[String] $vcenter_password,[String] $cluster_name,[String] $source_host
)
    
$maxTries = 3
While ($maxTries -gt 0) {
  
    Connect-VIServer $vcenter_fqdn -User $vcenter_username -Password $vcenter_password -ErrorAction stop >$null
    $clusterhosts = Get-Cluster $cluster_name | Get-VMHost | Where-Object { $_.name -notlike "$source_host" }
    $vms = Get-VMHost $source_host | Get-VM
    $vmcount = $vms.count
    if ($vmcount -eq 0) {
        Write-Host "Host Does Not found any VM"
        break
    }
    else {
        Write-Host there are $vmcount number VMs found will try to Migrate
    
        foreach ($vm in $vms) {
            $targethost = $clusterhosts | Get-Random
            Move-VM -VM $vm -Destination $targethost >$null #-ErrorAction stop
        }
        
        break
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...