VMware Cloud Community
crosen
Contributor
Contributor

List VMs per VLAN and creation date

I want to be able to specify a given subnet and list all of the VMs that are on it.  Ideally I'd like to list the creation date as well.  Thanks.

0 Kudos
4 Replies
LucD
Leadership
Leadership

Are you looking for something like this

$subnet = "192.168.1" 
foreach($vm in Get-VM){   $vm.Guest.IPAddress | where {$_ -like ($subnet + ".*")} |
 
Select @{N="VM";E={$vm.Name}},@{N="IP";E={$_}} }

The creation date of a VM is more tricky.

That info is not present in the VM properties, but you could look through all the events (with Get-VIEvent) and try find the event that corresponds with the creation of the VM.

The script assumes that you have the VMware Tools installed on all of your VMs.


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

0 Kudos
crosen
Contributor
Contributor

I'd rather do it by the network label (i.e., ProdVLAN100) instead of the IP.

0 Kudos
crosen
Contributor
Contributor

How do I merge the first part that gets me the VM name and IP with the part below that gets the Notes field?

$subnet = "192.168.10"
&{foreach($vm in Get-VM){
  $vm.Guest.IPAddress | where {$_ -like ($subnet + ".*")} |
  Select @{N="VM";E={$vm.Name}},@{N="IP";E={$_}}
}} | Export-Csv "$filename1" -NoTypeInformation -UseCulture

and

#$report = @()
#foreach($vm in Get-VM){
#    $guest = Get-VMGuest -VM $vm
#    $row = "" | Select VMName,Notes,IPAddr
#    $row.VMName = $vm.Name
#    $notesfield = $VM.Description
#    $row.Notes = ($notesfield -replace ",",";")
#    $row.IPAddr = $guest.IPAddress[0]
#    $report += $row   
#}

#$report | Export-Csv "$filename1" -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership

Just add another calculated property to the 1st script

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


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

0 Kudos