导入 CSV 并取 属性 转换为列表

Import CSV and take the property to convert to list

我有一个要导入的 CSV 属性,然后在下拉框中使用该列表。下拉列表需要一个字符串,但我将列表转换为字符串所做的一切都失败了。这是我拥有的:

    $roles = (Import-CSV "\Networklocation\somefile.csv" | Select ProjectRoleProfile | Where { $_.ProjectRoleProfile -ne "" }).ProjectRoleProfile # Selecting my attribute and cleaning out the empty cells
foreach ($role in $roles)
{
    $role = [string]$role
    Write-Host $role
    Update-ComboBox $role #Function that updates my drop down box. Functions as long as I get strings. 
}

错误:

    ERROR: Update-ComboBox : Cannot process argument transformation on parameter 'ComboBox'. Cannot convert the "MYData" value of type "System.String" to type
ERROR: "System.Windows.Forms.ComboBox".
MyFile.ps1 (97, 18): ERROR: At Line: 97 char: 18
ERROR: +         Update-ComboBox $role
ERROR: +                         ~~~~~
ERROR:     + CategoryInfo          : InvalidData: (:) [Update-ComboBox], ParameterBindingArgumentTransformationException
ERROR:     + FullyQualifiedErrorId : ParameterArgumentTransformationError,Update-ComboBox
ERROR:

我猜我漏掉了一些小东西。任何帮助表示赞赏。

编辑:

更新组合框的代码

function Update-ComboBox
{
<#
    .SYNOPSIS
        This functions helps you load items into a ComboBox.

    .DESCRIPTION
        Use this function to dynamically load items into the ComboBox control.

    .PARAMETER ComboBox
        The ComboBox control you want to add items to.

    .PARAMETER Items
        The object or objects you wish to load into the ComboBox's Items collection.

    .PARAMETER DisplayMember
        Indicates the property to display for the items in this control.

    .PARAMETER Append
        Adds the item(s) to the ComboBox without clearing the Items collection.

    .EXAMPLE
        Update-ComboBox $combobox1 "Red", "White", "Blue"

    .EXAMPLE
        Update-ComboBox $combobox1 "Red" -Append
        Update-ComboBox $combobox1 "White" -Append
        Update-ComboBox $combobox1 "Blue" -Append

    .EXAMPLE
        Update-ComboBox $combobox1 (Get-Process) "ProcessName"

    .NOTES
        Additional information about the function.
#>

    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNull()]
        [System.Windows.Forms.ComboBox]
        $ComboBox,
        [Parameter(Mandatory = $true)]
        [ValidateNotNull()]
        $Items,
        [Parameter(Mandatory = $false)]
        [string]
        $DisplayMember,
        [switch]
        $Append
    )

    if (-not $Append)
    {
        $ComboBox.Items.Clear()
    }

    if ($Items -is [Object[]])
    {
        $ComboBox.Items.AddRange($Items)
    }
    elseif ($Items -is [System.Collections.IEnumerable])
    {
        $ComboBox.BeginUpdate()
        foreach ($obj in $Items)
        {
            $ComboBox.Items.Add($obj)
        }
        $ComboBox.EndUpdate()
    }
    else
    {
        $ComboBox.Items.Add($Items)
    }

    $ComboBox.DisplayMember = $DisplayMember

数据截图:我无法显示对某些敏感信息所做的一切,但我正在尝试获取 ProjectRoleProfile 列

错误消息说 "I can't turn a string into a combobox"

因此查看 Update-ComboBox 函数,它有两个强制参数,而您只给了它一个。

param (
    [Parameter(Mandatory = $true)]
    [ValidateNotNull()]
    [System.Windows.Forms.ComboBox]
 -> $ComboBox,
    [Parameter(Mandatory = $true)]
    [ValidateNotNull()]
 -> $Items,

由于 Combobox 参数是第一个,因此它尝试使用那里的字符串。所以 Update-ComboBox $role 需要变成 Update-ComboBox $yourcombobox $role