Automation

 View Only
Expand all | Collapse all

Powercli move-vm if on a certain host

  • 1.  Powercli move-vm if on a certain host

    Posted Oct 13, 2019 11:23 AM

    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


  • 2.  RE: Powercli move-vm if on a certain host

    Posted Oct 13, 2019 03:02 PM

    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



  • 3.  RE: Powercli move-vm if on a certain host

    Posted Oct 13, 2019 03:51 PM

    Thanks for the info



  • 4.  RE: Powercli move-vm if on a certain host

    Posted Oct 13, 2019 03:53 PM

    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



  • 5.  RE: Powercli move-vm if on a certain host

    Posted Oct 13, 2019 04:03 PM

    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



  • 6.  RE: Powercli move-vm if on a certain host

    Posted Oct 13, 2019 04:27 PM

    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



  • 7.  RE: Powercli move-vm if on a certain host

    Posted Oct 13, 2019 04:35 PM

    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



  • 8.  RE: Powercli move-vm if on a certain host

    Posted Oct 14, 2019 06:31 AM

    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



  • 9.  RE: Powercli move-vm if on a certain host

    Posted Oct 14, 2019 06:40 AM

    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

    }



  • 10.  RE: Powercli move-vm if on a certain host

    Posted Oct 14, 2019 07:29 AM

    Great thanks I’ll give it a go today



  • 11.  RE: Powercli move-vm if on a certain host

    Posted Oct 14, 2019 09:17 AM

    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


  • 12.  RE: Powercli move-vm if on a certain host
    Best Answer

    Posted Oct 14, 2019 09:22 AM

    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

        }

    }



  • 13.  RE: Powercli move-vm if on a certain host

    Posted Oct 14, 2019 09:34 AM

    brill thanks

    1.0.0.20


  • 14.  RE: Powercli move-vm if on a certain host

    Posted Oct 15, 2019 09:06 AM

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

    1.0.0.20