VMware Cloud Community
tjc516
Contributor
Contributor
Jump to solution

Help with Move-VM Script

Hi all, appreciate any help you can provide.  I found the majority of the script online so i assume it works without my edits.  I had to change the $vms variable to search for a specific tag and create the list from that.  I believe that's where the errors coming from.

$maxParallel = 3

$vms =  Get-Cluster MSCTW06 | Get-VM | Get-Annotation -Name vrmManagedMachine | Where {$_.Value -ne "True"} | Select-Object -Name 'AnnotatedEntity' |Sort 'AnnotatedEntity' | ft -hidetableheaders

$newCluster = Get-Cluster -Name MSSWVS01

foreach($vm in $vms){

  Move-VM -VM (Get-VM -Name $vm) -destination $newCluster -datastore (Get-Datastore -Name msswvs01_pmx01_0* | Sort-Object -Property FreeSpaceGB  -Descending | Select -First 1) -RunAsync

  do

  {

    sleep 5

  } while((Get-Task -Status Running | where{$_.Name -eq 'RelocateVM_Task'}).Count -gt $maxParallel)}

Error i'm receiving;

Get-VM : 12/5/2019 9:51:33 AM   Get-VM          VM with name 'Microsoft.PowerShell.Commands.Internal.Format.FormatStartData' was

not found using the specified filter(s).

At C:\scripts\MigrateVM.ps1:6 char:16

+   Move-VM -VM (Get-VM -Name $vm) -destination $newCluster -datastore  ...

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

    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Move-VM : Cannot validate argument on parameter 'VM'. The argument is null or empty. Provide an argument that is not

null or empty, and then try the command again.

At C:\scripts\MigrateVM.ps1:6 char:15

+   Move-VM -VM (Get-VM -Name $vm) -destination $newCluster -datastore  ...

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

    + CategoryInfo          : InvalidData: (:) [Move-VM], ParameterBindingValidationException

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, then try like this

$maxParallel = 3

$vms =  Get-Cluster MSCTW06 | Get-VM | where{(Get-Annotation -Entity $_ -Name vrmManagedMachine).Value-ne "True"}

$newCluster = Get-Cluster -Name MSSWVS01


foreach($vm in $vms){

  Move-VM -VM $vm -Destination $newCluster -Datastore (Get-Datastore -Name msswvs01_pmx01_0* | Sort-Object -Property FreeSpaceGB  -Descending | Select -First 1) -RunAsync

  do

  {

    sleep 5

  } while((Get-Task -Status Running | where{$_.Name -eq 'RelocateVM_Task'}).Count -gt $maxParallel)}


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

View solution in original post

Reply
0 Kudos
6 Replies
Gidrakos
Hot Shot
Hot Shot
Jump to solution

It looks like you need to get the string literal for the name rather than the AnnotatedEntity object which contains the name.

Add -expandProperty to your Select-Object call in $vms =  Get-Cluster MSCTW06 | Get-VM | Get-Annotation -Name vrmManagedMachine | Where {$_.Value -ne "True"} | Select-Object -Name 'AnnotatedEntity' |Sort 'AnnotatedEntity' | ft -hidetableheaders - And see if that fixes anything.

Best way to test this would be to simply return $vms after running the command to see what it gives back to you.

Replicated your problem and successfully tested it with that fix.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are using the result of a Select-Object, which produces objects ready for output, as input to the Get-VM Name parameter.

The Name parameter expects a string.

What is the purpose of filter on the Custom Attribute?


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

Reply
0 Kudos
tjc516
Contributor
Contributor
Jump to solution

Thank you Gidrakos, i'll give it a shot.

Reply
0 Kudos
tjc516
Contributor
Contributor
Jump to solution

The custom attribute is to only migrate VM's that are not managed by vRA.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, then try like this

$maxParallel = 3

$vms =  Get-Cluster MSCTW06 | Get-VM | where{(Get-Annotation -Entity $_ -Name vrmManagedMachine).Value-ne "True"}

$newCluster = Get-Cluster -Name MSSWVS01


foreach($vm in $vms){

  Move-VM -VM $vm -Destination $newCluster -Datastore (Get-Datastore -Name msswvs01_pmx01_0* | Sort-Object -Property FreeSpaceGB  -Descending | Select -First 1) -RunAsync

  do

  {

    sleep 5

  } while((Get-Task -Status Running | where{$_.Name -eq 'RelocateVM_Task'}).Count -gt $maxParallel)}


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

Reply
0 Kudos
tjc516
Contributor
Contributor
Jump to solution

Thank you both very much.  Both solutions worked.

Reply
0 Kudos