Evaluating nonexistent propertie value

Welcome Forums General PowerShell Q&A Evaluating nonexistent propertie value

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

1 year, 11 months ago.

  • Author
  • #68943

    Points: 0
    Rank: Member

    Hi everyone

    Consider this
    $computer = get-adcomputer -Identity server1
    $computer | ? {$_.lastlogondate -lt (get-date).AddDays(-90)} | Do-SomethingCreative

    The problem here is that the propertie value for 'lastlogondate' is nonexistent (This has happened to me once or twice)
    In the "about_Where-Object" it says " The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it"

    But it will pass all objects becaus the value is missing (as I suspect most of you know).
    My question is. Is this a expexted behavior or a bug?

    To me, it's a little bit like

    If you have a parachute, I will throw you off the plane
    If you dont have a parachute, I will NOT throw you off the plane
    If I dont know if you hava a parachute, I will throw you off the plane!

    Is this logical 🙂


  • #69114

    Points: 41
    Rank: Member

    When PowerShell does the comparison the empty value is always going to evaluate as less than any given date. You need to change your logic so that you only perform a -lt operation on a real value.

    #Test array with dummy computer objects
    $computers = @(
            'Name' = 'Server1';
            'LastLoginDate' = $null     
            'Name' = 'Server2';
            'LastLoginDate' = (Get-Date).AddDays(-95)
    #Example 1 - both objects are output
    $computers | Where-Object {$_.LastLoginDate -lt (Get-Date).AddDays(-90)}
    #Example 2 - only the correct object is output
    $computers | Where-Object {$_.LastLoginDate -lt (Get-Date).AddDays(-90) -and $_.LastLoginDate -ne $null}
  • #69133

    Points: 62
    Rank: Member

    Also worth noting is the reason you're not getting the 'lastlogindate' property is because get-adcomputer returns only a small subset of properties by default. This is by design, and to correct this use the -properties parameter of get-adcomputer to specify what additional properties you'd like returned for each computer object. Eg.

    Get-ADComputer -Identity pc001 -Properties lastlogindate

    You can use * to return all properties but it's recommended to specify only the properties you're interested in.

The topic ‘Evaluating nonexistent propertie value’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort