VMware Cloud Community
matthewcdove
Contributor
Contributor

Create an Event Log entry in lieu of email

I have the following script.  It was originally intended to send an email with teh results of the script.  I need to change it from emailing the data to writing an event log entry that monitoring software can pick up instead.

Does anyone know how to create a log entry instead?  I'm completely lost as to where to start with that.

Here is the script, I left out the part taht defines the variables as it isn't relevant....


$Snapshots = @()
$SnapDateTreshold = (Get-Date).AddHours(-1 * $SnapshotAge)
# Get all VMs with snapshots
ForEach ($Snap in (Get-View -ViewType VirtualMachine -Filter @{"Snapshot" = ""} | Get-VIObjectbyVIView | Get-Snapshot)) {
If ($Snap.Created -le $SnapDateTreshold) {
  $ThisSnap = New-Object PSObject -Property @{
   VM = $Snap.VM
   Name = $Snap.Name
   Description = $Snap.Description
   CreateDate = $Snap.Created
   SizeGB = [math]::Round($Snap.SizeGB, 2)
  }
  $Snapshots += $ThisSnap
}
}

If ($Snapshots.Length -gt 0) {
$HTMLHead = "<style>`nbody{font-family: Verdana, sans-serif;}`ntable{border-collapse:collapse;}`ntable,th,td{border: 1px solid black;padding: 5px;}`nth{color: #FFF; background-color: #A40000}`n</style>"
$HTMLTitle = "Snapshot Report for $VIServer"
$HTMLBody = "<h2>Snapshot Report for $VIServer</h2>`nThe following snapshots have been active for more than $SnapshotAge hours. Please verify if they are needed and remove them as soon as possible.<br/><br/>"
$EmailBody = "$($Snapshots | ConvertTo-Html -Head $HTMLHead -Title $HTMLTitle -Body $HTMLBody VM,Name,Description,CreateDate,SizeGB)"
    Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body $EmailBody -SmtpServer $EmailServer -BodyAsHtml
} else {
Write-Host "No snapshots older than $SnapshotAge hours found"
}

0 Kudos
1 Reply
LucD
Leadership
Leadership

You can create a user event like this.

You can post the event against any entity in your vSphere environment

$si = Get-view ServiceInstance

$evtMgr = Get-View $si.Content.EventManager

$vm = Get-VM -Name MyVM

$evtMgr.LogUserEvent($vm.ExtensionData.MoRef,"My event text")


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

0 Kudos