I have a CSV with data as
Date Time Impact Warning
12/20/2021 10:00 AM Low 9:57:00 AM
12/21/2021 8:30 AM Low 8:27:00 AM
12/22/2021 8:30 AM Medium 8:27:00 AM
I have a timer loop running and I want the variable- $eventsoon to be true if a Warning time is -eq to Current time(EST).
#Import CSV
$csv = Import-Csv C:\filelocation.csv
#Get eastern Timezone#
$eastern = [System.TimeZoneInfo]::convertTimeBySystemTimeZoneId([datetime]::Now, 'Eastern Standard Time')
#Format Eastern Time#
$easternTime = Get-Date $eastern -format "HH:mm:ss tt"
#Find Matching Time between Current US Eastern Time and Warningtime
$eventsoon = $csv |
Where-Object { $_.Warning -eq $easternTime} |
ForEach-Object {
Write-Host ($_.Warning,$_.Impact)
}
$eventsoon
When testing, I can trigger the -eq but nothing saves to the $eventsoon variable?
What am I missing?
Thanks for your time.
CodePudding user response:
Here is a hint on how you can approach your problem, first of all, Lee_Dailey has given you a great peace of advice: always use DateTime
to compare Dates and Times.
Another thing to note, using -eq
might not be the right comparison operator, specially to filter Time, normally you would filter by time ranges, something like: "2 minutes more than this and 2 mintues less than this..."
Here goes the hint:
$csv = @'
Date Time Impact Warning
12/20/2021 10:00 AM Low 9:57:00 AM
12/21/2021 8:30 AM Low 8:27:00 AM
12/22/2021 8:30 AM Medium 8:27:00 AM
12/22/2021 8:30 AM Medium 1:50:00 PM
'@ -replace ' ',',' | ConvertFrom-Csv
# Let's try to filter the last line of the CSV (1:50:00 PM)
$est = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
[datetime]::Now,
'Eastern Standard Time'
) # => Monday, December 20, 2021 1:48:45 PM
$result = $csv | Where-Object {
[datetime]$_.Warning -gt $est.AddMinutes(-15) -and
[datetime]$_.Warning -lt $est.AddMinutes(15)
}
# $result would yield:
Date Time Impact Warning
---- ---- ------ -------
12/22/2021 8:30 AM Medium 1:50:00 PM
# If you want to see just a True / False
[bool]$result # => True