VMware Cloud Community
crosen
Contributor
Contributor

Consolidated list VMs by subnet

I'm running through thousands of VMs to get a list of VMs on specific networks.  Currently it is looping through each time for each of the given subnets, but I want to consolidate so it looks for A or B.

Now:

$subnet = "1.2.3"
&{foreach($vm in Get-VM){
  $vm.Guest.IPAddress | where {$_ -like ($subnet + ".*")} |  Select @{N="VM";E={$vm.Name}},@{N="IP";E={$_}},@{N="OS";E={$vm.Guest.OSFullName}},  @{N="Notes";E={$vm.Description.Replace(',','')}}
}} | Out-File "$filename1" -Append
$subnet = "1.2.4"
&{foreach($vm in Get-VM){
  $vm.Guest.IPAddress | where {$_ -like ($subnet + ".*")} |  Select @{N="VM";E={$vm.Name}},@{N="IP";E={$_}},@{N="OS";E={$vm.Guest.OSFullName}},  @{N="Notes";E={$vm.Description.Replace(',','')}}
}} | Out-File "$filename1" -Append

Trying:

$subnet1 = "1.2.3"
$subnet2 = "1.2.4"
&{foreach($vm in Get-VM){
  $vm.Guest.IPAddress | where {$_ -like (($subnet1 + ".*") or ($subnet2 + ".*"))} |  Select @{N="VM";E={$vm.Name}},@{N="IP";E={$_}},@{N="OS";E={$vm.Guest.OSFullName}},  @{N="Notes";E={$vm.Description.Replace(',','')}}
}} | Export-Csv "$filename1" -NoTypeInformation -UseCulture

Get:

Unexpected token 'or' in expression or statement.
At C:\MyDocs\work\Virtualization\VMWare\VMware_Healthcheck\Compliant_Network_Re
port\co_network_vms6_all.ps1:12 char:62
+   $vm.Guest.IPAddress | where {$_ -like (($subnet1 + ".*") or <<<<  ($subnet2
+ ".*"))} |  Select @{N="VM";E={$vm.Name}},@{N="IP";E={$_}},@{N="OS";E={$vm.Gu
est.OSFullName}},  @{N="Notes";E={$vm.Description.Replace(',','')}}
    + CategoryInfo          : ParserError: (or:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

Thanks.

0 Kudos
1 Reply
LucD
Leadership
Leadership

Try -or instead of or.


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

0 Kudos