问题描述
好吧,我几乎可以按照我需要的方式来工作。最后一件事...而不是为我的每个技术人员更改脚本中的用户名...我想使用一个文本框,他们可以输入用户名..现在我将其设置的方式连接到交换机但是它要求输入的密码不是PIN&PASSCODE,就像我在脚本中保留用户名时通常那样。
#############
# Variables #
#############
$form1 = New-Object System.Windows.Forms.Form
$comboBox1 = New-Object System.Windows.Forms.ComboBox
$label1 = New-Object System.Windows.Forms.Label
$button1 = New-Object System.Windows.Forms.Button
$username = New-Object System.Windows.Forms.TextBox
$label2 = New-Object System.Windows.Forms.Label
###########################################################
# FORM LAYOUT #
###########################################################
$CBEvent_SelectedindexChanged = {
$label1.Text = $comboBox1.SelectedItem.Address.ToString()
}
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 292
$System_Drawing_Size.Height = 266
$form1.ClientSize = $System_Drawing_Size
###################
# Label1 Location #
###################
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 125
$System_Drawing_Point.Y = 5
###################
# Label2 Location #
###################
$System_Drawing_Point3 = New-Object System.Drawing.Point
$System_Drawing_Point3.X = 130
$System_Drawing_Point3.Y = 50
###################
# Button Location #
###################
$System_Drawing_Point1 = New-Object System.Drawing.Point
$System_Drawing_Point1.X = 157
$System_Drawing_Point1.Y = 140
######################
# TextBox1 Location #
######################
$System_Drawing_Point2 = New-Object System.Drawing.Point
$System_Drawing_Point2.X = 150
$System_Drawing_Point2.Y = 110
#############
# Control's #
#############
$label1.Location = $System_Drawing_Point
$label1.Text = 'Select Switch'
$label2.Location = $System_Drawing_Point3
$label2.Text = 'Enter NEMA Account'
$form1.Controls.Add($label1)
$form1.Controls.Add($comboBox1)
$form1.Controls.Add($button1)
$form1.Controls.Add($username)
$username.Location = $System_Drawing_Point2
$button1.Location = $System_Drawing_Point1
$button1.Text = 'Connect'
$button1.Add_Click({cmd /c start powershell -Command ssh $username@$($label1.Text)})
$form1.add_Load($FormEvent_Load)
$ComboBox1.add_SelectedindexChanged($CBEvent_SelectedindexChanged)
$form1.ShowDialog()| Out-Null
尤其是这行... $button1.Add_Click({cmd /c start powershell -Command ssh $username@$($label1.Text)})
不工作。
$button1.Add_Click({cmd /c start powershell -Command ssh domain\myusername@$($label1.Text)})
工作
其作用类似于用户名吗?我在正确的道路上吗?
感谢您的帮助!
解决方法
至此...
为什么文本框不将信息传递给变量?
...好吧,您根本没有使用文本框。
这是不正确的...
$button1.Add_Click({cmd /c start powershell -Command ssh $username@$($label1.Text)})
...您实际上是在要求标签显示文本而不是在组合框中选择的内容。
从UX / UI的角度来看,从用户交互表单元素中获取文本,然后将该文本分配给UX标签,然后从该label元素中调用结果文本是不正确或谨慎的。
您从用户交互表单元素(文本框,组合框,数据网格等)中获取文本内容
您不使用标签控件来托管用户交互式提供的数据。它们只是横幅信息,指示应在用户交互表单元素中输入/选择的内容。
例如:
# sample form with,no label elements,just interactive elements,combobox selection to richtextbox
Add-Type -AssemblyName System.Drawing
$ComboBox = New-Object System.Windows.Forms.ComboBox
$ComboBox.Location = New-Object System.Drawing.Point(10,10)
$ComboBox.Items.AddRange(@('One','Two'))
$RichTextBox = New-Object System.Windows.Forms.RichTextBox
$RichTextBox.Location = New-Object System.Drawing.Point(10,40)
$Form = New-Object System.Windows.Forms.Form
$Form.Controls.Add($ComboBox)
$Form.Controls.Add($RichTextBox)
$ComboBox.Add_TextChanged({
# Code here
switch($ComboBox.Text){
"One" {$RichTextBox.Text = "This is one"}
"Two" {$RichTextBox.Text = "This is two"}
}
})
$Form.ShowDialog()
因此,仅针对交互式结果,即可快速解决您的GUI问题...
#############
# Variables #
#############
$form1 = New-Object System.Windows.Forms.Form
$combobox1 = New-Object System.Windows.Forms.ComboBox
$label1 = New-Object System.Windows.Forms.Label
$button1 = New-Object System.Windows.Forms.Button
$username = New-Object System.Windows.Forms.TextBox
$label2 = New-Object System.Windows.Forms.Label
###########################################################
# FORM LAYOUT #
###########################################################
$CBEvent_SelectedIndexChanged = {
$combobox1.SelectedItem
}
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 292
$System_Drawing_Size.Height = 266
$form1.ClientSize = $System_Drawing_Size
###################
# Label1 Location #
###################
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 125
$System_Drawing_Point.Y = 5
###################
# Label2 Location #
###################
$System_Drawing_Point3 = New-Object System.Drawing.Point
$System_Drawing_Point3.X = 130
$System_Drawing_Point3.Y = 50
###################
# Button Location #
###################
$System_Drawing_Point1 = New-Object System.Drawing.Point
$System_Drawing_Point1.X = 157
$System_Drawing_Point1.Y = 140
######################
# Textbox1 Location #
######################
$System_Drawing_Point2 = New-Object System.Drawing.Point
$System_Drawing_Point2.X = 150
$System_Drawing_Point2.Y = 110
#############
# Control's #
#############
$label1.Location = $System_Drawing_Point
$label1.Text = 'Select Switch'
$combobox1.Items.AddRange(@('Switch1','Switch2'))
$label2.Location = $System_Drawing_Point3
$label2.Text = 'Enter NEMA Account'
$form1.Controls.Add($label1)
$form1.Controls.Add($combobox1)
$form1.Controls.Add($button1)
$form1.Controls.Add($username)
$username.Location = $System_Drawing_Point2
$button1.Location = $System_Drawing_Point1
$button1.Text = 'Connect'
$button1.Add_Click({
"$($username.Text)@$($combobox1.Text)" | Out-Host
})
# $form1.add_Load($FormEvent_Load) # you are not using this anywhere,so,not needed.
$ComboBox1.add_SelectedIndexChanged($CBEvent_SelectedIndexChanged)
$form1.ShowDialog()| Out-Null
但是,我不得不说,您确实应该使用拖放设计器来帮助您安排UX / UI工作,而不是硬编码。 UX / UI最佳实践中还有其他地方不合时宜。
使用以下方法查看:https://poshgui.com,或使用免费的Visual Studio 2019社区版,还有其他一些。另请参见YouTube上的“ Windows窗体”和“ WPF窗体”开发视频,以获取更多示例/指导。
,这是完整的工作代码,您可以随意使用它,只需更改开关信息和nema帐户下的值即可。
代码:
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#############
# Variables #
#############
$form1 = New-Object System.Windows.Forms.Form
$combobox1 = New-Object System.Windows.Forms.ComboBox
$combobox2 = New-Object System.Windows.Forms.ComboBox
$label1 = New-Object System.Windows.Forms.Label
$label2 = New-Object System.Windows.Forms.Label
$button1 = New-Object System.Windows.Forms.Button
##################################################################
# SWITCH INFORMATION - SWitch Combo Box #
##################################################################
$FormEvent_Load = {
##################################################################################################################### Basement
$combobox1.DisplayMember = 'Name'
$combobox1.Items.Add(
[PSCustomObject]@{
'Name' = '******BASEMENT******'
})
$combobox1.DisplayMember = 'Name'
$combobox1.Items.Add(
[PSCustomObject]@{
'Name' = 'GA- GC105C'
'Address' = [ipaddress]::Parse('10.10.10.100')
})
}
##################################################################
# NEMA INFORMATION - NEMA COMBOBOX #
##################################################################
$FormEvent_Load2 = {
$combobox2.DisplayMember = 'Name'
$combobox2.Items.Add(
[PSCustomObject]@{
'Name' = 'Aaron'
'UserName' = ('domain\useracct')
})
}
###########################################################
# FORM LAYOUT #
###########################################################
$CBEvent_SelectedIndexChanged = {
$label1.Text = $combobox1.SelectedItem.Address.ToString()
}
$CBEvent_SelectedIndexChanged2 = {
$label2.Text = $combobox2.SelectedItem.Username.ToString()
}
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 292
$System_Drawing_Size.Height = 266
$form1.ClientSize = $System_Drawing_Size
###################
# Label1 Location #
###################
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 125
$System_Drawing_Point.Y = 5
###################
# Label2 Location #
###################
$System_Drawing_Point3 = New-Object System.Drawing.Point
$System_Drawing_Point3.X = 150
$System_Drawing_Point3.Y = 85
###################
# Button Location #
###################
$System_Drawing_Point1 = New-Object System.Drawing.Point
$System_Drawing_Point1.X = 157
$System_Drawing_Point1.Y = 140
#######################
# combo box2 Location #
#######################
$System_Drawing_Point2 = New-Object System.Drawing.Point
$System_Drawing_Point2.X = 150
$System_Drawing_Point2.Y = 110
#############
# Control's #
#############
$form1.Controls.Add($combobox1)
$form1.Controls.Add($combobox2)
$form1.Controls.Add($label1)
$form1.Controls.Add($label2)
$form1.Controls.Add($button1)
$combobox2.Location = $System_Drawing_Point2
$label1.Location = $System_Drawing_Point
$label1.Text = 'Select Switch:'
$label2.Location = $System_Drawing_Point3
$label2.Text = 'Select NEMA Account:'
$button1.Location = $System_Drawing_Point1
$button1.Text = 'Connect'
$button1.Add_Click({cmd /c start powershell -Command ssh -l $($label2.Text)@$($label1.Text)})
$form1.add_Load($FormEvent_Load)
$form1.add_Load($FormEvent_Load2)
$ComboBox1.add_SelectedIndexChanged($CBEvent_SelectedIndexChanged)
$ComboBox2.add_SelectedIndexChanged($CBEvent_SelectedIndexChanged2)
$form1.ShowDialog()| Out-Null