VMware Cloud Community
DZ1
Hot Shot
Hot Shot
Jump to solution

Trying to make this more elegant

I'm trying to make the below script more elegant and more efficient.  I have created reports where I'm only using 1 type of object (Only VM related events), but now I'm trying to use VM, ESXi, and other info, and I'm not sure if this is the way to do it. 

$vms = Get-VM
$ESXihosts = Get-VMHost
$totalESXiHost = ($ESXihosts).count
$totalVMs = ($vms).count
$totalVMsOff = ($vms | where { $_.powerstate -eq "PoweredOff" }).count
$snapsTotal = ($vms | Get-Snapshot).count
$Report = "" | Select ESXiHosts, VMs, "Total Snapshots"
$Report.esxiHosts = $totalESXiHost
$Report.VMs = "$totalVMs (Of those $totalVMs, $totalVMsoff are powered off)"
$Report."Total Snapshots" = $snapsTotal
Write $Report | fl
"VMs created over the past 14 days"
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14) | where { $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmBeingClonedEvent" -or $_.Gettype().Name -eq "VmBeingDeployedEvent" } | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage | fl
"VMs removed over the past 14 days"
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14) | where { $_.Gettype().Name -eq "VmRemovedEvent" } | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage | fl

The output is what I want, but I just think it can be improved greatly.  Thanks in advance for any help. 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The biggest improvement is probably eliminating the 2 Get-VIEvent calls with 1 call.

This

"VMs created over the past 14 days" 
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14) | where { $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmBeingClonedEvent" -or $_.Gettype().Name -eq "VmBeingDeployedEvent" } | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage | fl
"
VMs removed over the past 14 days"
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14) | where { $_.Gettype().Name -eq "VmRemovedEvent" } | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage | fl

could be done like this

$events = Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14)
"VMs created over the past 14 days" 
$events
| where { $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmBeingClonedEvent" -or $_.Gettype().Name -eq "VmBeingDeployedEvent" } | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage | fl
"
VMs removed over the past 14 days"
$events | where { $_.Gettype().Name -eq "VmRemovedEvent" } | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage | fl


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The biggest improvement is probably eliminating the 2 Get-VIEvent calls with 1 call.

This

"VMs created over the past 14 days" 
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14) | where { $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmBeingClonedEvent" -or $_.Gettype().Name -eq "VmBeingDeployedEvent" } | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage | fl
"
VMs removed over the past 14 days"
Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14) | where { $_.Gettype().Name -eq "VmRemovedEvent" } | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage | fl

could be done like this

$events = Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(-14)
"VMs created over the past 14 days" 
$events
| where { $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmBeingClonedEvent" -or $_.Gettype().Name -eq "VmBeingDeployedEvent" } | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage | fl
"
VMs removed over the past 14 days"
$events | where { $_.Gettype().Name -eq "VmRemovedEvent" } | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage | fl


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

Reply
0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

If I didn't see you at VMWorld, I would swear that you actually lived inside a computer, never sleeping, never resting.  I will make that change, Thanks Smiley Happy

Reply
0 Kudos