如何通过Powershell获取远程Windows Server的文件夹树视图,而不是在本地GUI中输出它们?

我正在尝试使用PowerShell创建本地GUI,以递归方式显示远程Windows Server的所有驱动器(不包括“ C:\”)和所有子文件夹树。通过此PowerShell代码(已从另一个站点示例中窃取并改编),我已经成功地在Windows Server上实现了本地化目标。在本地运行它是不幸的,我不得不对它进行一些更改,就像我在此处发布远程版本一样:

$credentials = Get-Credential

$Computername = ""

Add-Type -AssemblyName System.Windows.Forms

function Get-DriveTree {
    Param($Computername)

    if ($driveNodes) {
        $treeview1.Nodes.Remove($driveNodes)
        $form1.Refresh()
    }
    $driveNodes = New-Object System.Windows.Forms.TreeNode
    $driveNodes.text = "Root"
    $driveNodes.Name = "Root"
    $driveNodes.Tag = "root"
    $treeView1.Nodes.Add($driveNodes) | Out-Null

    Invoke-Command -Credential $credentials -ComputerName $Computername -ArgumentList $driveNodes -OutVariable driveNodes -ScriptBlock {
        Param($driveNodes);

        Add-Type -AssemblyName System.Windows.Forms

        function Add-Node {
            Param(
                $selectedNode,$name,$tag
            )

            $newNode = new-object System.Windows.Forms.TreeNode
            #$newNode.Parent = $selectedNode
            $newNode.Name = $name
            $newNode.Text = $name
            $newNode.Tag = $tag
            $selectedNode.Nodes.Add($newNode) | Out-Null
            return $newNode
        }

        $drives = Get-PSDrive -PSProvider 'FileSystem'

        foreach ($M in $drives) {
            if ($M.Name -eq "C") {continue}
            $Parentnodes = Get-ChildItem $M.Root -Directory -Erroraction SilentlyContinue
            $Root = Add-Node $driveNodes $M.Root "Parent"

            foreach ($P in $Parentnodes) {
                $parentNode = Add-Node $Root $P.Name "Folder"
                $childnodes = Get-ChildItem $P -Directory -Recurse #| ? {$_.PSIscontainer -and $_.FullName -notmatch "c:\"}
                $childnodes | % { Add-Node $parentNode $_.Name "SubFolder" }
            }
        }

        return $driveNodes
    }

    $driveNodes.Expand()
}
#$childNode = Add-Node $parentNode $P.Name "Folder"

#Generated Form Function
function GenerateForm {
    #region Import the Assemblies
    add-type -assemblyname "System.Windows.Forms" | Out-Null
    add-type -assemblyname "System.Drawing" | Out-Null
    #endregion

    #region Generated Form Objects
    $form1 = New-Object System.Windows.Forms.Form
    $linkLabel1 = New-Object System.Windows.Forms.LinkLabel
    $label4 = New-Object System.Windows.Forms.Label
    #$label3 = New-Object System.Windows.Forms.Label
    $label2 = New-Object System.Windows.Forms.Label
    $button1 = New-Object System.Windows.Forms.Button
    #$richTextBox1 = New-Object System.Windows.Forms.RichTextBox
    $treeView1 = New-Object System.Windows.Forms.TreeView
    $InitialFormwindowstate = New-Object System.Windows.Forms.Formwindowstate

    $button1_OnClick = {
        $form1.Close()
    }

    $OnLoadForm_StateCorrection = {
        Get-DriveTree $Computername
    }

    $form1.Text = "Primal Form"
    $form1.Name = "form1"
    $form1.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 438
    $System_Drawing_Size.Height = 612
    $form1.ClientSize = $System_Drawing_Size

    $linkLabel1.Font = New-Object System.Drawing.Font("microsoft Sans Serif",9,3,0)
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 539
    $System_Drawing_Size.Height = 23
    $linkLabel1.Size = $System_Drawing_Size
    $linkLabel1.TabIndex = 10
    $linkLabel1.Text = "Super Manlio Perforante"
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 253
    $System_Drawing_Point.Y = 541
    $linkLabel1.Location = $System_Drawing_Point
    $linkLabel1.TabStop = $True
    $linkLabel1.DataBindings.DefaultDataSourceUpdateMode = 0
    $linkLabel1.Name = "linkLabel1"
    $linkLabel1.($linkLabel1_OpenLink)

    $form1.Controls.Add($linkLabel1)

    $label4.TabIndex = 9
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 136
    $System_Drawing_Size.Height = 23
    $label4.Size = $System_Drawing_Size
    $label4.Text = "Manlio Consiglia:"
    $label4.Font = New-Object System.Drawing.Font("microsoft Sans Serif",1,0)

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 253
    $System_Drawing_Point.Y = 518
    $label4.Location = $System_Drawing_Point
    $label4.DataBindings.DefaultDataSourceUpdateMode = 0
    $label4.Name = "label4"

    $form1.Controls.Add($label4)

    $label2.TabIndex = 5
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 177
    $System_Drawing_Size.Height = 23
    $label2.Size = $System_Drawing_Size
    $label2.Text = "PowerShell Help Tree"
    $label2.Font = New-Object System.Drawing.Font("microsoft Sans Serif",0)

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 13
    $label2.Location = $System_Drawing_Point
    $label2.DataBindings.DefaultDataSourceUpdateMode = 0
    $label2.Name = "label2"

    $form1.Controls.Add($label2)

    $button1.TabIndex = 4
    $button1.Name = "button1"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 75
    $System_Drawing_Size.Height = 23
    $button1.Size = $System_Drawing_Size
    $button1.UseVisualStyleBackColor = $True

    $button1.Text = "Close"

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 253
    $System_Drawing_Point.Y = 577
    $button1.Location = $System_Drawing_Point
    $button1.DataBindings.DefaultDataSourceUpdateMode = 0
    $button1.($button1_OnClick)

    $form1.Controls.Add($button1)

    #$form1.Controls.Add($richTextBox1)

    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 224
    $System_Drawing_Size.Height = 563
    $treeView1.Size = $System_Drawing_Size
    $treeView1.Name = "treeView1"
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 13
    $System_Drawing_Point.Y = 37
    $treeView1.Location = $System_Drawing_Point
    $treeView1.DataBindings.DefaultDataSourceUpdateMode = 0
    $treeView1.TabIndex = 0

    $form1.Controls.Add($treeView1)

    #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

我不能做的就是正确输出我的$driveNodes函数的返回Get-DriveTree。它从Invoke-Command的输出为System.Object (deserialized),而不是所有文件夹树的System.MarshalObjReference输出,所以我无法在GenerateForm函数内部对其进行扩展。

我尝试将Invoke-Command输出转换为序列化的对象,但实际上并不能做到(我不知道该怎么做)。

zhongxin360 回答:如何通过Powershell获取远程Windows Server的文件夹树视图,而不是在本地GUI中输出它们?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3097054.html

大家都在问