简体   繁体   中英

Importing Data From CSV in Powershell Providing UK Date in CSV is Less Than 7 Days Away

so I've got a CSV file with 5 headings: name, collectionDate, location, equipmentNotes, manager.

Now my collectionDates are all in UK format so DD/MM/YYYY. I only want to import data from the CSV with collection dates that are 7 days or less in the future. So today is 17/08, therefore I would only want to pull data from the CSV that is dated between 17/08 to 24/08.

Although Powershell sort of handles datetime objects in UK format if you tell it to, I seem to be unable to then manipulate the date to add on 7 days.

Here is my current code:

$today = Get-Date
$thisWeek = $today.AddDays(7)
$thisWeekString = $thisWeek.ToString()
$dateParts = $thisweekString -split " "
$weekFinal = $dateParts[0]

$import = Import-Csv @("\\location\EmailCSV.csv") | Where-Object {$_.collectionDate -lt $weekFinal}

Powershell correctly adds 7 days to the datetime to make it 24/08, and then when converting it to a string and removing the time from it, it correctly sets the variable as 24/08/2018. But when I then go to compare them in the Import cmdlet, it just returns all data in the CSV, rather than dates less than 24/08.

I also know Powershell can compare these, because if I create a separate variable $otherDate with 24/08/2018 in it, and then create an if statement that checks if $weekFinal is greater or less than $otherDate, it correctly runs the statement when true.

I've tried using both Where and Where-Object in the Import-Csv cmdlet, but both have returned the same results.

How do I get Powershell to correctly compare $_.collectionDate and $weekFinal to filter the imported data from the csv?

It is easiest to perform all calculations and comparisons using [datetime] instances - avoid string operations (except for converting a string representation of a date to a [datetime] instance).

First, express today's date and 1 week from now as a [datetime] instance without a time-of-day component:

$today = (Get-Date).Date  # Today's date, without a time component (midnight)
$oneWeekFromToday = $today.AddDays(7)

Then use [datetime]::Parse() to convert the invariably string-typed CSV column value of interest to a [datetime] instance so you can perform proper date-comparison.

$import = Import-Csv \\location\EmailCSV.csv | Where-Object { 
  $thisDate = [datetime]::Parse($_.collectionDate)
  # Process this row if its date falls between today and 1 week from now, inclusively.
  $thisDate -ge $today -and $thisDate -le $oneWeekFromToday
}

Important : use [datetime]::Parse('...') , not a [datetime] '...' cast , because only [datetime]::Parse() respects the current culture 's date and time formats; by design, PowerShell's casts and string interpolation always use the invariant culture , irrespective of the current culture - for more information, see this answer of mine.

Try casting the strings to [datetime] in your comparison. When using a sample set of data, I was able to get the expected results.

$import = Import-Csv @("\\location\EmailCSV.csv") | Where-Object {[datetime]$_.collectionDate -lt [datetime]$weekFinal}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM