VMware Cloud Community
jamie20
Enthusiast
Enthusiast
Jump to solution

Change date time format in Get-hvevent

Hi guys,

I tried a script of collecting horizon errors for a day. The script is working but the date format is horrible. 

Need to change it as dd-mm-yyyy hh:mm format.

Below is the script:

#EventDB

$events=Get-HVEvent -HvDbServer $eventdb -TimePeriod day -SeverityFilter error
$ts = $events.events

# Create column 
$HtmlTable1 = "<table border='1' align='Left' cellpadding='2' cellspacing='0' style='color:black;font-family:arial,helvetica,sans-serif;text-align:left;'>
<tr style ='font-size:13px;font-weight: normal;background: #0789f7'>
<th align=left><b>Time</b></th>
<th align=left><b>Alert</b></th>
</tr>"

foreach ($row in $ts)
{
$HtmlTable1 += "<tr style='font-size:13px;background-color:#FFFFFF'>
<td>" + $row.EventTime + "</td>
<td>" + $row.Message + "</td>
</tr>"
}
$HtmlTable1 += "</table>"

# Send Mail Inputs
$smtpserver = "***************"
$from = "***************"
$to = "***************"
$subject = "Horizon Alert"
$body = $HtmlTable1

Send-MailMessage -smtpserver $smtpserver -from $from -to $to -subject $subject -body $body -bodyashtml

 

I dont where to use the .tostring() function...Any help?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Did you try with

 

([DateTime]$row.EventTime).ToString('dd-MM-yyyy HH:mm')

 


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

View solution in original post

16 Replies
LucD
Leadership
Leadership
Jump to solution

Did you try with

 

([DateTime]$row.EventTime).ToString('dd-MM-yyyy HH:mm')

 


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

jamie20
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Its working...But see the month...its not normal...

jamie20_0-1616441797963.png

How to solve this? 😶

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, that should have been MM instead of mm.
I corrected the code above


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

jamie20
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

Here  I could filter the events by only day. The default parameter types also has day,week and month.

Is there any possibility to filter last one hour events?

Get-HVEvent -HvDbServer $eventdb -TimePeriod day -SeverityFilter error

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You would have to do that with the objects returned by GetHVEvent.
You can use (Get-Date).AddHours(-1) to find the start of the last hour, then just compare all events times against that value and only select the ones that are bigger (>)


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

This didnt worked LucD...

$events=Get-HVEvent -HvDbServer $eventdb -TimePeriod ((Get-Date).AddHours(-1)) -SeverityFilter error
$events.events

Gives the below error:

jamie20_0-1616591765382.png

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is not what I meant, the TimePeriod doesn't accept a DateTime variable.
You will have to use a Where-clause after the Get-HVEvent that filters out the events for the last hour.


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Hi Lucd...I dont know how to do this...Below is the one which I tried , but it returned nothing.

 

$filterDate = (Get-Date).AddHours(-1)
$events=Get-HVEvent -HvDbServer $eventdb -TimePeriod day -SeverityFilter error | Where-Object {$_.TimePeriod -ge $filterDate}
$events.events

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you try like this?

 

$filterDate = (Get-Date).AddHours(-1)
$events = Get-HVEvent -HvDbServer $eventdb -TimePeriod day -SeverityFilter error 
    
$events.events | Where-Object {[DateTime]($_.EventTime) -ge $filterDate}

 


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

No LucD....Still the same it returned nothing...

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And when we cast the EventTime string to a DateTime object?
I updated the code above.


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

No LucD....Still the same.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are we looking at the correct property.
What does this return?

$events.events | Select EventTime,
    @{N='Type';E={$_.EventTime.GetType().Name}},
    @{N='DT';E={[DateTime]$_.EventTime}}


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

Reply
0 Kudos
jamie20
Enthusiast
Enthusiast
Jump to solution

Find the output below LucD.

jamie20_0-1616613309825.png

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That all looks ok.
You are sure there are events over the last hour?
Perhaps change to something like the last 6 or 12 hours, just to check


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

jamie20
Enthusiast
Enthusiast
Jump to solution

My Bad....Sorry LucD....I didnt think of that....Now its working.

Reply
0 Kudos