Winforms 使用映射驱动器填充combox

Winforms 使用映射驱动器填充combox,winforms,powershell,Winforms,Powershell,尝试用映射的驱动器号(名称)和FQDN(根)填充组合框。我想我有大部分代码,但是组合框条目包括编码条目 我不仅好奇如何解决这个问题,而且还好奇为什么结果是以这种方式输入的。通过命令行运行此命令不会以这种方式显示结果 注意:我还使用了一个函数来填充组合框 检索映射驱动器的代码 Load-ComboBox -ComboBox $cboDomain -Items (Get-PSDrive -PSProvider FileSystem | Select-Object name, @{ n = "Root

尝试用映射的驱动器号(名称)和FQDN(根)填充组合框。我想我有大部分代码,但是组合框条目包括编码条目

我不仅好奇如何解决这个问题,而且还好奇为什么结果是以这种方式输入的。通过命令行运行此命令不会以这种方式显示结果

注意:我还使用了一个函数来填充组合框

检索映射驱动器的代码

Load-ComboBox -ComboBox $cboDomain -Items (Get-PSDrive -PSProvider FileSystem | Select-Object name, @{ n = "Root"; e = { if ($_.DisplayRoot -eq $null) { $_.Root } else { $_.DisplayRoot } } })
加载组合框的函数

function Load-ComboBox{
Param (
    [ValidateNotNull()]
    [Parameter(Mandatory=$true)]
    [System.Windows.Forms.ComboBox]$ComboBox,
    [ValidateNotNull()]
    [Parameter(Mandatory=$true)]
    $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}
这些条目看起来像
@{Name=C;Root=C:}
@Name=S;Root=\\server\share}

我希望它看起来像
抄送:\
S\\server\share


*抱歉,无法确定如何实际插入制表符,因为您正在向函数发送对象(选择对象返回对象),而不是制表符分隔的字符串数组,如果您这样调用该函数,该函数将正常工作:

$drives = (Get-PSDrive -PSProvider FileSystem | ForEach-Object { 
    $root = if ($_.DisplayRoot -eq $null) { $_.Root } else { $_.DisplayRoot }
    # output a tab-separated string that gets collected in the $drives variable
    "$($_.Name)`t$root"
})

Load-ComboBox -ComboBox $cboDomain -Items $drives

希望这能解释

谢谢你的解释@Theo。完美的