none
Prevent DateTime from being converted to 24-hour format RRS feed

  • Question

  • I have a script that grabs event log data and send the results in an email.  The time stamp of an event log entry shows up correctly in non-24hour format...for example 2:30 PM and not 14:30.

    However when I send the email, the message body contains the event date (as a variable) but this event date is converted to 24 hour format.  I believe when the DateTime object is sent to the Send-MailMessage function, it must convert it to a STRING object for the message body.

    Is there a way to prevent this or covert it back to non-24 hour format when adding it to the mail body variable?

    Thanks

    NK

    Thursday, August 28, 2014 8:21 PM

Answers

  • $dt=Get-Date
    
    $bodytext=@"
       Dear anybody.   Here is the date:
            $dt
            $($dt.ToLongDateString())
            $($dt.ToUniversalTime())
            $($dt.ToString('MM-dd-yyyy hh:mm tt'))
    
       Now you know who to keep times!
    "@


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, August 28, 2014 8:41 PM
    • Marked as answer by NJK-Work Thursday, August 28, 2014 8:47 PM
    Thursday, August 28, 2014 8:41 PM

All replies

  • You can convert it to string in whatever format you want, prior to using it in the email body. 

    Can't really be more specific than that, given the available information.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, August 28, 2014 8:27 PM
    Moderator
  • Here is my code if that helps:

    Function GetLogs ($ServerName, $LogName, $Source, $MailTo, $Trigger)
    {
     $Time = (Get-Date) - (New-TimeSpan -Hour 1)
     Get-WinEvent -FilterHashtable @{LogName=$LogName;StartTime=$Time;ProviderName=$Source} -ComputerName $ServerName | Where {($_.Level -eq 3) -or ($_.Level -eq 2)} | Select-Object TimeCreated,Message,ID -OutVariable +Events | Out-Null
     $EventsCount = $Events.Count
     if ($Events -ne $null)
     {
      if ((@($Events).Count -ge $Trigger) -and (@($Events).Count -le $KillSwitch))
      {
       ForEach ($EventItem IN $Events)
       {
        $BodyTime = $EventItem.TimeCreated
        $BodyMessage = $EventItem.Message
        $BodyEventID = $EventItem.ID
        $Body = "Server:`t`t$ServerName`n`nProblem:`t$Source`nEvent ID:`t$BodyEventID`nDate:`t`t$BodyTime`n`nEvent Message:`n`n$BodyMessage"
        Send-MailMessage -To $MailTo -From $MailFrom -SmtpServer $SMTP -Subject "$ServerName : `"$Source`" Hardware Alert" -Body $Body
       }
      }
      elseif (@($Events).Count -gt $KillSwitch)
      {
       $BodyEventID = $EventItem.EventID
       $FloodTime = [DateTime]::Now
       $Body = "Problem:`t$Source`nServer:`t`t$ServerName`n`nAn Event Log flood was detected at $FloodTime for the $Source application.  The total events detected in a $TimeRange minute period was $EventsCount which triggered a kill-switch on the script.  Please manually check the event logs on server $ServerName to determine the cause of these events and correct them."
       Send-MailMessage -To $MailTo -From $MailFrom -SmtpServer $SMTP -Subject "$ServerName : `"$Source`" Event Log Flood Alert" -Body $Body
      }
     }
    }

    Foreach ($Server in $ServerList)
    {
     GetLogs $Server "System" "HP Ethernet" "blah@blah.com" 1
     GetLogs $Server "System" "HP Fiber Channel" "blah@blah.com" 1
     GetLogs $Server "System" "HP Memory" "blah@blah.com" 1
     GetLogs $Server "System" "HP MgmtProc" "blah@blah.com" 1
     GetLogs $Server "System" "HP Processor" "blah@blah.com" 1
     GetLogs $Server "System" "HP Sensor" "blah@blah.com" 1
     GetLogs $Server "System" "HP Smart Array" "blah@blah.com" 1
     GetLogs $Server "System" "HP System" "blah@blah.com" 1
     GetLogs $Server "System" "HP Test" "blah@blah.com" 1
    }

    In the email I get, from an "HP Test" event I manually generate, the date of the event shows up as this (24 hour format):

    Date:  08/28/2014 15:34:04

    I want it to show up a 3:34:04 instead.

    Thanks

    NK

    Thursday, August 28, 2014 8:38 PM
  • And I just noticed I need to do some clean up with  "$EventsCount = $Events.Count" portion of the code.  So just ignore that little bad piece of coding on my part for the time being...I think I took two different scripts and put them together so there is some "old junk" hanging around in my code.

    NK

    Thursday, August 28, 2014 8:41 PM
  • $dt=Get-Date
    
    $bodytext=@"
       Dear anybody.   Here is the date:
            $dt
            $($dt.ToLongDateString())
            $($dt.ToUniversalTime())
            $($dt.ToString('MM-dd-yyyy hh:mm tt'))
    
       Now you know who to keep times!
    "@


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, August 28, 2014 8:41 PM
    • Marked as answer by NJK-Work Thursday, August 28, 2014 8:47 PM
    Thursday, August 28, 2014 8:41 PM
  • This worked:

    $BodyTime = ($EventItem.TimeCreated).ToString('MM-dd-yyyy hh:mm tt')

    Thank you!

    NK

    Thursday, August 28, 2014 8:46 PM