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.
The output is what I want, but I just think it can be improved greatly. Thanks in advance for any help.
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
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
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