Search code examples
powershellvmwarepowercli

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


I need to check particular VMware VM is currently in the recent task like CLone_task, Migrate_VMTask,.etc and also skip that VM before VM migration starts..

I have tried the below code:

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

Get-Task : Cannot bind parameter 'Status'. Cannot convert the "nalb00cava3"
value of type "VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl"
to type "VMware.VimAutomation.Sdk.Types.V1.TaskState".
At line:1 char:10
+ Get-Task (Get-VM -Name nalb00cava3) | Select State
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Task], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetTask 

Solution

  • This should do what you're looking for, the final pipe is where you would define the specific VM.

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

    It filters on ObjectId from Get-Task, then referencing the Id, determines the VM names, and finally filters on the VM you define.