VMware Cloud Community
thg_jimleslie
Contributor
Contributor
Jump to solution

Powercli move-vm if on a certain host

Hi and thanks in advance for the help.  I need to use power cli to move a vm if it resides on a certain esxhost (we are storage migrating some VM's and if they reside on a problematic host it crashes)  could anybody please assist with this line?

Thanks

Jim

1.0.0.20
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Something like this?

$problemEsx = 'ProblemEsx1','ProblemEsx2','ProblemEsx3'

$newDS = '???'


Get-VM -Name (Get-Content -Path '.\vmnames.txt') -PipelineVariable vm |

ForEach-Object -Process {

    if($problemEsx -notcontains $vm.VMHost.Name){

        Move-VM -VM $vm  -Datastore $newDS -Confirm:$false

    }

}


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

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this.

For the target ESXi node you have multiple options. This example just picks another ESXi node.

$esxName = 'ProblemEsx'

$tgtEsx = 'MyEsx'


Get-VMHost -Name $esxName | Get-VM | Move-VM -Destination $tgtEsx -Confirm:$false


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

0 Kudos
thg_jimleslie
Contributor
Contributor
Jump to solution

Thanks for the info

0 Kudos
thg_jimleslie
Contributor
Contributor
Jump to solution

That would effectively be the wrong way round.  I simply want to move vm If not in problem esx host.  If on problem host cancel resquest

0 Kudos
LucD
Leadership
Leadership
Jump to solution

So you want to move the VM when it is not on the problematic ESXi node?
Where do you want to move it to? The problematic ESXi node?

If yes, replace $tgtEsx by $esxName on the Move-VM cmdlet.

You could do

$esxName = 'ProblemEsx'

$tgtEsx = 'MyEsx'


Get-VMHost | where{$_.Name -ne $esxName} |

Get-VM | Move-VM -Destination $tgtEsx -Confirm:$false


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

0 Kudos
thg_jimleslie
Contributor
Contributor
Jump to solution

So what I need to do is to storage vmotion vm.   It we have 3 problem esx hosts.  Should the vms reside on one of those hosts and we storage vmotion the host fails so we want to check if vms is on bad host, vmotion vm to a good host then storage vmotion

thabks I advance

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Your description is rather confusing I'm afraid.


My last try on interpreting what you want to do.

  • Get all the VMs on the 3 problematic ESXi nodes
  • Move (vMotion) those VMs to another ESXi node (which one or how to determine you didn't clarify)
  • Then move (svMotion) these VMs to a new datastore (which one or how to determine you didn't clarify)

$problemEsx = 'ProblemEsx1','ProblemEsx2','ProblemEsx3'

$goodEsx = '???'

$newDS = '???'


Get-VMHost -Name $problemEsx | Get-VM |

Move-VM -Destination $goodEsx -Confirm:$false |

Move-VM -Datastore $newDS -Confirm:$false


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

0 Kudos
thg_jimleslie
Contributor
Contributor
Jump to solution

Hi not quit.  We will not be doing it in an all vm approach we have a list and would like it as a check so would be more like this

1 is vm hosted on problem host

yes- move to another host

then storage move

no- vm is on a good host storage move

if we took the approach to move all of the host we could simplify that and place the host into maintenance mode, but we don’t have the capacity to loose a host at this point.

thanks in advance

Jim

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.
The names of the VMs are in the .txt file, one name per line.

$problemEsx = 'ProblemEsx1','ProblemEsx2','ProblemEsx3'

$goodEsx = '???'

$newDS = '???'


Get-VM -Name (Get-Content -Path '.\vmnames.txt') -PipelineVariable vm |

ForEach-Object -Process {

    if($problemEsx -contains $vm.VMHost.Name){

        $vm = Move-VM -VM $vm -Destination $goodEsx -Confirm:$false

    }

    Move-VM -VM $vm  -Datastore $newDS -Confirm:$false

}


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

0 Kudos
thg_jimleslie
Contributor
Contributor
Jump to solution

Great thanks I’ll give it a go today

0 Kudos
thg_jimleslie
Contributor
Contributor
Jump to solution

another approach we are thinking of, if the VM resides on aproblem vm, leave it where it is and move on to the next vm in the text file list.  how can i just move on.

Thanks

Jim

1.0.0.20
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Something like this?

$problemEsx = 'ProblemEsx1','ProblemEsx2','ProblemEsx3'

$newDS = '???'


Get-VM -Name (Get-Content -Path '.\vmnames.txt') -PipelineVariable vm |

ForEach-Object -Process {

    if($problemEsx -notcontains $vm.VMHost.Name){

        Move-VM -VM $vm  -Datastore $newDS -Confirm:$false

    }

}


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

0 Kudos
thg_jimleslie
Contributor
Contributor
Jump to solution

brill thanks

1.0.0.20
0 Kudos
thg_jimleslie
Contributor
Contributor
Jump to solution

That did what i required it to do.  thanks for your help, much appreciated

1.0.0.20
0 Kudos