Proper filter statement

Welcome Forums General PowerShell Q&A Proper filter statement

Viewing 2 reply threads
  • Author
    Posts
    • #222981
      Participant
      Topics: 11
      Replies: 23
      Points: 162
      Rank: Participant

      I’m trying to get all users enabled, lastlogondate greater than 120 days and msexchlitigationholddate like null. Then I want all users enabled, modified greater than 120 days, lastlogondate like Null and msexchlitigationholddate like null

      get-aduser -filter {((enabled -eq $True) -and (lastlogonDate -le $DaysInactive) -and (msExchLitigationHoldDate -notlike "*")) -or ((enabled -eq $True) -and (lastlogonDate -notlike "*") -and (modified -le $DaysInactive) -and (msExchLitigationHoldDate -notlike "*"))} -Properties modified, lastlogondate, msExchLitigationHoldDate

      I can do this with two commands but thinking there is a way to build this into one.

    • #223239
      Participant
      Topics: 12
      Replies: 1607
      Points: 2,495
      Helping Hand
      Rank: Community Hero

      Would have to test that the separate and joined filters return the same users, but normally recommend formatting it something like this so logic can be validated:

          (enabled -eq $True) 
          -and 
          (msExchLitigationHoldDate -notlike "*") 
          -and 
          (
              (lastlogonDate -le $DaysInactive) 
                  -or 
              (
                  (lastlogonDate -notlike "*") 
                  -and  
                  (modified -le $DaysInactive)
              )
          )
      

      Then you can place it back into a single line for execution. The biggest question is if the filter applies the order of operations using parens so that the statement correctly group and identify true\false subqueries. It may be easier to just join the results than troubleshoot an advanced filter, but you can try the logic above to see if the query executes properly.

      • This reply was modified 3 weeks, 6 days ago by Rob Simmers.
    • #223374
      Participant
      Topics: 11
      Replies: 23
      Points: 162
      Rank: Participant

      Rob, thanks for the information I will give it a shot and see what I end up with.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.