Listing specific OUs for context menu

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 3 days old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
StillLearning
Posts: 39
Last visit: Tue Apr 10, 2018 9:39 pm

Listing specific OUs for context menu

Post by StillLearning »

Hello, as you all know sometimes PCs end up in the wrong OU
We have a few to choose from and this is how my domain is setup

Company.net
Computers
Managed Computers
East Coast
West Coast
Test
Pilot

I'd like to populate just East Coast and West Coast in my context menu
Using ADSI is much quicker than cmdlets
So essentialy be able to move a computer to the correct OU with a few clicks.

I've searched the web and cannot seem to find how to find and filter this specific
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listing specific OUs for context menu

Post by jvierra »

What is it that you do not know how to do?

What is it you are trying to filter? You need to refine your requirements to something much more detailed before you can turn it into code.
User avatar
StillLearning
Posts: 39
Last visit: Tue Apr 10, 2018 9:39 pm

Re: Listing specific OUs for context menu

Post by StillLearning »

Hi, sorry about that.
I'm trying to list specific OU's. Lets start with that.
Capture.PNG
Capture.PNG (5.29 KiB) Viewed 3296 times
As you can see there are several OUs
The results I do not want to return are:
NYC, Miami, Las Vegas, Seattle, Pilot and Test
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listing specific OUs for context menu

Post by jvierra »

Then you need to filter them out of the results. You can use a "Where-Object"

... Where-Object{ $computer.DistinguishedName -notmatch 'NYC|Miami|Las Vegas|Seattle|Pilot|Test' }
User avatar
StillLearning
Posts: 39
Last visit: Tue Apr 10, 2018 9:39 pm

Re: Listing specific OUs for context menu

Post by StillLearning »

Yes

Now how do I find those parents using ADSI?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listing specific OUs for context menu

Post by jvierra »

What parents? The code just matches the computers distinguished name.
User avatar
StillLearning
Posts: 39
Last visit: Tue Apr 10, 2018 9:39 pm

Re: Listing specific OUs for context menu

Post by StillLearning »

I need the ADSI part to list the OUs

That’s where I’m stuck now, I’ve searched the web but I’m unable to find an example I can work from

I think once I get that figured out, the rest should come together for me.

Your .... in the previous post is the part I’m missing.

I apologize for the inconvenience.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listing specific OUs for context menu

Post by jvierra »

I thought you wanted the computers? If you know the OUs then just add them by name to the listbox.

Get all computers and filter them.

Your intent is too vague without a clear statement of use case and workflow.

To get all computers just do this:

([adsisearcher]'objectclass=computer').FindAll()

Now just filter that.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listing specific OUs for context menu

Post by jvierra »

Here is a small demo of how to use ADSI:

Code: Select all

([adsisearcher]'objectclass=computer').FindAll() | 
     Where{$_.Path -notmatch 'Domain Controllers'} |
     ForEach{[adsi]$_.Path} |
     select name,parent
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listing specific OUs for context menu

Post by jvierra »

Filter OUs by name:

Code: Select all

([adsisearcher]'objectclass=organizationalunit').FindAll() | 
      Where{$_.Path -notmatch 'Domain Controllers|Terminated|TestOU.*'}
This topic is 6 years and 3 days old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked