VMware Cloud Community
FABSAN
Contributor
Contributor

get-vm where statement to find windows with particular network

Hi, i've been trying to figure this out. any help would be appreciated. i'm trying to query vcenter to only output windows vms with a particular network assigned. for instance, find all windows vms with "QA Network"

To get windows vms i got

get-vm | where {$_.guest.osfullname -like "*windows*"}

but cant figure out how to add the network part. I need the output to contain VMName,VMHost,PowerState,OS,Cluster,IPAdress,Pool (resource pool)

Thanks in advance.

0 Kudos
9 Replies
LucD
Leadership
Leadership

Try something like this

Get-VM | 
where {$_.guest.osfullname -like "*windows*" -and (Get-NetworkAdapter -VM $_).NetworkName -eq "QA Network"} | 
Select Name,Host,PowerState,OSName,
  @{N="Cluster";E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},
  @{N="IP";E={[string]::Join(',',($_.Guest.IPAddress))}},
  @{N="ResourcePool";E={Get-ResourcePool -VM $_ | Select -ExpandProperty Name}}


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

FABSAN
Contributor
Contributor

hi LucD

thanks, its not returning any results. when i execute the below it does give results so i know "qa networks" works, just not in the format i need. i verified i do have windows vms with "QA Network". its vDS, not sure if that matters.

get-vm | get-networkadapter | where {$_.networkname -eq "QA Network"}

any ideas? much appreciated.

0 Kudos
LucD
Leadership
Leadership

Let's verify if you have any VM that fullfil the conditions.

We can just display the 2 properties

Get-VM | 
select Name,@{N="OS";E={$_.Guest.OsFullname}}, 
@
{N="Network";E={Get-NetworkAdapter -VM $_ | Select -ExpandProperty NetworkName}}


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

FABSAN
Contributor
Contributor

It comes back with "Name" "OS" "Newtork" with results, server name, os and all the network names.

then it errors at "Select -ExpandProperty". The error is "runtime error, cannot process argument because the value of argument "obj" is null. change the value of argument "obj" to a non-null value".

0 Kudos
LucD
Leadership
Leadership

That could mean that there are 1 or more VM that are not running the VMware Tools or are powered off.

But for the ones that do return data, are there any there that fit the condition ?


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

0 Kudos
FABSAN76
Contributor
Contributor

your right, there a number of powered off vms. there is at least one windows server that fit the criteria. i confirmed by looking up the resulting mac addresses. the network label on the vm has "org management network". its nic 2 on the VM, not sure if that matters

this is what i used.

i get results with this

get-vm | get-networkadapter | where {$_.networkname -like "*org*"}  (using a different network name)

not with this.

get-vm | where {$_.guest.osfullname -like "*windows*" -and (get-networkadapter -vm $_.networkname -like "*org*"}

thanks in advance

0 Kudos
LucD
Leadership
Leadership

That would mean that the condition '$_.guest.osfullname -like "*windows*"' didn't return $true.

That could be caused by the VMware Tools not running.

Can you check with

Get-VM | Select Name,@{N="OS";E={$_.guest.osfullname}}

Since we use an -and, both conditions have to be $true !


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

0 Kudos
FABSAN
Contributor
Contributor

finally got it to work. i replaced the ( ) with { }, and putting the closing in the end, (red below)

the only missing piece is resouce pool which is not showing up in the output. shouldnt be too hard for me to figure out. thanks for your help!

Get-VM | where {$_.guest.osfullname -like "*windows*" -and {Get-NetworkAdapter -VM $_.networkname -like "*org*"}} | select Name,VMHost,Powerstate,OSName,
@{N="Cluster";E={Get-Cluster -VM $_ |select -ExpandProperty name}},
@{N="IP";E={[string]::Join(',',($_.Guest.IPAddress))}},
@{N="ResourcePool";E={Get-ResourcePool -VM $_ | Select -ExcludeProperty}}

0 Kudos
LucD
Leadership
Leadership

That last line should be

@{N="ResourcePool";E={Get-ResourcePool -VM $_ | Select -ExpandProperty Name}}


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

0 Kudos