有人能指出我正确的方向吗?我想创建一个powershell脚本,打开带有复选框和字段的模块。
假设我有一些我想在服务器上运行的powershell脚本。我希望能够将它们全部放在窗口中,并且能够根据需要打开和关闭它们(带有复选框)。这是一些代码和我想要制作的GUI的例子。
因此,每个复选框都会将相应的代码添加到要运行的列表中。我应该能够获得GUI设置。它的运行按钮,获取复选框以调用脚本和变量字段我不太确定。
以下是我正在使用的代码
#This creates the form and sets its size and position
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Server Setup / Install Roles"
$objForm.Size = New-Object System.Drawing.Size(800,800)
#This creates a checkbox called Set Remote Execution to: unrestricted
$objSet_RECheckbox = New-Object System.Windows.Forms.Checkbox
$objSet_RECheckbox.Location = New-Object System.Drawing.Size(10,10)
$objSet_RECheckbox.Size = New-Object System.Drawing.Size(500,20)
$objSet_RECheckbox.Text = "Set Remote Execution to: unrestricted"
$objSet_RECheckbox.TabIndex = 1
$objForm.Controls.Add($objSet_RECheckbox)
#This creates a checkbox called Change time zone
$objTimeCheckbox = New-Object System.Windows.Forms.Checkbox
$objTimeCheckbox.Location = New-Object System.Drawing.Size(10,30)
$objTimeCheckbox.Size = New-Object System.Drawing.Size(500,20)
$objTimeCheckbox.Text = "Change time zone"
$objTimeCheckbox.TabIndex = 2
$objForm.Controls.Add($objTimeCheckbox)
#This creates a checkbox called Install DNS Role
$objDNSCheckbox = New-Object System.Windows.Forms.Checkbox
$objDNSCheckbox.Location = New-Object System.Drawing.Size(10,50)
$objDNSCheckbox.Size = New-Object System.Drawing.Size(500,20)
$objDNSCheckbox.Text = "Install DNS Role"
$objDNSCheckbox.TabIndex = 3
$objForm.Controls.Add($objDNSCheckbox)
#This creates a checkbox called Install DHCP Role
$objDHCPCheckbox = New-Object System.Windows.Forms.Checkbox
$objDHCPCheckbox.Location = New-Object System.Drawing.Size(10,70)
$objDHCPCheckbox.Size = New-Object System.Drawing.Size(500,20)
$objDHCPCheckbox.Text = "Install DHCP Role"
$objDHCPCheckbox.TabIndex = 4
$objForm.Controls.Add($objDHCPCheckbox)
#This creates a checkbox called Install Print Role
$objPrintCheckbox = New-Object System.Windows.Forms.Checkbox
$objPrintCheckbox.Location = New-Object System.Drawing.Size(10,90)
$objPrintCheckbox.Size = New-Object System.Drawing.Size(500,20)
$objPrintCheckbox.Text = "Install Print Role"
$objPrintCheckbox.TabIndex = 5
$objForm.Controls.Add($objPrintCheckbox)
#This creates a checkbox called Install AD and DC promo
$objAD_NewCheckbox = New-Object System.Windows.Forms.Checkbox
$objAD_NewCheckbox.Location = New-Object System.Drawing.Size(10,110)
$objAD_NewCheckbox.Size = New-Object System.Drawing.Size(500,20)
$objAD_NewCheckbox.Text = "Install AD and DC promo"
$objAD_NewCheckbox.TabIndex = 6
$objForm.Controls.Add($objAD_NewCheckbox)
#This creates a label for the DomainMode TextBox1
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,140)
$objLabel1.Size = New-Object System.Drawing.Size(280,20)
$objLabel1.Text = "Domain Mode"
$objForm.Controls.Add($objLabel1)
#This creates the DomainMode TextBox1
$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Location = New-Object System.Drawing.Size(10,160)
$objTextBox1.Size = New-Object System.Drawing.Size(100,20)
$objTextBox1.TabIndex = 0
$objForm.Controls.Add($objTextBox1)
#This creates the RUN button and sets the event
$RUNButton = New-Object System.Windows.Forms.Button
$RUNButton.Location = New-Object System.Drawing.Size(10,190)
$RUNButton.Size = New-Object System.Drawing.Size(75,23)
$RUNButton.Text = "RUN"
$RUNButton.Add_Click({$objForm.Close()})
$RUNButton.TabIndex = 9
$objForm.Controls.Add($RUNButton)
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
答案 0 :(得分:0)
我想你已经在那里回答了所有问题。
在您指的其他代码中,该按钮具有事件处理程序。无论在此代码块中是什么,当您按下按钮时都会执行。
在代码块中,代码正在查找使用if
循环检查了哪个复选框。您需要做的就是从正确的if
循环中调用所需的脚本。您也可以调用scriptblocks
而不是外部脚本文件,我认为这是一种更清洁的方式。
$handler_button1_Click=
{
$listBox1.Items.Clear();
if ($checkBox1.Checked) { "C:\MyScript\Script1.ps1" }
if ($checkBox2.Checked) { "C:\MyScript\Script2.ps1" }
if ($checkBox3.Checked) { "C:\MyScript\Script3.ps1" }
}
如果你想在执行后对脚本的结果做更多的事情,你也可以将它放在if
块内。
如果您想引用:Windows Patching Assistant
,我在GUI中编写了一个使用复选框的文件