none
Treview Active Directory RRS feed

  • Pergunta

  • Bom dia,

     

        Estou fazendo uma aplicação em Delphi 7, e estou precisando colocar um treeview do AD com todas as OU´s. Alguém saberia como posso fazer isso?

     

    Obrigado

     

     

    sábado, 14 de abril de 2007 13:31

Respostas

  • Bom Dia,

    Segue:

    PowerShell GUIs: Active Directory TreeView


    function Add-Node { 
            param ( 
                $selectedNode, 
                $name
            ) 
            $newNode = new-object System.Windows.Forms.TreeNode  
            $newNode.Name = $name 
            $newNode.Text = $name 
            $selectedNode.Nodes.Add($newNode) | Out-Null 
            return $newNode 
    } 
    
    function Get-NextLevel {
        param (
            $selectedNode,
            $dn
       )
       
        $OUs = Get-ADObject -Filter 'ObjectClass -eq "organizationalUnit" -or ObjectClass -eq "container"' -SearchScope OneLevel -SearchBase $dn
    
        If ($OUs -eq $null) {
            $node = Add-Node $selectedNode $dn
        } Else {
            $node = Add-Node $selectedNode $dn
            
            $OUs | % {
                Get-NextLevel $node $_.distinguishedName
            }
        }
    }
     
    function Build-TreeView { 
        if ($treeNodes)  
        {  
              $treeview1.Nodes.remove($treeNodes) 
            $form1.Refresh() 
        } 
        
        $treeNodes = New-Object System.Windows.Forms.TreeNode 
        $treeNodes.text = "Active Directory Hierarchy" 
        $treeNodes.Name = "Active Directory Hierarchy" 
        $treeNodes.Tag = "root" 
        $treeView1.Nodes.Add($treeNodes) | Out-Null 
         
        $treeView1.add_AfterSelect({ 
            $textbox1.Text = $this.SelectedNode.Name
        }) 
         
        #Generate Module nodes 
        $OUs = Get-NextLevel $treeNodes $strDomainDN
        
        $treeNodes.Expand() 
    } 
     
    #Generated Form Function 
    function GenerateForm { 
     
        #region Import the Assemblies 
        Import-Module ActiveDirectory
        [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null 
        [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null 
        $objIPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
        $strDNSDomain = $objIPProperties.DomainName.toLower()
        $strDomainDN = $strDNSDomain.toString().split('.'); foreach ($strVal in $strDomainDN) {$strTemp += "dc=$strVal,"}; $strDomainDN = $strTemp.TrimEnd(",").toLower()
        #endregion 
         
        #region Generated Form Objects 
        $form1 = New-Object System.Windows.Forms.Form 
        $treeView1 = New-Object System.Windows.Forms.TreeView 
        $label1 = New-Object System.Windows.Forms.Label
        $textbox1 = New-Object System.Windows.Forms.TextBox
        $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState 
        #endregion Generated Form Objects 
         
        #---------------------------------------------- 
        #Generated Event Script Blocks 
        #---------------------------------------------- 
        $button1_OnClick=  
        { 
        $form1.Close() 
         
        } 
         
        $OnLoadForm_StateCorrection= 
        {Build-TreeView 
        } 
         
        #---------------------------------------------- 
        #region Generated Form Code 
        $form1.Text = "Form" 
        $form1.Name = "form1" 
        $form1.DataBindings.DefaultDataSourceUpdateMode = 0 
        $form1.ClientSize = New-Object System.Drawing.Size(400,500) 
         
        $treeView1.Size = New-Object System.Drawing.Size(350,375)
        $treeView1.Name = "treeView1" 
        $treeView1.Location = New-Object System.Drawing.Size(15,15)
        $treeView1.DataBindings.DefaultDataSourceUpdateMode = 0 
        $treeView1.TabIndex = 0 
        $form1.Controls.Add($treeView1)
        
        $label1.Name = "label1" 
        $label1.Location = New-Object System.Drawing.Size(15,400)
        $label1.Size = New-Object System.Drawing.Size(350,20)
        $label1.Text = "Selected Value:"
        $form1.Controls.Add($label1) 
        
        $textbox1.Name = "textbox1" 
        $textbox1.Location = New-Object System.Drawing.Size(15,425)
        $textbox1.Size = New-Object System.Drawing.Size(350,20)
        $textbox1.Text = ""
        $form1.Controls.Add($textbox1) 
         
         
        #endregion Generated Form Code 
         
        #Save the initial state of the form 
        $InitialFormWindowState = $form1.WindowState 
        #Init the OnLoad event to correct the initial state of the form 
        $form1.add_Load($OnLoadForm_StateCorrection) 
        #Show the Form 
        $form1.ShowDialog()| Out-Null 
         
    } #End Function 
     
    #Call the Function 
    GenerateForm


    * Ajude a organizar melhor o Fórum, dando Feedback sobre a dúvida solicitada, e marcando como Útil e como Resposta, a postagem dos colaboradores que resolveram o seu caso, ou foram úteis *


    terça-feira, 5 de junho de 2012 14:08
    Moderador