DriveLetter ComboBox 未正确填充

问题描述

我正在创建一个 GUI 脚本,显示当前未使用/未分配的驱动器号。 虽然该功能正常工作(ECHO 按钮将正确的字母写入 PS),但 ComboBox 由单个字母填充(见图)。有人可以向我解释我做错了什么吗?

Write-Host & ComboBox

Current Assigned Letters

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Test' 
$form.Size = New-Object System.Drawing.Size(280,400) 
$form.StartPosition = 'Manual'
$form.Location      = '10,10'
$form.Topmost = $true  

$button = New-Object System.Windows.Forms.Button
$button.Size = New-Object System.Drawing.Size(50,20)
$button.Location = New-Object System.Drawing.Size(20,20)
$button.Text = "ECHO"
$button.Add_Click($button_click)

$comboBox1 = New-Object System.Windows.Forms.ComboBox 
$comboBox1.Location = New-Object System.Drawing.Point(80,55) 
$comboBox1.Size = New-Object System.Drawing.Size(98,10) 
$ComboBox1.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;

$AllLetters = 67..90 | ForEach-Object {[char]$_ + ":"}
$UsedLetters = Get-WmiObject Win32_Logicaldisk | Select -Expand deviceid
$FreeLetters = $AllLetters | Where-Object {$UsedLetters -notcontains $_}
ForEach ($Letter in $FreeLetters) { $comboBox1.Items.Add($Drives) }



$button_click = {Write-Host ($FreeLetters)}                   

$form.Controls.AddRange(@($button,$comboBox1))
$form.ShowDialog()

解决方法

在这一行:

.scrollable {
   padding-top:0.5%;
   height: 200px;
   overflow-y: scroll;
}

ForEach ($Letter in $FreeLetters) { $comboBox1.Items.Add($Drives) } 改为 $Drives