none
Working with -and chains in Powershell RRS feed

  • Question

  • Hi Scripting guys!

    I am working with the get-adcomputer cmdlet, trying to get a group of computer names while bypassing a couple of OUs that I don't need.  I read this awesome post that started to help me get around the ones I don't need, but I have several OUs that I need to ignore.  Here is my code so far:

    get-adcomputer -filter * -SearchBase "OU=OU Labs,OU=Computers,OU=OU,DC=campus,DC=school,DC=edu" | select name | Where-Object {$_.name -notlike "studio" -and "lab" -and "lab2"}

    I am trying to use the name (not distinguished) of the objects it's getting to filter them out, but it's only working for the first item, not the others.  Not sure if -and can work here, of if I can chain it, but if I can, then I need to use it.

    As always, any help is appreciated!


    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Thursday, January 26, 2017 6:53 PM

Answers

  • You can not do a -notlike with -and  because either one of the would result in $true.

    And ofcourse Like statements need wildcards like * .

    get-adcomputer -filter * -SearchBase "OU=OU Labs,OU=Computers,OU=OU,DC=campus,DC=school,DC=edu" |?{$_.name -notlike "*studio*"} | ?{ $_.name -notlike "*lab*" } | ?{ $_.name -notlike "*lab2*"}


    MCTS exchange 2013 | MCTS-MCITP exchange 2010 | MCTS-MCITP Exchange: 2007 | MCSA Messaging: 2003 | MCP windows 2000

    • Marked as answer by Obujuwami Thursday, January 26, 2017 7:51 PM
    Thursday, January 26, 2017 6:59 PM

All replies

  • You can not do a -notlike with -and  because either one of the would result in $true.

    And ofcourse Like statements need wildcards like * .

    get-adcomputer -filter * -SearchBase "OU=OU Labs,OU=Computers,OU=OU,DC=campus,DC=school,DC=edu" |?{$_.name -notlike "*studio*"} | ?{ $_.name -notlike "*lab*" } | ?{ $_.name -notlike "*lab2*"}


    MCTS exchange 2013 | MCTS-MCITP exchange 2010 | MCTS-MCITP Exchange: 2007 | MCSA Messaging: 2003 | MCP windows 2000

    • Marked as answer by Obujuwami Thursday, January 26, 2017 7:51 PM
    Thursday, January 26, 2017 6:59 PM
  • First  we need to straighten out you formatting and make this readable.  Next you need to use the correct proerpty and lastly selecting only name serves no purpose before the Where filter.

    get-adcomputer -filter * -SearchBase "OU=OU Labs,OU=Computers,OU=OU,DC=campus,DC=school,DC=edu" | 
    	Where-Object{
    		$_.DistinguishedName -notmatch 'studio|lab|lab2'
    	}
    
    Using AND when you mean OR also causes things to fail.


    \_(ツ)_/

    Thursday, January 26, 2017 7:08 PM
  • Thanks for the help Martijn!  I cleaned up my code to make it generic, but on my actual code, I have * wildcards in there.


    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Thursday, January 26, 2017 7:51 PM