简体   繁体   中英

How to populate Combobox with result of Powershell command

Can someone assist on how to fill a combobox with the result of a powershell command?

Im trying to fill a combobox with the result of a "Get" cmdlet but I only get some powershell parameters as result.

$ButtonCollectionSearch.Add_Click({
    $name = $textboxlogonname.text
    $ComboBox = New-Object System.Windows.Forms.ComboBox
    $ComboBox.Width = 400
    $Collections = Get-RDSessionCollection | fl  -Property CollectionName

    Foreach ($Collection in $Collection) {
        $ComboBox.Items.Add($Collection);
    }
    $ComboBox.Location = New-Object System.Drawing.Point(120, 10)
    $main_form.Controls.Add($ComboBox)
})

在此处输入图片说明

The reason you're getting formatting metadata is that you asked for formatting metadata - by piping all your data through fl (which is a alias for Format-List ).

Since we just want the value of the CollectionName , use ForEach-Object -MemberName in place of fl -Property :

$Collections = Get-RDSessionCollection | ForEach-Object -MemberName CollectionName

You'll also want to address the typo in the foreach loop declaration - change:

Foreach ($Collection in $Collection) {

to:

Foreach ($Collection in $Collections) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM