$Start = (Get-Date -Hour 0 -Minute 0 -Second 0).AddDays(-4)
$Finish = (Get-Date -Hour 0 -Minute 0 -Second 0)
$VIEvent = Get-VIEvent -Start $Start -Finish $Finish -MaxSamples ([int]::MaxValue)
$export = foreach ($v in $vievent){
$Level1 = $v | where {
($_.FullFormattedMessage -like '*Your guest has entered a standby sleep state*') -or
($_.FullFormattedMessage -like '*Set console window screen resolution*')-or
($_.FullFormattedMessage -like '*Revert to current snapshot*') -or
($_.FullFormattedMessage -like '*power off virtual machine*') -or
($_.FullFormattedMessage -like '*power on virtual machine*') -or
($_.FullFormattedMessage -like '*webmks*')
}
$Level1 | where {
($_.UserName -like '*user*')-or
($_.UserName -like '*admin1*')-or
($_.UserName -like '*admin2*')-or
($_.UserName -like '*admin3*')} |
Sort-Object CreatedTime |
select @{n='VM';e={$v.VM.Name}}, CreatedTime,Username,FullFormattedMessage}
if ($export -eq $null){
Add-Type -AssemblyName System.Windows.Forms
$global:balmsg = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
$balmsg.BalloonTipText = ‘VM Report is empty'
$balmsg.BalloonTipTitle = "Empty Report!"
$balmsg.Visible = $true
$balmsg.ShowBalloonTip(20000)}
else {
$export | Sort-Object CreatedTime -Descending |
Export-csv -path "$folder\Weekly_$NOW.csv" -NoTypeInformation
Add-Type -AssemblyName System.Windows.Forms
$global:balmsg = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
$balmsg.BalloonTipText = "There is an updated report available in $folder"
$balmsg.BalloonTipTitle = "New Report Available!"
$balmsg.Visible = $true
$balmsg.ShowBalloonTip(20000)}
}