powershell - Compare Dates in loop -
i have requirement need read date csv file , compare date variable within script. however, code doesn't work when of date entries in csv file blank. thought how can store blank date in [datetime]
variable?
here's part of code:
#this date variable in script $inactivitydate = (get-date).adddays(-62).date import-csv $mycsv | foreach { #this date variable i'm reading csv file $whencreated = $_.whencreated #this converts value in string variable $whencreated [datetime] format $convertwhencreated = ([datetime]::parseexact($whencreated,"dd/mm/yyyy",$null)) #comparing dates if($convertwhencreated -le $inactivitydate) { "account dormant" }
above code works fine when $whencreated
contains value, when it's blank powershell cannot compare date variable blank value :( solution can see check if $whencreated
blank , save old date happens in excel, e.g.:
if($whencreated -eq "") { $whencreated = "01/01/1900 00:00:00" }
should ok or there logical solution?
your problem isn't comparison, conversion of blank value date via parseexact()
. if want accounts no date treated dormant this:
$whencreated = $_.whencreated if ($whencreated) { $whencreated = [datetime]::parseexact($whencreated, 'dd/mm/yyyy', $null) } if ($whencreated -le $inactivitydate) { 'account dormant' }
checking if empty string (or $null
) lesser or equal $inactivitydate
homecoming $true
long $inactivitydate
contains date.
date powershell
No comments:
Post a Comment