Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell Date Comparison Problem / Question

$
0
0

Hello,

I am having trouble with a script that I am developing and I am getting stuck on a date comparison.  I'm on the newer side when it comes to Powershell and have been trying to do a lot of reading and research before posting here.  However, after a few days I have not found much and was hoping that someone here would be able to assist.  

At the end of the day, this script will run once every two weeks.  It will send an email to an end user approximately 60 days after their hire date and then again at about 75 days.  I have a CSV that stores first name, last name, email, hire date, hire date + 60 and hire date +75.

This is my script, or at least the relevant part.

$First = @()
$Last = @()
$Email = @()
$Date = @()
$DateP60 = @()
$DateP75 = @()

$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToShortDateString()

$NeedEmail = @()

Import-Csv D:\Test\HR\BenefitsNotification\List\Master.csv |`
    ForEach-Object {
        $First += $_.First
        $Last += $_.Last
        $Email += $_.Email
        $Date += $_.Date
        $DateP60 += $_.DateP60
        $DateP75 += $_.DateP75
    }

#---   Step 2
for($b=0; $b -lt $First.length; $b++)
 {
   If (([DateTime]::Parse($CurrentDate) -ge [DateTime]::Parse($DateP60[$b])) -eq $True)
    {
      If (([DateTime]::Parse($CurrentDate) -lt [DateTime]::Parse($DateP75[$b])) -eq $true)
       {
         $NeedEmail += $Email[$b]
       }
      If (([DateTime]::Parse($CurrentDate) -ge [DateTime]::Parse($DateP75[$b])) -eq $true)
       {
         $NeedEmail += $Email[$b]
         Add-Content D:\test\HR\BenefitsNotification\List\Temp.csv -Value "$Email[$b]"
       }
    }
 }

This is the error I am getting

Exception calling "Parse" with "1" argument(s): "The string was not recognized as a valid DateTime. There is an unknown word starting at index 
9."
At line:87 char:11
+       If (([DateTime]::Parse($CurrentDate) -lt [DateTime]::Parse($DateP75[$b]))  ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException
 
Exception calling "Parse" with "1" argument(s): "The string was not recognized as a valid DateTime. There is an unknown word starting at index 
9."
At line:91 char:11
+       If (([DateTime]::Parse($CurrentDate) -ge [DateTime]::Parse($DateP75[$b]))  ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

The line numbers referenced in the error are the second and third parse.

I appreciate any and all assistance.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>