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 4 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

Re: Listing specific OUs for context menu

Post by StillLearning »

I have the computer name because that’s what I’m using to start this project

Asking the customer what the PC# is then I pull several items from that PC

I’d just rather script an OU move then have the techs go through ADUC.

Remember my goal is to list the OUs in a context menu so if they are wrong, the techs can right click a label(OU they are presently in) and the list of OUs I want to listed are present so on the click event, they are moved to the correct OU
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 »

Like I noted above. You need a clear use case and workflow. What you are posting is just bits with little definition. I cannot understand what you are trying to do. First you ask for computers from some OUs then you ask for OUs and finally add something about getting things from a PC.

Start small and write the first pieces of your code. Ask each new question as you arrive at it. This will help you avoid ambiguous questions.
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 an old example of how to use ADSI from a form or a script:
Attachments
Demo-ADSITreeView.psf
(25.77 KiB) Downloaded 117 times
User avatar
StillLearning
Posts: 39
Last visit: Tue Apr 10, 2018 9:39 pm

Re: Listing specific OUs for context menu

Post by StillLearning »

My use case:
Help techs identify computers that are in the wrong OU. They are aware that if a PC is not in East Coast or West Coast it’s wrong. Why not just move them automatically? It’s less of a hassle on the end user being met with new changes when I can have my techs explain to them while my script is running.

My workflow is this:
Techs type in PC name and a label displays what OU it’s currently in, if it’s wrong I want the techs to right click on that label and choose the correct OU,upon doing so, the PC is moved to the correct OU, GPUpdate is ran and the PC is restarted.
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 »

Great. Now where is you code that collects the PC names and looks up the OU?

Code: Select all

$computersam = $name + '$'
$parentOU = ([adsisearcher]"SamAccountName=$computersam").FindOne().GetDirectoryEntry().Parent
# test for correct OU
User avatar
StillLearning
Posts: 39
Last visit: Tue Apr 10, 2018 9:39 pm

Re: Listing specific OUs for context menu

Post by StillLearning »

To give you a rough idea
TestMainForm.psf
(76.46 KiB) Downloaded 192 times
User avatar
StillLearning
Posts: 39
Last visit: Tue Apr 10, 2018 9:39 pm

Re: Listing specific OUs for context menu

Post by StillLearning »

Since there has been 3 downloads, I don't want to confuse anyone.. Everything works beside dynamically populating the OUs I want in the context menu
I just thought I would share a run down
  1. $contextmenustrip1_Opening=[System.ComponentModel.CancelEventHandler]{
  2. #Event Argument: $_ = [System.ComponentModel.CancelEventArgs]
  3.     #TODO: Place custom script here
  4.     #This is where I think the ADSI code would go to find the OUs since it's on the opening.
  5. }
  6.  
  7. $eastCoastToolStripMenuItem_Click={
  8.     #TODO: Place custom script here
  9.     $wshell = New-Object -ComObject Wscript.Shell
  10.     $wshell.Popup("Moving To East Coast OU", 0, "Done", 0x1)
  11.     #script to move the PC to the OU
  12.     $wshell = New-Object -ComObject Wscript.Shell
  13.     $wshell.Popup("Updating GP on PC", 0, "Done", 0x1)
  14.     #invoke-command to run GPUPdate /force /boot
  15.     $wshell = New-Object -ComObject Wscript.Shell
  16.     $wshell.Popup("Restarting PC", 0, "Done", 0x1)
  17. }
  18.  
  19. $textbox1_TextChanged={
  20.     #TODO: Place custom script here
  21.     #if the PC matches a naming convention we'll do $computer = $textbox1.text
  22.     $labelWindows10.Text = (gwmi -ComputerName $computer -Class Win32_OperatingSystem).caption
  23.     $labelMiami.Text = #I have the script on my other PC but it pulls the parent of the current PC so it will only show Miami or Las Vegas, etc...
  24. }
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 »

That is just a dummy that assume nothing. How do you envision this working? What is your first question.

Look at the form I posted to se how to navigate and use ADSI.
User avatar
StillLearning
Posts: 39
Last visit: Tue Apr 10, 2018 9:39 pm

Re: Listing specific OUs for context menu

Post by StillLearning »

That's beautiful.
-----
This is how I know what OU the PC is in
$pc = 'PC123456'
(([adsisearcher]"samaccountname=$PC$").FindOne().Properties['adspath']).split(",")[1] -replace ('OU=')

It will return this.

East Coast

All PCs reside in these 5 OUs.
LDAP://OU=EastCoast,OU=Org,OU=Workstations,DC=orgname,DC=net
LDAP://OU=Test,OU=Org,=Workstations,DC=orgname,DC=net
LDAP://OU=WestCoast,OU=Org,OU=Workstations,DC=orgname,DC=net
LDAP://OU=Miami,OU=Org,OU=Workstations,DC=orgname,DC=net
LDAP://OU=LasVegas,OU=Org,OU=Workstations,DC=orgname,DC=net

I want to be able to filter out Miami, Las Vegas, Test and any others ( through a external text file)
Load my contextmenu with the remaining results ( East Coast, West Coast)
Like so
DomainLabelPicker.psf
(59.19 KiB) Downloaded 155 times
I don't know how to populate the contextmenu items dynamically with the correct results.
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 showed you all of that already.
This topic is 6 years and 4 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