【发布时间】:2021-04-21 17:56:31
【问题描述】:
1.csv:
服务器名称
服务器1
服务器2
服务器3
我想将 csv 文件导入组合框并将所选值导入变量。 我可以将上述文件加载到组合框,但输出变量为空
function button ($WF) {
###################Load Assembly for creating form & button######
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 190;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form.ControlBox = $True
##############Define text label2
$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 80;
$textLabel2.Text = $WF;
############Define text box2 for input
$cBox2 = New-Object “System.Windows.Forms.combobox”;
$cBox2.Left = 150;
$cBox2.Top = 80;
$cBox2.width = 200;
###############"Add descriptions to combo box"##############
$NameHash = @{}
import-csv "C:\Users\Komp\Desktop\1.csv" | ForEach-Object {
$cBox2.Items.Add($_.ServerName)
}
#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 45;
$button.Width = 100;
$button.Text = “Ok”;
$Button.Cursor = [System.Windows.Forms.Cursors]::Hand
$Button.Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::BOLD)
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$cBox2.Text;
$form.Close();};
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel2);
$form.Controls.Add($cBox2);
$ret = $form.ShowDialog();
#################return values
$output = $cBox2.SelectedItem.ToString()
}
$return = button “Job Descriptions"
问题是$output变量为空,如何将选中的值导出到$output变量中?
从this 问题获得此代码
【问题讨论】:
-
试试
$output = $cBox2.SelectedItem.Text -
this 回答你的问题了吗?
-
@Sceptalist ,还没试过,还是空的
-
这是范围的问题,试试我的链接答案。
标签: powershell