locked
Powershell Date Comparison Problem / Question RRS feed

  • Question

  • 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.

    • Moved by Bill_Stewart Thursday, September 25, 2014 8:56 PM Unanswerable drive-by question
    Sunday, September 7, 2014 2:36 AM

Answers

  • Here is how we would handle both conditions.

    Import-Csv D:\Test\HR\BenefitsNotification\List\Master.csv |
    	ForEach-Object{
    	    if([DateTime]::Today -ge [DateTime]($_.DateP60 -and [DateTime]::Today -lt [DateTime]($_.$DateP75) ){
    		  # send 60 dat email
    		}elseif([DateTime]::Today -ge [DateTime]($_.$DateP75) )
    		  # send 75 day email		
    		}
    	}
    
    I am not going to write and debug this for you.  You will be able to do it yourself after the tutorials because then you will understand what I have shown you.


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Sunday, September 7, 2014 3:36 AM
    • Marked as answer by Just Karl Wednesday, April 15, 2015 9:21 PM
    Sunday, September 7, 2014 3:11 AM

All replies

  • I think you would do well by reading the basics of PowerShell.  You seem to have a lot of ideas that are very fr away from anything reasonable.  I cannot even begin to guess what you are trying to copy.

    To start with this is pointless.  It does nothing useful.

    $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
        }

    This is how we would do it:

    Import-Csv D:\Test\HR\BenefitsNotification\List\Master.csv |
    	Where-Object{
    		[DateTime]::Today -ge [DateTime]($_.DateP60) -and [DateTime]::Today -lt [DateTime]($_.$DateP75)
    	} |
        ForEach-Object{ $_.Email }

    Of course that only does one case but it demonstrates how dates and logic work.  You need to spend sometime learning basic logic.

    if(<true>)  ---- does not need to be if(<true> -eq $true) 

    That is just silly. In computers double negatives and double positives are  just silly.

    Date time objects to not need to be converted to strings then back into objects then back into strings.  Just think about how silly that is.  It is like driving your car to a mall taking a bus back home so you can then walk to the mall and have your car available to take you home.

    You cannot learn computer technology and programming by guesswork.  Start by taking a tutorial and do all exercises until you fully understand.

    Start here: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    I know that this is hard if you have no background or training in how software based systems work.  You can learn it but it takes a bit of initial effort.



    ¯\_(ツ)_/¯


    • Edited by jrv Sunday, September 7, 2014 3:07 AM
    Sunday, September 7, 2014 3:06 AM
  • Here is how we would handle both conditions.

    Import-Csv D:\Test\HR\BenefitsNotification\List\Master.csv |
    	ForEach-Object{
    	    if([DateTime]::Today -ge [DateTime]($_.DateP60 -and [DateTime]::Today -lt [DateTime]($_.$DateP75) ){
    		  # send 60 dat email
    		}elseif([DateTime]::Today -ge [DateTime]($_.$DateP75) )
    		  # send 75 day email		
    		}
    	}
    
    I am not going to write and debug this for you.  You will be able to do it yourself after the tutorials because then you will understand what I have shown you.


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Sunday, September 7, 2014 3:36 AM
    • Marked as answer by Just Karl Wednesday, April 15, 2015 9:21 PM
    Sunday, September 7, 2014 3:11 AM
  • Here is a good demo of how PowerShell works with loops and collections.

    Import-Csv D:\Test\HR\BenefitsNotification\List\Master.csv |
        ForEach-Object{
            '60DAY:{0}  75DAY:{1} EMAIL:{2}' -f [DateTime]($_.DateP60),[DateTime]($_.DateP75),$_.email
        }
    


    ¯\_(ツ)_/¯

    Sunday, September 7, 2014 3:36 AM