如何检查特定 VMware VM 当前不在使用 PowerShell 的任务中 运行?

How to check particular VMware VM is not currently running in the task using PowerShell?

我需要检查特定的 VMware VM 当前是否在最近的任务中,例如 CLone_task、Migrate_VMTask、.etc,并且在 VM 迁移开始之前跳过该 VM..

我试过下面的代码:

PS> Get-Task (Get-VM -Name VM1) | Select 状态

Get-Task:无法绑定参数 'Status'。无法转换 "nalb00cava3"
"VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl" 类型的值
键入 "VMware.VimAutomation.Sdk.Types.V1.TaskState"。
在 line:1 char:10
+ 获取任务(获取 VM -名称 nalb00cava3)| Select 状态
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-Task], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetTask

这应该可以满足您的需求,最后一个管道是您定义特定 VM 的地方。

Get-Task | ?{$_.ObjectId -match 'VirtualMachine'} | Select @{N='VM';E={(Get-View -Id $_.ObjectId).Name }},State,Description | where  {$_.VM -eq "VM1"}

它从 Get-Task 过滤 ObjectId,然后引用 Id,确定 VM 名称,最后过滤您定义的 VM。