Where clause not working with Get-ADUSer

Welcome Forums General PowerShell Q&A Where clause not working with Get-ADUSer

Viewing 9 reply threads
  • Author
    Posts
    • #162522
      Participant
      Topics: 13
      Replies: 42
      Points: 243
      Rank: Participant

      I have two problems:

      1.  When I use this command and view the variables in Visual Studio Code, I only see 8 variables as opposed to all of them, even though I specified “*” for properties. Interestingly, under the “property count” property of each object, it states 103. I don’t know why I can’t see them though. Why is this?
        $test=Get-ADUser-filter *-properties *-SearchBase "OU=Service Accounts,DC=blah,DC=blah,DC=blah"
      2. When trying to filter the items with the Where clause, based on the PasswordNeverExpires propery, it does indeed filter the objects, but it isn’t giving me what I want. When I go through the results manually and do a Get-ADUser lookup in Powershell ISE on the filtered objects, it states that the PasswordNeverExpires is set to True, even though I specified that I only wanted objects where that property was false.
        $test = $test | Where-Object {$_.PasswordNeverExpires -eq "False"}

        I feel like the first problem is related to the second one so I thought I’d include it. This must be something simple that I’m missing because I’ve been debugging this for an hour. Can someone please explain to me why I can’t see the properties of the object that I specified and why the Where clause is seemingly doing the opposite of what I want?

    • #162557
      Participant
      Topics: 5
      Replies: 2376
      Points: 6,030
      Helping Hand
      Rank: Community MVP

      Even when you specify -Properties * the cmdlet will show you the default output. If you like to see all attributes you should use eiteher a Format-List * or a Select-Object -Property *.
      If you like to check for true or false you will have to use the Powershell default variables $true and $false.

      BTW: to format code in this forum you should use the code tag button of post editor (pre). Thanks

    • #162605
      Participant
      Topics: 13
      Replies: 42
      Points: 243
      Rank: Participant

      Even when you specify -Properties * the cmdlet will show you the default output. If you like to see all attributes you should use eiteher a Format-List * or a Select-Object -Property *.

      If you like to check for true or false you will have to use the Powershell default variables $true and $false.

      BTW: to format code in this forum you should use the code tag button of post editor (pre). Thanks

      Thank you for the response. Piping the output to Select-Object -Property * worked. If it did indeed have the values for those properties stored in $test, even if they weren’t clearly visible, they must have been somewhere that I can find within the Variables window pane in Visual Studio Code. If not, then how would piping to Select-Object work if it didn’t have the properties to begin with? Could you explain more in-depth why simply specifying the properties within the Get-ADUSer command was not enough?

      I have fixed the code tag above, sorry about that.

    • #162617
      js
      Participant
      Topics: 31
      Replies: 828
      Points: 2,567
      Helping Hand
      Rank: Community Hero

      That’s strange. Just running $test should show all the properties. I’m not sure what you’re doing in Vscode.

    • #162645
      Participant
      Topics: 13
      Replies: 42
      Points: 243
      Rank: Participant

      That’s strange. Just running $test should show all the properties. I’m not sure what you’re doing in Vscode.

      The code I have above is the entire script, just those two lines. The other commentor indicated that it may just be the way Powershell is. What happens when you run those two lines that I have?

    • #162650
      js
      Participant
      Topics: 31
      Replies: 828
      Points: 2,567
      Helping Hand
      Rank: Community Hero

      If I do something like this, I see all the properties. I don’t know how to get a variables window in vscode like you do.

      $test = Get-ADUser-filter * -properties * -SearchBase "OU=Service Accounts,DC=blah,DC=blah,DC=blah"
      $test
      
    • #162659
      Participant
      Topics: 13
      Replies: 42
      Points: 243
      Rank: Participant

      If I do something like this, I see all the properties. I don’t know how to get a variables window in vscode like you do.

      PowerShell
      3 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      $test = Get-ADUser-filter * properties * SearchBase “OU=Service Accounts,DC=blah,DC=blah,DC=blah”
      $test
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      You have to set a breakpoint and temporarily halt code execution. Set the breakpoint at the first statement, then step over and look at the $test variable in the Variables window

      • #162666
        js
        Participant
        Topics: 31
        Replies: 828
        Points: 2,567
        Helping Hand
        Rank: Community Hero

        You have to set a breakpoint and temporarily halt code execution. Set the breakpoint at the first statement, then step over and look at the $test variable in the Variables window

        I don’t get it. Things like Step Over, Step Into, Step Out, Continue, Stop Debugging, and Restart Debugging under Debug are greyed out, and I set a breakpoint and picked Start Debugging.

        EDIT: Ok, I did an update and debug seems to work. I don’t know why vscode doesn’t show all the properties in the debug variables window. This seems to be a known bug: https://github.com/PowerShell/vscode-powershell/issues/821 This is a problem with VSCODE.

        It’s true that in many other cases, the default view doesn’t show all the properties of an object at the command line. But not in this case.

    • #162662
      Participant
      Topics: 3
      Replies: 17
      Points: -8
      Rank: Member

      Even when you specify -Properties * the cmdlet will show you the default output. If you like to see all attributes you should use eiteher a Format-List * or a Select-Object -Property *.

      Not true in my env. Using `-Prop *` on an AD cmdlet will show all the properties without `ft` or `select`

    • #162684
      Participant
      Topics: 24
      Replies: 175
      Points: 588
      Helping Hand
      Rank: Major Contributor

      also be aware, if you use format-list, you immediately loose the object.
      it will just become text strings and no longer available in the pipeline.

      I agree with Ray, in vs code, console, or ise, a -properties * returns every property on the ad-user.

      now to roll back to the original problem.

      $test=Get-ADUser -properties PasswordNeverExpires -filter {PasswordNeverExpires -eq “True”} -SearchBase “OU=Service Accounts,DC=blah,DC=blah,DC=blah”
      

      why this approach? we always want to filter as far left as possible, your initial approach you literally return every user object in that ou, as well as every property possible. You should only retrieve the exact properties you require, and you should filter as early in the pipeline as possible.

    • #162774
      Participant
      Topics: 13
      Replies: 42
      Points: 243
      Rank: Participant

      You have to set a breakpoint and temporarily halt code execution. Set the breakpoint at the first statement, then step over and look at the $test variable in the Variables window

      I don’t get it. Things like Step Over, Step Into, Step Out, Continue, Stop Debugging, and Restart Debugging under Debug are greyed out, and I set a breakpoint and picked Start Debugging.

      EDIT: Ok, I did an update and debug seems to work. I don’t know why vscode doesn’t show all the properties in the debug variables window. This seems to be a known bug: https://github.com/PowerShell/vscode-powershell/issues/821 This is a problem with VSCODE.

      It’s true that in many other cases, the default view doesn’t show all the properties of an object at the command line. But not in this case.

      That’s so weird. I didn’t think it was even retrieving the variables successfully since the Where clause wasn’t working, but as the above poster stated, I need to use $true or $false, I can’t specify it as a string.

      I know it’s best to specify only the needed properties. I only specified “*” for testing purposes.

Viewing 9 reply threads
  • The topic ‘Where clause not working with Get-ADUSer’ is closed to new replies.