简体   繁体   English

用映射的驱动器填充combox

[英]Populate combox with mapped drives

Trying to populate a combo box with mapped drive letters (name) and FQDN (Root). 尝试使用映射的驱动器号(名称)和FQDN(根)填充组合框。 I think I have most of the code but the combo box entries includes coded entries. 我想我拥有大多数代码,但是组合框条目包括编码条目。

I'm not only curious about how to fix this but why the results are entered this way. 我不仅对如何解决此问题感到好奇,而且对为什么以这种方式输入结果感到好奇。 Running this via command line does not display results this way. 通过命令行运行此命令不会以这种方式显示结果。

NOTE: I'm also using a function to populate the combo box. 注意:我还使用一个函数来填充组合框。

Code to retrieve mapped drives 检索映射驱动器的代码

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

Function to load combo box 加载组合框的功能

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}

The entries look like; 条目看起来像;
@{Name=C; @ {名称= C; Root=C:} 根= C:}
@Name=S; @名称= S; Root=\\\\server\\share} 根= \\\\服务器\\共享}

I want it to look like; 我希望它看起来像;
C<-tab->C:\\ ç<-tab-> C:\\
S<-tab->\\\\server\\share 小号<-Tab - > \\\\服务器\\共享

*Sorry couldn't figure out how to actually insert tab *抱歉无法弄清楚如何实际插入标签

Since you are sending Objects to the function (Select-Object returns objects), and not an array of tab separated strings, the function would work if you call it like this: 由于您是将对象发送到函数(选择对象返回对象),而不是使用制表符分隔的字符串数组,因此,如果这样调用该函数,它将起作用:

$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

Hope that explains 希望能解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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