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{$_}
})
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:
That do it for you?
$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
$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
$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
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....
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:
That do it for you?
Thanks Matt, script answers my question perfectly!