Advertisement
Guest User

AD

a guest
Oct 18th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Browse-AD()
  2. {
  3.     # original inspiration: https://itmicah.wordpress.com/2013/10/29/active-directory-ou-picker-in-powershell/
  4.     # author: Rene Horn the.rhorn@gmail.com
  5. <#
  6.     Copyright (c) 2015, Rene Horn
  7.     All rights reserved.
  8.     Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  9.     1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  10.     2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  11.     3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  12.     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13. #>
  14.     $dc_hash = @{}
  15.     $selected_ou = $null
  16.  
  17.     Import-Module ActiveDirectory
  18.     $forest = Get-ADForest
  19.     [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
  20.     [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
  21.  
  22.     function Get-NodeInfo($sender, $dn_textbox)
  23.     {
  24.         $selected_node = $sender.Node
  25.         $dn_textbox.Text = $selected_node.Name
  26.     }
  27.  
  28.     function Add-ChildNodes($sender)
  29.     {
  30.         $expanded_node = $sender.Node
  31.  
  32.         if ($expanded_node.Name -eq "root") {
  33.             return
  34.         }
  35.  
  36.         $expanded_node.Nodes.Clear() | Out-Null
  37.  
  38.         $dc_hostname = $dc_hash[$($expanded_node.Name -replace '(OU=[^,]+,)*((DC=\w+,?)+)','$2')]
  39.         $child_OUs = Get-ADObject -Server $dc_hostname -Filter 'ObjectClass -eq "organizationalUnit" -or ObjectClass -eq "container"' -SearchScope OneLevel -SearchBase $expanded_node.Name
  40.         if($child_OUs -eq $null) {
  41.             $sender.Cancel = $true
  42.         } else {
  43.             foreach($ou in $child_OUs) {
  44.                 $ou_node = New-Object Windows.Forms.TreeNode
  45.                 $ou_node.Text = $ou.Name
  46.                 $ou_node.Name = $ou.DistinguishedName
  47.                 $ou_node.Nodes.Add('') | Out-Null
  48.                 $expanded_node.Nodes.Add($ou_node) | Out-Null
  49.             }
  50.         }
  51.     }
  52.  
  53.     function Add-ForestNodes($forest, [ref]$dc_hash)
  54.     {
  55.         $ad_root_node = New-Object Windows.Forms.TreeNode
  56.         $ad_root_node.Text = $forest.RootDomain
  57.         $ad_root_node.Name = "root"
  58.         $ad_root_node.Expand()
  59.  
  60.         $i = 1
  61.         foreach ($ad_domain in $forest.Domains) {
  62.             Write-Progress -Activity "Querying AD forest for domains and hostnames..." -Status $ad_domain -PercentComplete ($i++ / $forest.Domains.Count * 100)
  63.             $dc = Get-ADDomainController -Server $ad_domain
  64.             $dn = $dc.DefaultPartition
  65.             $dc_hash.Value.Add($dn, $dc.Hostname)
  66.             $dc_node = New-Object Windows.Forms.TreeNode
  67.             $dc_node.Name = $dn
  68.             $dc_node.Text = $dc.Domain
  69.             $dc_node.Nodes.Add("") | Out-Null
  70.             $ad_root_node.Nodes.Add($dc_node) | Out-Null
  71.         }
  72.  
  73.         return $ad_root_node
  74.     }
  75.    
  76.     $main_dlg_box = New-Object System.Windows.Forms.Form
  77.     $main_dlg_box.ClientSize = New-Object System.Drawing.Size(400,600)
  78.     $main_dlg_box.MaximizeBox = $false
  79.     $main_dlg_box.MinimizeBox = $false
  80.     $main_dlg_box.FormBorderStyle = 'FixedSingle'
  81.  
  82.     # widget size and location variables
  83.     $ctrl_width_col = $main_dlg_box.ClientSize.Width/20
  84.     $ctrl_height_row = $main_dlg_box.ClientSize.Height/15
  85.     $max_ctrl_width = $main_dlg_box.ClientSize.Width - $ctrl_width_col*2
  86.     $max_ctrl_height = $main_dlg_box.ClientSize.Height - $ctrl_height_row
  87.     $right_edge_x = $max_ctrl_width
  88.     $left_edge_x = $ctrl_width_col
  89.     $bottom_edge_y = $max_ctrl_height
  90.     $top_edge_y = $ctrl_height_row
  91.  
  92.     # setup text box showing the distinguished name of the currently selected node
  93.     $dn_text_box = New-Object System.Windows.Forms.TextBox
  94.     # can not set the height for a single line text box, that's controlled by the font being used
  95.     $dn_text_box.Width = (14 * $ctrl_width_col)
  96.     $dn_text_box.Location = New-Object System.Drawing.Point($left_edge_x, ($bottom_edge_y - $dn_text_box.Height))
  97.     $main_dlg_box.Controls.Add($dn_text_box)
  98.     # /text box for dN
  99.  
  100.     # setup Ok button
  101.     $ok_button = New-Object System.Windows.Forms.Button
  102.     $ok_button.Size = New-Object System.Drawing.Size(($ctrl_width_col * 2), $dn_text_box.Height)
  103.     $ok_button.Location = New-Object System.Drawing.Point(($right_edge_x - $ok_button.Width), ($bottom_edge_y - $ok_button.Height))
  104.     $ok_button.Text = "Ok"
  105.     $ok_button.DialogResult = 'OK'
  106.     $main_dlg_box.Controls.Add($ok_button)
  107.     # /Ok button
  108.  
  109.     # setup tree selector showing the domains
  110.     $ad_tree_view = New-Object System.Windows.Forms.TreeView
  111.     $ad_tree_view.Size = New-Object System.Drawing.Size($max_ctrl_width, ($max_ctrl_height - $dn_text_box.Height - $ctrl_height_row*1.5))
  112.     $ad_tree_view.Location = New-Object System.Drawing.Point($left_edge_x, $top_edge_y)
  113.     $ad_tree_view.Nodes.Add($(Add-ForestNodes $forest ([ref]$dc_hash))) | Out-Null
  114.     $ad_tree_view.Add_BeforeExpand({Add-ChildNodes $_})
  115.     $ad_tree_view.Add_AfterSelect({Get-NodeInfo $_ $dn_text_box})
  116.     $main_dlg_box.Controls.Add($ad_tree_view)
  117.     # /tree selector
  118.  
  119.     $main_dlg_box.ShowDialog() | Out-Null
  120.  
  121.     return  $dn_text_box.Text
  122. }
  123.  
  124. Browse-AD
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement