Automation

 View Only
  • 1.  VM Variable for VM object

    Posted Apr 14, 2014 07:18 AM

    HI,

    I am connected to multiple vcenters where we have some VM objects that have the same name (no duplicates in the same vcenter).

    and I need to run a get-vm while connected to the 2 vcenters and store that in a variable $vms

    it is always the case for VMs with the same name that if a VM is powered on in one vcenter, it is powered off in the other vcenter. (DR)

    for those VMs in that case, I need my variable to store the VM object that is powered on AND ignore the powered off ones... for other VMs, I need powered on and powered off vms to be listed.

    here is what i am doing but it is not working

    $vms = (get-vm | ForEach-Object {

    if($_.name -eq $null){

    $_ = ($_ |  Where-Object {$_.powerstate -eq "poweredon"}) }else{$_}

    })



  • 2.  RE: VM Variable for VM object

    Posted Apr 14, 2014 08:52 AM

    $poweredoff = @()

    $poweredon = @()

    $vmlist =  Get-VM

    ForEach ($vm in $vmlist) {

        if ($vm.powerstate -eq  "Poweredon") {

            $poweredon += $vm | select name, Powerstate, @{N="vcenter"; E={$_.extensiondata.Client.ServiceUrl.Split('/')[2].trimend(":443")}}

            }

        else {

            $poweredoff += $vm | select name, Powerstate, @{N="vcenter"; E={$_.extensiondata.Client.ServiceUrl.Split('/')[2].trimend(":443")}}

          }

    }

      

    $poweredoff

    $poweredon



  • 3.  RE: VM Variable for VM object

    Posted Apr 14, 2014 12:09 PM

    $vms = Get-VM | Select Name, Powerstate, @{N="vcenter"; E={$_.extensiondata.Client.ServiceUrl.Split('/')[2].trimend(":443")}}

    -----------------------------OR------------------------------------------

    $vminfo = @()

    $vmlist =  Get-VM

    ForEach ($vm in $vmlist) {

            $vminfo += $vm | select name, Powerstate, @{N="vcenter"; E={$_.extensiondata.Client.ServiceUrl.Split('/')[2].trimend(":443")}}

    }

     

    $vminfo | Sort-Object Powerstate


  • 4.  RE: VM Variable for VM object

    Posted Apr 14, 2014 12:12 PM

    $vms = Get-VM | Select Name, Powerstate, @{N="vcenter"; E={$_.extensiondata.Client.ServiceUrl.Split('/')[2].trimend(":443")}}

    -----------------------------OR------------------------------------------

    $vminfo = @()

    $vmlist =  Get-VM

    ForEach ($vm in $vmlist) {

            $vminfo += $vm | select name, Powerstate, @{N="vcenter"; E={$_.extensiondata.Client.ServiceUrl.Split('/')[2].trimend(":443")}}

    }

     

    $vminfo | Sort-Object Powerstate



  • 5.  RE: VM Variable for VM object

    Posted Apr 14, 2014 12:17 PM

    Thanks,

    The above would still return the VM that is powered off, I need the script to "NOT" return the powered off VM if there is one with the same name that is powered on...

    apologies if i did not make this clear enough....



  • 6.  RE: VM Variable for VM object
    Best Answer

    Posted Apr 16, 2014 02:15 AM

    Hello, max2001-

    How about something like the following:

    ## get all VMs from all connect vCenters, and group them by name
    $arrAllVMs_grouped = Get-VM | Group-Object Name

    $arrDesiredVMs = @()
    ## for any group of VMs where there is more than one (2, presumably), only return the PoweredOn VM in the group
    $arrDesiredVMs = $arrAllVMs_grouped | ?{$_.Count -gt 1} | %{$_.Group | ?{$_.PowerState -eq "PoweredOn"}}
    ## and for the groups of VMs where there is only one VM in the group, add that VM to the DesiredVMs array
    $arrDesiredVMs += $arrAllVMs_grouped | ?{$_.Count -eq 1} | %{$_.Group}

    That will:

    1. get all VMs, and group by name
    2. of the groups of VMs where there are more than one, just grab the ones that are in the PoweredOn PowerState
    3. and, grab all the VMs whose groups are of count 1 (uniquely named VMs), without regard to their PowerState


    That do it for you?



  • 7.  RE: VM Variable for VM object

    Posted Apr 16, 2014 05:51 AM

    Thanks Matt, script answers my question perfectly!