组合框选择变量



我正在编写一个GUI脚本,将网络共享映射为驱动器。我已经创建了(在其他SO用户的帮助下(一个工作组合框,列出了未使用的驱动器let。我现在想做的是,在单击按钮后,将选定的组合框项目捕获到一个变量中,用于脚本的另一个尚未实现的部分。到目前为止,我已经设法获得了"编写宿主"所选项目的脚本。请帮助

$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 = "Literki"
$button.Add_Click($button_click)
$button2 = New-Object System.Windows.Forms.Button
$button2.Size = New-Object System.Drawing.Size(50,20)
$button2.Location = New-Object System.Drawing.Size(20,80)
$button2.Text = "Text"
$button2.Add_Click($button_click2)
$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.Add_SelectedIndexChanged(
{ $A =$ComboBox1.Text
write-host $A}
)
$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($Letter) }

$button_click = {Write-Host $FreeLetters} 
$button_click2 = {write-host $A}
$form.Controls.AddRange(@($button, $combobox1,$button2))
$form.ShowDialog()

您需要确定变量的范围,以便它在函数之外可用。

$comboBox1.Add_SelectedIndexChanged({
$Script:A =$ComboBox1.Text
})
$button_click2 = {Write-Host $Script:A}

请参阅https://www.varonis.com/blog/powershell-variable-scope/有关更多详细信息,

相关内容

  • 没有找到相关文章

最新更新