在textBox WindowsForm中输入数组对象



我尝试输入数组对象$WinRoles(Get-WindowsFeature from remote hosts)。需要插入$WinRoles。DisplayName$WinRoles。$TextBoxInstalledRolesFeatures

My Code (这是出现此问题的代码部分下面)。我在哪里不见了?

<#------======== Label Installed Roles&Features ========------#>
$LabelInstalledRolesFeatures = New-Object System.Windows.Forms.Label
$LabelInstalledRolesFeatures.Location = '810,10'
$LabelInstalledRolesFeatures.AutoSize = $true
$LabelInstalledRolesFeatures.Text = 'Installed Roles and Features'
$CommonForm.Controls.Add($LabelInstalledRolesFeatures)
<#------======== Label Installed Roles&Features ========------#>
<#------======== TextBox Installed Roles&Features ========------#>
$TextBoxInstalledRolesFeatures = New-Object System.Windows.Forms.ListBox
$TextBoxInstalledRolesFeatures.Location = '810,30'
$TextBoxInstalledRolesFeatures.Size = '250,316'
$TextBoxInstalledRolesFeatures.Text = ''
$CommonForm.Controls.Add($TextBoxInstalledRolesFeatures)
<#------======== TextBox Installed Roles&Features ========------#>
<#------======== Button Show Roles&Features ========------#>
$ButtonShowRolesFeatures = New-Object System.Windows.Forms.Button
$ButtonShowRolesFeatures.Location = '985, 5'
$ButtonShowRolesFeatures.Text = 'Show'
$ButtonShowRolesFeatures.AutoSize = $true
$ButtonShowRolesFeatures.add_Click({

$SelectedServer = $ListboxListeServers.SelectedItem
$Session = New-PSSession -ComputerName $SelectedServer
$WinRoles = Invoke-Command -Session $Session -ScriptBlock {
Get-WindowsFeature | where Installed -eq $true
}
[array]$AllRoles = $WinRoles | select DisplayName, InstallState
$TextBoxInstalledRolesFeatures.Text = $AllRoles
})
$CommonForm.Controls.Add($ButtonShowRolesFeatures)
<#------======== Button Show Roles&Features ========------#>

您应该像Mathias在评论中建议的那样使用DataGridView而不是ListBox。实现不是很复杂,是您用例的理想控制。我的笔记本电脑中没有Get-WindowsFeature,但作为一个例子,这里是使用Get-Process作为网格数据源的实现:

Add-Type -AssemblyName System.Windows.Forms
$CommonForm = [System.Windows.Forms.Form]@{
Size          = '1800, 900'
StartPosition = 'CenterScreen'
}
$ListboxListeServers = [System.Windows.Forms.ListBox]@{
Location = '30, 40'
Size     = '200, 800'
}
$ListboxListeServers.Items.AddRange(@((Get-Process).ProcessName | Sort-Object -Unique))
$CommonForm.Controls.Add($ListboxListeServers)
$dgvInstalledRolesFeatures = [System.Windows.Forms.DataGridView]@{
Location = '240, 40'
Size     = '1400, 800'
}
$CommonForm.Controls.Add($dgvInstalledRolesFeatures)
$ButtonShowRolesFeatures = [System.Windows.Forms.Button]@{
Location = '985, 5'
Text     = 'Show'
AutoSize = $true
}
$processlist = [System.Collections.ArrayList]::new()
$ButtonShowRolesFeatures.add_Click({
$processlist.AddRange(@(Get-Process $ListboxListeServers.SelectedItem))
$dgvInstalledRolesFeatures.DataSource = $null
$dgvInstalledRolesFeatures.DataSource = $processlist
})
$CommonForm.Controls.Add($ButtonShowRolesFeatures)
$CommonForm.ShowDialog()

最新更新