如何从 PowerShell 的列表框中捕获选定的值?
How do I capture the selected value from a ListBox in PowerShell?
我正在尝试创建一个弹出窗口,要求用户 select 一个部门,然后根据该部门 运行 一个 selection Choco 命令来安装每个部门需要的软件使用。我 运行 遇到了一个问题,它没有返回 selected #var。
到目前为止的疑难解答表明,无论我 select 在列表中的什么项目,它都只是 运行 第一个 IF 部分下的命令,然后停止。
我试图缩减我的代码,只要求它打印每个业务部门,这样我就可以检查列表框设置,但我找不到我缺少的东西。
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Department"
$objForm.Size = New-Object System.Drawing.Size(249,190)
$objForm.StartPosition = "CenterScreen"
#OK Button action
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$TSDepartment=$objListBox.SelectedItem;$objForm.Close()}})
#Ok Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,115)
$OKButton.Size = New-Object System.Drawing.Size(200,30)
$OKButton.Text = "OK"
$OKButton.Add_Click({$TSDepartment=$objListBox.SelectedItem;$objForm.Close()})
$objForm.Controls.Add($OKButton)
# List box Lable
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(200,20)
$objLabel.Text = "Please select a Department:"
$objForm.Controls.Add($objLabel)
#list box Side
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(200,20)
$objListBox.Height = 70
#pick list
[void] $objListBox.Items.Add("Call-Center")
[void] $objListBox.Items.Add("Law-Office")
[void] $objListBox.Items.Add("Admin-Support")
[void] $objListBox.Items.Add("Base")
[void] $objListBox.Items.Add("IT")
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
#resulting var
#$TSDepartment
# Simple PowerShell ElseIf
# Admin-Support softwre list
if ($TSDepartment = "Admin-Support")
{
'Admin Support Software'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
choco install 7zip --ignore-checksum
choco install adobereader --ignore-checksum
choco install cutepdf --ignore-checksum
choco install jre8 --ignore-checksum
choco install Silverlight --ignore-checksum
choco install vlc --ignore-checksum
choco install webex --ignore-checksum
}
# Law Office softwre list
ElseIf ( $TSDepartment = "Law-Office")
{
'Law Office Software'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
choco install 7zip --ignore-checksum
choco install adobereader --ignore-checksum
choco install cutepdf --ignore-checksum
choco install vlc --ignore-checksum
choco install jre8 --ignore-checksum
choco install rsclientprint --ignore-checksum
choco install Silverlight --ignore-checksum
choco install webex --ignore-checksum
}
# Call Center softwre list
ElseIf ( $TSDepartment = "Call-Center")
{
'Call Center Software'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
choco install adobereader --ignore-checksum
choco install cutepdf --ignore-checksum
choco install jre8 --ignore-checksum
choco install Silverlight --ignore-checksum
choco install vlc --ignore-checksum
choco install webex --ignore-checksum
choco install Silverlight --ignore-checksum
choco install softphone --ignore-checksum
}
# IT softwre list
ElseIf ( $TSDepartment = "IT")
{
'IT Software'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
choco install 7zip --ignore-checksum
choco install adobereader --ignore-checksum
choco install cutepdf --ignore-checksum
choco install jre8 --ignore-checksum
choco install Silverlight --ignore-checksum
choco install vlc --ignore-checksum
choco install webex --ignore-checksum
choco install Firefox --ignore-checksum
choco install foxitreader --ignore-checksum
choco install Ghostscript.app --ignore-checksum
choco install GoogleChrome --ignore-checksum
choco install greenshot --ignore-checksum
choco install notepadplusplus --ignore-checksum
choco install PowerShell --ignore-checksum
choco install putty --ignore-checksum
choco install sysinternals --ignore-checksum
choco install windirstat --ignore-checksum
choco install wireshark --ignore-checksum
}
# Base Software List
ElseIf ( $TSDepartment = "Base")
{
'Base Software'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
choco install adobereader --ignore-checksum
choco install cutepdf --ignore-checksum
choco install jre8 --ignore-checksum
choco install Silverlight --ignore-checksum
choco install webex --ignore-checksum
}
# null Software List
Else
{
'Upgrade Choco'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
}
简单的 PowerShell 错误,但 = 是一个赋值:
$TSDepartment -eq "Admin-Support"
那是你的比较运算符。
补充说明:
- -ne -> 不等于
- -ceq -> 区分大小写等于
- -cne -> 大小写不等于
我正在尝试创建一个弹出窗口,要求用户 select 一个部门,然后根据该部门 运行 一个 selection Choco 命令来安装每个部门需要的软件使用。我 运行 遇到了一个问题,它没有返回 selected #var。
到目前为止的疑难解答表明,无论我 select 在列表中的什么项目,它都只是 运行 第一个 IF 部分下的命令,然后停止。
我试图缩减我的代码,只要求它打印每个业务部门,这样我就可以检查列表框设置,但我找不到我缺少的东西。
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Department"
$objForm.Size = New-Object System.Drawing.Size(249,190)
$objForm.StartPosition = "CenterScreen"
#OK Button action
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$TSDepartment=$objListBox.SelectedItem;$objForm.Close()}})
#Ok Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,115)
$OKButton.Size = New-Object System.Drawing.Size(200,30)
$OKButton.Text = "OK"
$OKButton.Add_Click({$TSDepartment=$objListBox.SelectedItem;$objForm.Close()})
$objForm.Controls.Add($OKButton)
# List box Lable
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(200,20)
$objLabel.Text = "Please select a Department:"
$objForm.Controls.Add($objLabel)
#list box Side
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(200,20)
$objListBox.Height = 70
#pick list
[void] $objListBox.Items.Add("Call-Center")
[void] $objListBox.Items.Add("Law-Office")
[void] $objListBox.Items.Add("Admin-Support")
[void] $objListBox.Items.Add("Base")
[void] $objListBox.Items.Add("IT")
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
#resulting var
#$TSDepartment
# Simple PowerShell ElseIf
# Admin-Support softwre list
if ($TSDepartment = "Admin-Support")
{
'Admin Support Software'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
choco install 7zip --ignore-checksum
choco install adobereader --ignore-checksum
choco install cutepdf --ignore-checksum
choco install jre8 --ignore-checksum
choco install Silverlight --ignore-checksum
choco install vlc --ignore-checksum
choco install webex --ignore-checksum
}
# Law Office softwre list
ElseIf ( $TSDepartment = "Law-Office")
{
'Law Office Software'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
choco install 7zip --ignore-checksum
choco install adobereader --ignore-checksum
choco install cutepdf --ignore-checksum
choco install vlc --ignore-checksum
choco install jre8 --ignore-checksum
choco install rsclientprint --ignore-checksum
choco install Silverlight --ignore-checksum
choco install webex --ignore-checksum
}
# Call Center softwre list
ElseIf ( $TSDepartment = "Call-Center")
{
'Call Center Software'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
choco install adobereader --ignore-checksum
choco install cutepdf --ignore-checksum
choco install jre8 --ignore-checksum
choco install Silverlight --ignore-checksum
choco install vlc --ignore-checksum
choco install webex --ignore-checksum
choco install Silverlight --ignore-checksum
choco install softphone --ignore-checksum
}
# IT softwre list
ElseIf ( $TSDepartment = "IT")
{
'IT Software'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
choco install 7zip --ignore-checksum
choco install adobereader --ignore-checksum
choco install cutepdf --ignore-checksum
choco install jre8 --ignore-checksum
choco install Silverlight --ignore-checksum
choco install vlc --ignore-checksum
choco install webex --ignore-checksum
choco install Firefox --ignore-checksum
choco install foxitreader --ignore-checksum
choco install Ghostscript.app --ignore-checksum
choco install GoogleChrome --ignore-checksum
choco install greenshot --ignore-checksum
choco install notepadplusplus --ignore-checksum
choco install PowerShell --ignore-checksum
choco install putty --ignore-checksum
choco install sysinternals --ignore-checksum
choco install windirstat --ignore-checksum
choco install wireshark --ignore-checksum
}
# Base Software List
ElseIf ( $TSDepartment = "Base")
{
'Base Software'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
choco install adobereader --ignore-checksum
choco install cutepdf --ignore-checksum
choco install jre8 --ignore-checksum
choco install Silverlight --ignore-checksum
choco install webex --ignore-checksum
}
# null Software List
Else
{
'Upgrade Choco'
choco upgrade chocolatey
get-executionpolicy
set-executionpolicy remotesigned
}
简单的 PowerShell 错误,但 = 是一个赋值:
$TSDepartment -eq "Admin-Support"
那是你的比较运算符。
补充说明:
- -ne -> 不等于
- -ceq -> 区分大小写等于
- -cne -> 大小写不等于