VMware Cloud Community
piercj2
Enthusiast
Enthusiast
Jump to solution

Move VM's between ResourcePools

Trying to prioritize VM's in a large Cluster using Resource Pools. Currently all VM's are in the default "Resources" pool.

Additional Resource Pools have been created and priorities set to low, medium and high.

I've come up with the below script and it worked for approx 50% of the VM's but, i'm getting the following error on all remaining VM's

Move-VM : Cannot bind parameter 'VM'. Cannot convert the "" value of type "System.Management.Automation.PSCustomObject" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine".

At line:9 char:17

+         Move-VM $t -Destination $finalRP

+                 ~~

    + CategoryInfo          : InvalidArgument: (:) [Move-VM], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

Any help with the below would be appreciated. It's just me using this for now so i'm not too worried about checking for correct Cluster/ResourcePool names at this point but, i will be adding that in once the move-vm piece is working.

$clusterName = Read-Host "Enter the name of the target Cluster"

$targetCluster = Get-Cluster -Name $clusterName

$RPname = Read-Host "Enter the name of the destination Resource Pool"

$targetRP = $targetCluster | Get-ResourcePool -Name $RPname | select name, Id

$finalRP = Get-ResourcePool | where {$_.Id -like $targetRP.Id}

$targetVMs = $null

$targetVMs = $targetCluster | Get-VM | select Name, ResourcePool, ResourcePoolId | sort Name

$targetVMs | Export-Excel

foreach ($t in $targetVMs) {

    if ($t.ResourcePool -Like "Resources") {

        Write-Host $t.Name "is currently in Resource Pool " -NoNewline

        Write-Host -ForegroundColor Red $t.ResourcePool - $t.ResourcePoolid

        Write-Host Moving $t.name "from " -NoNewline

        Write-Host -ForegroundColor Red $t.ResourcePool -NoNewline

        Write-Host " to " -NoNewline

        write-host -ForegroundColor Green $targetRP.Name - $targetRP.Id

        Move-VM $t -Destination $finalRP

        write-host

    }

}

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could be the victim of a known issue.
Try the bypass I gave in Re: Move-VM doesn't work against new VCSA 6.7 instance


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

That is because you do a Select-Object.

The object returned by Select-Object is not the same as an object returned by Get-VM.

Just leave out the Select-Object after the Get-VM


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
piercj2
Enthusiast
Enthusiast
Jump to solution

Hi Luc, i didn't know that selecting an object changed the returned type. Thanks !

I've made the suggested changes and, using "-whatIf" appears to work, i'e. the output is

PS C:\WINDOWS\system32> foreach ($t in $targetVMs) {

    if ($t.ResourcePool -Like "Resources") {

        Write-Host $t.Name "is currently in Resource Pool " -NoNewline

        Write-Host -ForegroundColor Red $t.ResourcePool - $t.ResourcePoolId

        Write-Host Moving $t.name "from " -NoNewline

        Write-Host -ForegroundColor Red $t.ResourcePool -NoNewline

        Write-Host " to " -NoNewline

        write-host -ForegroundColor Green $targetRP.Name - $targetRP.Id

        Move-VM -vm $t -Destination $finalRP -WhatIf

        write-host

    }

}

MyTestVM is currently in Resource Pool Resources - ResourcePool-resgroup-53

Moving MyTestMv from Resources to RP-Low - ResourcePool-resgroup-2663

What if: Performing the operation "Move-VM" on target "Moving VM 'MyTestVM'".

However, if i # out the "-WhatIf", i get the following error

MyTestVM is currently in Resource Pool Resources - ResourcePool-resgroup-53

Moving MyTestMv from Resources to RP-Low - ResourcePool-resgroup-2663

Move-VM : 13/01/2020 15:16:53    Move-VM        Object reference not set to an instance of an object.   

At line:9 char:9

+         Move-VM -vm $t -Destination $finalRP #-WhatIf

+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Move-VM], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

below is the amended code

$clusterName = Read-Host "Enter the name of the target Cluster"

$targetCluster = Get-Cluster -Name $clusterName

$RPname = Read-Host "Enter the name of the destination Resource Pool"

$targetRP = $targetCluster | Get-ResourcePool -Name $RPname

$targetVMs = $null

$targetVMs = $targetCluster | Get-VM MyTestVM

$targetVMs | select Name, PowerState, ResourcePool, ResourcePoolId | Export-Excel

foreach ($t in $targetVMs) {

    if ($t.ResourcePool -Like "Resources") {

        Write-Host $t.Name "is currently in Resource Pool " -NoNewline

        Write-Host -ForegroundColor Red $t.ResourcePool - $t.ResourcePoolId

        Write-Host Moving $t.name "from " -NoNewline

        Write-Host -ForegroundColor Red $t.ResourcePool -NoNewline

        Write-Host " to " -NoNewline

        write-host -ForegroundColor Green $targetRP.Name - $targetRP.Id

        Move-VM -vm $t -Destination $finalRP #-WhatIf

        write-host

    }

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You seem to be using $finalRP instead of $targetRP


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
piercj2
Enthusiast
Enthusiast
Jump to solution

sorry, that was a typo on my part, i'd already spotted that and corrected it in my script but, same error on run.

$clusterName = Read-Host "Enter the name of the target Cluster"

$targetCluster = Get-Cluster -Name $clusterName

$RPname = Read-Host "Enter the name of the destination Resource Pool"

$targetRP = $targetCluster | Get-ResourcePool -Name $RPname

$targetVMs = $null

$targetVMs = $targetCluster | Get-VM coir1sccmdp1

$targetVMs | select Name, PowerState, ResourcePool, ResourcePoolId | Export-Excel

foreach ($t in $targetVMs) {

    if ($t.ResourcePool -Like "Resources") {

        Write-Host $t.Name "is currently in Resource Pool " -NoNewline

        Write-Host -ForegroundColor Red $t.ResourcePool - $t.ResourcePoolId

        Write-Host Moving $t.name "from " -NoNewline

        Write-Host -ForegroundColor Red $t.ResourcePool -NoNewline

        Write-Host " to " -NoNewline

        write-host -ForegroundColor Green $targetRP.Name - $targetRP.Id

        Move-VM -vm $t -Destination $targetRP

        write-host

    }

}

the error returned is

MyTestVM is currently in Resource Pool Resources - ResourcePool-resgroup-53

Moving MyTestVM from Resources to RP-Low - ResourcePool-resgroup-2663

Move-VM : 13/01/2020 16:38:28    Move-VM        Object reference not set to an instance of an object.   

At line:9 char:9

+         Move-VM -vm $t -Destination $targetRP

+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Move-VM], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

I've looked at the main variables and they seem correct

PS C:\WINDOWS\system32> $t

Name                 PowerState Num CPUs MemoryGB      

----                 ---------- -------- --------      

MyTestVM         PoweredOn  4        8.000

PS C:\WINDOWS\system32> $t | gm

   TypeName: VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl

PS C:\WINDOWS\system32> $targetRP

Name                 CpuSharesL CpuReserva CpuLimitMH MemSharesL MemReservationG MemLimitGB    

                     evel       tionMHz    z          evel       B                             

----                 ---------- ---------- ---------- ---------- --------------- ----------    

RP-Low              Low        0          -1         Low        0.000           -1.000

PS C:\WINDOWS\system32> $targetRP | gm

   TypeName: VMware.VimAutomation.ViCore.Impl.V1.Inventory.ResourcePoolImpl

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you stop/start your session before trying again?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
piercj2
Enthusiast
Enthusiast
Jump to solution

i'm afraid i did.

i've even tried just running the following to keep things simple

PS C:\WINDOWS\system32> Get-ResourcePool | where {$_.Id -like $targetRP.Id}

Name                 CpuSharesL CpuReserva CpuLimitMH MemSharesL MemReservationG MemLimitGB   

                     evel       tionMHz    z          evel       B                            

----                 ---------- ---------- ---------- ---------- --------------- ----------   

RP-Low              Low        0          -1         Low        0.000           -1.000

PS C:\WINDOWS\system32> get-vm MyTestVM | Move-VM -destination (Get-ResourcePool | where {$_.Id -like $targetRP.Id})

Move-VM : 13/01/2020 17:10:17    Move-VM        Object reference not set to an instance of an object.  

At line:1 char:23

+ ... MyTestVM | Move-VM -destination (Get-ResourcePool | where {$_.Id -li ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Move-VM], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

The original script worked for approx 50% of the VM's in the Cluster, makes me now think it's something specific to these VM's and, not the Resource Pool.

Is there any reason why a VM couldn't be moved out of the default "Resources" pool ?

from the Web Client i just dragged one of the offending VM's into the RP-Low resource pool and it worked

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could be the victim of a known issue.
Try the bypass I gave in Re: Move-VM doesn't work against new VCSA 6.7 instance


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
piercj2
Enthusiast
Enthusiast
Jump to solution

That worked perfectly Luc, Thanks !

As an FYI, i'm running VCSA 6.5U3 to it looks like this issue/bug existed prior to 6.7.

The finished Code looks as follows for anyone who may need it

$clusterName = Read-Host "Enter the name of the target Cluster"

$targetCluster = Get-Cluster -Name $clusterName

$RPname = Read-Host "Enter the name of the destination Resource Pool"

$targetRP = $targetCluster | Get-ResourcePool -Name $RPname

# Move-VM not working due to known issues in version(s) VCSA

# use vSphere API Method as workaround

$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec

$spec.Pool = $targetRP.ExtensionData.MoRef

$targetVMs = $null

$targetVMs = $targetCluster | Get-VM

$targetVMs | select Name, PowerState, ResourcePool, ResourcePoolId | Export-Excel

foreach ($t in $targetVMs) {

    if ($t.ResourcePool -Like "Resources") {

        Write-Host $t.Name "is currently in Resource Pool " -NoNewline

        Write-Host -ForegroundColor Red $t.ResourcePool - $t.ResourcePoolId

        Write-Host Moving $t.name "from " -NoNewline

        Write-Host -ForegroundColor Red $t.ResourcePool -NoNewline

        Write-Host " to " -NoNewline

        write-host -ForegroundColor Green $targetRP.Name - $targetRP.Id

        # Move-VM not working due to known issues in version(s) of PowerCLI

        # use vSphere API Method as workaround

        # Move-VM -vm $t -Destination $targetRP

       

        $t.ExtensionData.RelocateVM($spec, [VMware.Vim.VirtualMachineMovePriority]::defaultPriority)

        write-host

    }

}

0 Kudos