动态生成powershell中的树节点

时间:2012-08-01 19:25:16

标签: windows powershell windows-forms-designer inventory tree-nodes

我目前刚接触并正在学习PowerShell用于行政目的。我发布这个问题,因为我无法在任何地方找到有关此问题的更多信息。

我正在创建一个带有gui的脚本,它将我们所有的服务器列为treenode。从那里我想在运行中生成/填充/创建服务器的子节点(即,当点击或选择节点'server1'时,它应该生成如下的子节点)。由于我们有大量的服务器,我不希望在添加新属性时更新每个服务器上的子节点。任何人都可以告诉我如何做到这一点?如果我的描述没有意义,我可以解释更多。

..... Server1的       。       。       ...... BIOSInfo(我通过使用标签来破坏服务器节点及其子节点)       。       。       ...... PROCInfo

我正在使用带有powershell 3.0的sapien原始形式

这是示例代码;

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" 
    }

1 个答案:

答案 0 :(得分:0)

让它按照以下方式工作;

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()
    }
}