gbouziden
Enthusiast
Enthusiast

Export list of VMs with certain notes

Jump to solution

I'm trying to filter out the VMs that have a certain note and export them to a CSV. However, the CSV seems to only pick up a couple hundred and I know there are 1000+

Also, is there a powercli command to find all VMs created from a specific template? I think that would be more accurate.

 

 

 

Get-Module -Name VMware* -ListAvailable | Import-Module -Force
Connect-VIServer -Server ip.addr -User 'username'
 
$exportto = "C:\Users\username\Desktop\templateVMs.csv"
 
$VMs = Get-Cluster -name cluster_name | Get-VM | Where -Property Notes -like noteString*
 
$RS = foreach ($VM in $VMs){
    Write-Output $VM
}
 
$RS | Export-Csv -Path $exportto -UseCulture -NoTypeInformation

 

 

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Seems the template information is only available in the VmDeployedEvent, not the VMCreatedEvent.
You can do something like this.
Note that I used the Start parameter, otherwise the Get-VIEvent might run for a very long time.

Get-VM |
Get-VIEvent -Start (Get-Date).AddDays(-1) |
Where-Object { $_ -is [VMware.Vim.VmDeployedEvent] -and $_.SrcTemplate.Name } |
Sort-Object CreatedTime -Descending |
Select CreatedTime,
  @{N='VM';E={$_.VM.Name}},
  @{N='Template';E={$_.SrcTemplate.Name}}

And if you want to check for a specific Template, you could do

$tgtTemplate = 'c7-template'

Get-VM |
Get-VIEvent -Start (Get-Date).AddMinutes(-30) |
Where-Object { $_ -is [VMware.Vim.VmDeployedEvent] -and $_.SrcTemplate.Name -eq $tgtTemplate } |
Sort-Object CreatedTime -Descending |
Select CreatedTime,
  @{N='VM';E={$_.VM.Name}},
  @{N='Template';E={$_.SrcTemplate.Name}}

 


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

View solution in original post

5 Replies
LucD
Leadership
Leadership

That -like expression only returns the VMs where the Notes field starts with "noteString".

Only via the VMCreatedEvent afaik,  and this provided you keep the Events long enough.


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

0 Kudos
gbouziden
Enthusiast
Enthusiast

@LucD  So, if I have a template named "c7-template," how would I use VMCreatedEvent to get all the VMs created from that template?

I tried this but I couldn't get it to work/think of how to structure the argument:

11/15/2021, 12:44:05 PMDeploying c7templatetest on host esxihost.business.com in DCID from template c7-template

 

 

$VMs = Get-VM | Get-VIEvent  | where {$_.Gettype().Name -eq "VmCreatedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, vm,FullformattedMessage
​

 

0 Kudos
LucD
Leadership
Leadership

Seems the template information is only available in the VmDeployedEvent, not the VMCreatedEvent.
You can do something like this.
Note that I used the Start parameter, otherwise the Get-VIEvent might run for a very long time.

Get-VM |
Get-VIEvent -Start (Get-Date).AddDays(-1) |
Where-Object { $_ -is [VMware.Vim.VmDeployedEvent] -and $_.SrcTemplate.Name } |
Sort-Object CreatedTime -Descending |
Select CreatedTime,
  @{N='VM';E={$_.VM.Name}},
  @{N='Template';E={$_.SrcTemplate.Name}}

And if you want to check for a specific Template, you could do

$tgtTemplate = 'c7-template'

Get-VM |
Get-VIEvent -Start (Get-Date).AddMinutes(-30) |
Where-Object { $_ -is [VMware.Vim.VmDeployedEvent] -and $_.SrcTemplate.Name -eq $tgtTemplate } |
Sort-Object CreatedTime -Descending |
Select CreatedTime,
  @{N='VM';E={$_.VM.Name}},
  @{N='Template';E={$_.SrcTemplate.Name}}

 


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

gbouziden
Enthusiast
Enthusiast

Yeah that worked well, thanks. You are right though, it takes quite a while. 

0 Kudos
LucD
Leadership
Leadership

You might want to have a look at my Get-VIEventPlus function from Get the vMotion/svMotion history.
It allows you to specify an EventType, which drastically reduces the time needed.


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

0 Kudos