Lots of examples on the interwebs. Here is one:

http://www.sapien.com/blog/2009/03/25/powershell-tree/

From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On 
Behalf Of Reichelt Philipp
Sent: Monday, September 22, 2014 8:44 AM
To: 'powershell@lists.myitforum.com'
Subject: [powershell] Read OUs from AD and fill TreeView

Hello,

I've a little question. I want to read OUs below a specific OU from AD and fill 
it into an TreeView object.

The AD structure:

something.de
  + myOU
      + computers
         + a
         + b
      + notebooks
      + servers


I want to get the OUs below "computers" - "a" and "b". These two OUs should be 
places in a TreeView like this:

myOU
   + a
   + b


I'm able to read the OUs i need. But I don't have any idea how I can 
dynamically add nodes tot he TreeView object.

$OUs = Get-ADOrganizationalUnit -SearchBase 
"OU=computers,OU=myOU,DC=something,DC=de" -Filter * | Select-Object Name | 
Sort-Object -Property Name
# $OUs contains "a", "b"


With this code I add nodes to the TreeView:

$objTreeNode_A = New-Object 'System.Windows.Forms.TreeNode' ("a")
$objTreeNode_A.Name = "a"
$objTreeNode_A.Text = "a"
$objTreeNode_B = New-Object 'System.Windows.Forms.TreeNode' ("b")
$objTreeNode_B.Name = "b"
$objTreeNode_B.Text = "b"
$objTreeNode_Root = New-Object 'System.Windows.Forms.TreeNode' ("myOU", 
[System.Windows.Forms.TreeNode[]] ( $objTreeNode_A, $objTreeNode_B ))
$objTreeNode_Root.Name = "myOU"
$objTreeNode_Root.Text = "myOU"
[void]$objTreeView.Nodes.Add($objTreeNode_Root)


Can anybody help me doing this better ("dynamically")?


================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1


================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

Reply via email to