windowspowershellwindows-forms-designerinventorytree-nodes

Generate tree nodes in powershell on the fly


I am currently new to and in process of learning powershell for andmistrative purposes. I am posting this question since I couldnt find much information regarding this anywhere.

I am creating a script with a gui which lists all our servers as treenodes. From there I want to generate/populate/create the child nodes of the server on the fly (i.e when the node'server1' is clicked or selected , it should generate child nodes as below). Since we have large quantity of servers, I dont want to update child nodes on every server when a new property is added. Can anyone please tell me how to accomplish this? If my description doesn't make sense I can explain more.

.....Server1 . . ......BIOSInfo (I am distiguishing server nodes and its child nodes by using tags) . . ......PROCInfo

etc

I am using sapien primal forms with powershell 3.0

here is the sample code;

function NodeClick( $object )
{
    if ($this.SelectedNode.Tag -eq "Server")
    {
        $Server = $this.selectednode.text
        $richTextBox1.Text = "Script for $Server Information"

        #~~< TreeNode11 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode1111 = New-Object System.Windows.Forms.TreeNode("OS")
        $TreeNode1111.Tag = "DevInfo"
        $TreeNode1111.Text = "OS"
        #~~< TreeNode12 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode1222 = New-Object System.Windows.Forms.TreeNode("Domain")
        $TreeNode1222.Tag = "DevInfo"
        $TreeNode1222.Text = "Domain"
        #~~< TreeNode13 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode1333 = New-Object System.Windows.Forms.TreeNode("Serial")
        $TreeNode1333.Tag = "DevInfo"
        $TreeNode1333.Text = "Serial"
        #~~< TreeNode14 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode1444 = New-Object System.Windows.Forms.TreeNode("BIOS")
        $TreeNode1444.Tag = "DevInfo"
        $TreeNode1444.Text = "BIOS"
        #~~< TreeNode15 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode1555 = New-Object System.Windows.Forms.TreeNode("Processor")
        $TreeNode1555.Tag = "DevInfo"
        $TreeNode1555.Text = "Processor"
        #~~< TreeNode16 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode1666 = New-Object System.Windows.Forms.TreeNode("Memory")
        $TreeNode1666.Tag = "DevInfo"
        $TreeNode1666.Text = "Memory"
        #~~< TreeNode17 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode1777 = New-Object System.Windows.Forms.TreeNode("Partitions")
        $TreeNode1777.Tag = "DevInfo"
        $TreeNode1777.Text = "Partitions"
        #~~< TreeNode18 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode1888 = New-Object System.Windows.Forms.TreeNode("Drive")
        $TreeNode1888.Tag = "DevInfo"
        $TreeNode1888.Text = "Drive"
        #~~< TreeNode19 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode1999 = New-Object System.Windows.Forms.TreeNode("IPInfo")
        $TreeNode1999.Tag = "DevInfo"
        $TreeNode1999.Text = "IPInfo"
        #~~< TreeNode110 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode2111 = New-Object System.Windows.Forms.TreeNode("PrintInfo")
        $TreeNode2111.Tag = "DevInfo"
        $TreeNode2111.Text = "PrintInfo"
        #~~< TreeNode111 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode2222 = New-Object System.Windows.Forms.TreeNode("FolderShare")
        $TreeNode2222.Tag = "DevInfo"
        $TreeNode2222.Text = "FolderShare"
        #~~< TreeNode112 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode2333 = New-Object System.Windows.Forms.TreeNode("Tasks")
        $TreeNode2333.Tag = "DevInfo"
        $TreeNode2333.Text = "Tasks"
        #~~< TreeNode113 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode2444 = New-Object System.Windows.Forms.TreeNode("Services")
        $TreeNode2444.Tag = "DevInfo"
        $TreeNode2444.Text = "Services"
        #~~< TreeNode114 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        $TreeNode2555 = New-Object System.Windows.Forms.TreeNode("Software")
        $TreeNode2555.Tag = "DevInfo"
        $TreeNode2555.Text = "Software"
        $this.SelectedNode = New-Object System.Windows.Forms.TreeNode("DOCACT01", [System.Windows.Forms.TreeNode[]] ( @($TreeNode1111, $TreeNode1222, $TreeNode1333, $TreeNode1444, $TreeNode1555, $TreeNode1666, $TreeNode1777, $TreeNode1888, $TreeNode1999, $TreeNode2111, $TreeNode2222, $TreeNode2333, $TreeNode2444, $TreeNode2555) ))
        $this.SelectedNode.Tag = "Server"
        $this.SelectedNode.Text = "$Server"

        $form1.refresh()
    }
    else
    {
        $this.SelectedNode.expand()
        $richTextBox1.Text = "Script for Server Information" 
    }

Solution

  • Got it working as following;

    function NodeClick( $object )
    {
    if (($this.SelectedNode.Tag -eq "Server") -and ($this.SelectedNode.nodes.count -eq 0))
    {           
        $Parent = $this.SelectedNode
        $richTextBox1.text = "Server Inventory Script"
        $Server = $Parent.text
        $Childs = ("OS", "Domain", "Serial", "BIOS", "Processor", "Memory", "Partitions", "Drive", "IPInfo", "PrintInfo", "FolderShare", "Tasks", "Services", "Software")
        $childs | %{
            $newNode = New-Object System.Windows.Forms.TreeNode             
            $newNode.Name = $_          
            $newNode.Text = $_              
            $newNode.Tag = "DevInfo"                
            $Parent.Nodes.Add($newNode) | Out-Null              
            return $newNode
            $Form1.refresh()
        }
    }