#OUADSPath2Clip.ps1 #This is a demo of new fast OU navigation #alan dot kaplan at va dot gov #4-28-2016, 5-4-16 #Requires -version 3 Function Select-ADOU { Param ( # Title help description [Parameter( Position=0)] $Title = "Select an OU", # Instruction Text help description [Parameter(Position=1)] [string]$Instructions = "Click on domain box after a domain change to show tree. Double click on a node to expand.", # Select Button Text [Parameter(Position=3)] [string]$BtnText = "Accept Selected OU" ) #Original form code generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0 $myDom = ([System.DirectoryServices.ActiveDirectory.domain]::GetCurrentDomain()).Name $Forest = ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()) $DomainList = ($Forest.Domains).name | sort -CaseSensitive $startNum= $DomainList.IndexOf($myDom) # Import the Assemblies Add-Type -assemblyname System.Windows.Forms Add-Type -assemblyname System.Drawing Add-Type -assemblyname Microsoft.VisualBasic # Form Objects $Form = New-Object System.Windows.Forms.Form $DomainBox = New-Object System.Windows.Forms.ListBox $DomainLbl = New-Object System.Windows.Forms.Label $AcceptBtn = New-Object System.Windows.Forms.Button $treeViewNav = New-Object System.Windows.Forms.TreeView $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState $StatusBar = New-Object System.Windows.Forms.StatusBar $InstructionsLabel = New-Object System.Windows.Forms.Label #Event Script Blocks $DomainBox_SelectedIndexChanged={ $CurrentDomain= $DomainBox.SelectedItem if ($Script:LoadCount -gt 0){ #initial domain is current $domain = [adsi]"LDAP://$CurrentDomain" }ELSE{ $domain=[adsi]'' } #Clear old results $TreeviewNav.Nodes.Clear() $newnode=New-Object System.Windows.Forms.TreeNode $newnode.Name=$domain.Name $newnode.Text=$domain.distinguishedName $newnode.Tag=$domain.distinguishedName $treeviewNav.Nodes.Add($newnode) $Script:LoadCount=$LoadCount+1 #Expand the initial tree Invoke-command $treeviewNav_DoubleClick } $treeviewNav_DoubleClick={ [System.Windows.Forms.Application]::UseWaitCursor = $true $StatusBar.Text = "Getting list, please wait..." if ($treeviewNav.SelectedNode -eq $null){ #For first listing in treeview, select root $node =$treeviewNav.Nodes[0] }Else{ $node=$treeviewNav.SelectedNode } if($node.Nodes.Count -eq 0){ $SearchRoot="LDAP://$($node.Tag)" $ADSearcher = [adsisearcher] '' $ADSearcher.SearchRoot = $SearchRoot $ADSearcher.PageSize = 500 $ADSearcher.SearchScope = "OneLevel" $ADSearcher.CacheResults = $false $ADSearcher.Filter = "(ObjectCategory=OrganizationalUnit)" $List = ($ADSearcher.FindAll()).getEnumerator().properties foreach ($OU in $List){ $OUName = $OU.Item("Name") $OUDN = $OU.Item("DistinguishedName") $newnode=New-Object System.Windows.Forms.TreeNode $newnode.Name=$OUName $newnode.Text=$OUName $newnode.Tag=$OUDN $node.Nodes.Add($newnode) } } $node.Expand() [System.Windows.Forms.Application]::UseWaitCursor = $False $statusbar.text = "" } $OnLoadForm_StateCorrection= {#Correct the initial state of the form to prevent the .Net maximized form issue $Form.WindowState = $InitialFormWindowState } $form.ClientSize = New-Object System.Drawing.Size(445,595) $Form.Name = "Form" $Form.Text = $Title $InstructionsLabel.Location = New-Object System.Drawing.Point(10,15) $InstructionsLabel.Name = "InstructionsLabel" $InstructionsLabel.Size = New-Object System.Drawing.Size(420,35) $InstructionsLabel.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,2,3,0) $InstructionsLabel.Text = $Instructions $Form.Controls.Add($InstructionsLabel) $DomainBox.Name = "DomainBox" $DomainBox.FormattingEnabled = $True $DomainBox.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",9.75,0,3,0) $DomainBox.Location = New-Object System.Drawing.Point(185,50) $DomainBox.Size = New-Object System.Drawing.Size(160,30) $DomainBox.add_SelectedIndexChanged($DomainBox_SelectedIndexChanged) foreach ($Domain in $domainlist){ [void] $DomainBox.Items.Add($domain) } $DomainBox.setSelected($startNum,$true) $Form.Controls.Add($DomainBox) $DomainLbl.Name = "DomainLbl" $DomainLbl.Location = New-Object System.Drawing.Point(20,50) $DomainLbl.Size = New-Object System.Drawing.Size(160,25) $DomainLbl.Text = "Selected Domain" $DomainLbl.TextAlign = "Middleright" $Form.Controls.Add($DomainLbl) $AcceptBtn.Name = "AcceptBtn" $AcceptBtn.Location = New-Object System.Drawing.Point(125,535) $AcceptBtn.Size =New-Object System.Drawing.Size(170,25) $AcceptBtn.Text = $BtnText $AcceptBtn.DialogResult = [System.Windows.Forms.DialogResult]::OK $Form.Controls.Add($AcceptBtn) $form.AcceptButton = $AcceptBtn $treeViewNav.Name = "treeViewNav" $treeViewNav.Location =New-Object System.Drawing.Point(45,80) $treeViewNav.Size =New-Object System.Drawing.Size(325,450) $treeViewNav.add_DoubleClick($treeViewNav_DoubleClick) $treeViewNav.add_AfterSelect($treeViewNav_AfterSelect) $Form.Controls.Add($treeViewNav) $form.StartPosition = "CenterParent" $form.Controls.Add($StatusBar) #Save the initial state of the form $InitialFormWindowState = $Form.WindowState #Init the OnLoad event to correct the initial state of the form $Form.add_Load($OnLoadForm_StateCorrection) #Show the Form $dialogResult = $Form.ShowDialog() if ($dialogResult -eq [System.Windows.Forms.DialogResult]::OK) { $RetVal = [PSCustomObject]@{ Domain = [string]$DomainBox.SelectedItem DN = [string]$treeViewNav.SelectedNode.Tag } $RetVal $Form.Close() } } #End Function #Call the Function $ADObject = Select-ADOU -BtnText "DN of Selected to Clipboard" $adsPath = $ADObject.DN if ($adsPath){ $adsPath | clip $message = "Done. $adsPath copied to the clipboard." #Use VB for MsgBox Add-Type -assemblyname Microsoft.visualBasic $retval = [Microsoft.VisualBasic.Interaction]::MsgBox($Message,'OkOnly,systemmodal,Information','DistinguishedName in Clipboard') }