Get date isnt working properly

This topic contains 2 replies, has 2 voices, and was last updated by  Jeremy-Smit 3 months, 1 week ago.

  • Author
  • #93472


    In have the follow script:

    [system.reflection.assembly]::loadwithpartialname('microsoft.visualbasic') | Out-Null
    $Country = [microsoft.visualbasic.interaction]::inputbox('Fill in the Country name in the short OU version','Country')
    $StartDate = [microsoft.visualbasic.interaction]::inputbox('Fill in the begin date of the month','Given Date')
    $EndDate = [microsoft.visualbasic.interaction]::inputbox('Fill in the last date of the month','Last Date') 
    $fileLocation = [microsoft.visualbasic.interaction]::inputbox('give up filelocation were to save the file','Save As','c:\temp\data.csv')
    $SDate = get-date $StartDate 
    $EDate = get-date $EndDate 
    $OurUsers = Get-ADUser -SearchBase "OU=$country,DC=corp,DC=local" -filter * -Properties *
    $OurUsers | Where-Object {($_.whencreated -ge $StartDate) -and ($_.Whencreated -le $EndDate)} | 
    Select-Object @{Name="AD DisplayName";Expression={$_.CN}}, @{Name="Accountname";Expression={$_.SamAccountName}},@{Name="Creation Date";Expression={$_.WhenCreated}}, @{Name="internal or external";Expression={$_.Extensionattribute6}}, @{Name="E-mail(if General account field can be Empty)";Expression={$_.Mail}}, @{Name="Notesfield";Expression={$_.Info}}, AccountExpirationDate, PasswordNeverExpires, CannotChangePassword, PasswordNotRequired| Sort-Object whencreated -Descending | 
    Export-Csv $fileLocation -Delimiter ";" 

    When i run this i get the follow message

    Could not compare "01/25/2018 10:45:18" to "31-1-2018". Error: "Cannot convert value "31-1-2018" to type "System.DateTime". Error: "String was not recognized as a valid DateTime.""
    At line:17 char:27
    + $OurUsers | Where-Object {($_.whencreated -ge $StartDate) -and ($_.Whencreated – ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : ComparisonFailure

    I know the problem comes from the line:

    $OurUsers | Where-Object {($_.whencreated -ge $StartDate) -and ($_.Whencreated -le $EndDate)} | 

    Any one know what must be filled in.

  • #93475

    Olaf Soyk

    With these 2 lines

    $SDate = get-date $StartDate 
    $EDate = get-date $EndDate 

    you're creating DateTime objects but you're not using them. ... should look like this:

    $OurUsers | Where-Object {($_.whencreated -ge $SDate) -and ($_.Whencreated -le $EDate)} | 
  • #93477


    i was just seconds to late to update this post,

    Yeah is saw the problem, oh what freaking stupid but thank you for the support

You must be logged in to reply to this topic.