locked
Joining Filters for Get-ADComputer RRS feed

  • Question

  • Hi  <o:p></o:p>

    I am trying to get a query to remove all computers from a group where extensionAttribute1 which is populated with a date is less than 1 day, <o:p></o:p>

    I have the query below, however
    it returning an error, (also below) I am sure it is because of the
    ',
    but despite a lot of effort an am unable to get it to work. And help would be
    appreciated. Hopefully just a minor correction and it will do what I want
    <o:p></o:p>


    Get-ADComputer -Filter {'samaccountname -like "Computername*"' "-and" 'MemberOf  -eq "SecurityGroup"'} -properties extensionAttribute1 |? { ((Get-Date-UFormat "%d / %m / %Y") - $_.extensionAttribute1).Days -ge 1} | remove-ADPrincipalGroupMembership -MemberOf "SecurityGroup"

    + Get-ADComputer -Filter {'samaccountname -like "Computername*"' "-and" 'MemberOf  ...

    +                                                                ~~~~~~

    Unexpected token '"-and"' in expression or statement.

    At line:1 char:71

    + Get-ADComputer -Filter {'samaccountname -like "Computername*"' "-and" 'MemberOf  ...

    +                                                                       ~~~~~~~~~~

    Unexpected token ''MemberOf  -eq "SecurityGroup"'' in expression or statement.

        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

        + FullyQualifiedErrorId : UnexpectedToken

     

    Thursday, June 7, 2018 1:11 PM

Answers

  • You quote the operators and property names, so they are not recognized. Try:

    -Filter {samaccountname -like "Computername*" -and MemberOf  -eq "SecurityGroup"'}

    Also, memberOf is DN syntax, so the value must be the full distinguished name of the group.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by MSP, Saturday, June 9, 2018 10:11 AM
    Thursday, June 7, 2018 1:24 PM

All replies

  • "extensionAttributes" are strings and not date objects.

    "Get-Date-UFormat "%d / %m / %Y")" is a string.  You cannot do date arithmetic with strings.


    \_(ツ)_/

    Thursday, June 7, 2018 1:20 PM
  • You quote the operators and property names, so they are not recognized. Try:

    -Filter {samaccountname -like "Computername*" -and MemberOf  -eq "SecurityGroup"'}

    Also, memberOf is DN syntax, so the value must be the full distinguished name of the group.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by MSP, Saturday, June 9, 2018 10:11 AM
    Thursday, June 7, 2018 1:24 PM
  • So if extensionAttributes are not in date formats how can I store a date for a computer object that  do date arithmetic against.,

    Thursday, June 7, 2018 2:42 PM
  • Hi

    Thanks for the help

    I managed to resolve the date issue using [DateTime]($_.extensionAttribute1), (although this uses US date format, which is annoying but can be managed as users do not see the data.

    and

    -Filter {samaccountname -like "Computername*" -and MemberOf  -eq "SecurityGroup"'}  worked, and allowed the query to resolve.

    Saturday, June 9, 2018 9:34 AM