Get date isnt working properly

Welcome Forums General PowerShell Q&A Get date isnt working properly

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
9 months ago.

  • Author
    Posts
  • #93472

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 144
    Helping Hand
    Rank: Participant

    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

    Participant
    Points: 0
    Rank: Member

    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

The topic ‘Get date isnt working properly’ is closed to new replies.