Catcher Rem
Catcher Rem

Reputation: 51

Input array object in textBox WindowsForm

I try to input array object $WinRoles (Get-WindowsFeature from remote hosts). Need to insert $WinRoles.DisplayName and $WinRoles.InstallState as a table in textbox $TextBoxInstalledRolesFeatures

My Code (This is the part of code where this problem occurs) below. Where i missing?

    <#------======== 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 ========------#>

Upvotes: 1

Views: 106

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60728

You should use a DataGridView instead of a ListBox as Mathias recommended in comments. The implementation is not very complex and is the ideal control for your use case. I don't have Get-WindowsFeature in my laptop but as an example, here is an implementation using Get-Process as the grid's data source:

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

Upvotes: 0

Related Questions