VMware Cloud Community
max2001
Contributor
Contributor
Jump to solution

VM Variable for VM object

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{$_}

})

Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

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?

View solution in original post

Reply
0 Kudos
6 Replies
kunaludapi
Expert
Expert
Jump to solution

$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

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
kunaludapi
Expert
Expert
Jump to solution

$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
--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
kunaludapi
Expert
Expert
Jump to solution

$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

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
max2001
Contributor
Contributor
Jump to solution

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....

Reply
0 Kudos
mattboren
Expert
Expert
Jump to solution

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?

Reply
0 Kudos
max2001
Contributor
Contributor
Jump to solution

Thanks Matt, script answers my question perfectly!

Reply
0 Kudos