VMware Cloud Community
an648
Enthusiast
Enthusiast
Jump to solution

How to display vm's snapshot list in a formatted way in windows event logs? Is there any alternate method to create alarm when snapshot exists more than 24 hours?

I was able to create events and display vm snaphot which exists more then 24 hours. But vms created appear in a single line as shown in first image in windows event logs. Please let me know how to get the desired out image in which vms should appear one below the other.

Also if there is any alternate method to create alarm when snapshot exists for more than 24 hours would be helpful?

Code-

$SnapVMlist = Get-VM | Get-Snapshot | Where {$_.Created -lt [datetime]::Now.AddDays(-1)} | Select VM, Name, Created

Get-VM | Get-Snapshot | Where {$_.Created -lt [datetime]::Now.AddDays(-1)} | Select-Object VM, Name, Created | Out-File -FilePath C:\Users\Administrator\VMSnapMonitor.txt

$snapcount = $SnapVMlist.Count

$file= Get-Content C:\Users\Administrator\VMSnapMonitor.txt

New-EventLog -LogName System -Source GMES2

if ($snapcount.count -gt 0){

    eventcreate /ID 102 /L SYSTEM /T ERROR /SO "GMES2" /D "$file"

}

vmsnap.jpgjavascript:;vmsnap3.jpg

Any assistance that I can get would be much appreciated.

Thank you

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can you try like this?

$text = Get-VM | Get-Snapshot | where{(New-TimeSpan -Start $_.Created -End ([DateTime]::Now)).TotalHours -gt 24} |

Select-Object -Property @{N='VM';E={$_.VM.Name}},Name,Created |

Format-Table | Out-String


Write-EventLog -LogName System -Source 'GMES2' -EntryType Error -EventId 102 -Message $text


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

View solution in original post

1 Reply
LucD
Leadership
Leadership
Jump to solution

Can you try like this?

$text = Get-VM | Get-Snapshot | where{(New-TimeSpan -Start $_.Created -End ([DateTime]::Now)).TotalHours -gt 24} |

Select-Object -Property @{N='VM';E={$_.VM.Name}},Name,Created |

Format-Table | Out-String


Write-EventLog -LogName System -Source 'GMES2' -EntryType Error -EventId 102 -Message $text


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