感谢您的帮助
我有一个带有多个按钮的Powershell表单,可以调用不同的功能。 这些功能将执行一系列任务
我似乎无法工作的是执行结果(错误和所有)显示在我的Outputbox文本框中。
相反,它进入用于启动应用程序的powershell控制台,尝试了我可以在互联网上找到的所有方法,但我显然误解了如何执行此操作。
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") #loading the necessary .net libraries (using void to suppress output)
$form = New-Object System.Windows.Forms.Form
$form.Text = "Azure Environment Build"
$form.Size = New-Object System.Drawing.Size(850,675)
$form.StartPosition = "CenterScreen"
#################################################################################################################################################################################################### Start functions
function Test-Variables {
$Outputbox.Appendtext = ($InputJSON)
#$Outputbox.AppendText = $ResourceINF
#$Outputbox.AppendText = $EnvCode
#$Outputbox.AppendText = $Location
#$Outputbox.AppendText = $Region
#$Outputbox.Appendtext = New-AzureRMResourceGroupDeployment -Name deployment -ResourceGroupName $ResourceINF-NET-01 -TemplateFile -Verbose
}
function Stage1 {
write-host "stage 1"
}
function Stage2 {
write-host "stage 2"
}
function Stage3 {
write-host "stage 3"
}
function Stage4 {
write-host "stage 4"
}
function Stage5 {
write-host "stage 5"
}
function Stage6 {
write-host "stage 6"
}
function Stage7 {
write-host "stage 7"
}
function Stage8 {
write-host "stage 8"
}
function Stage9 {
write-host "stage 9"
}
function Stage10 {
write-host "stage 10"
}
function Stage11 {
write-host "stage 11"
}
function Stage12 {
write-host "stage 12"
}
#################################################################################################################################################################################################### End Function
#################################################################################################################################################################################################### Start Lines Section
$mypen = new-object Drawing.Pen red
$mypen.width = 3
$formGraphics = $form.createGraphics()
$form.add_paint({$formGraphics.DrawLine($mypen, 10, 65, 800, 65)})
$mypen2 = new-object Drawing.Pen green
$mypen2.width = 3
$formGraphics = $form.createGraphics()
$form.add_paint({$formGraphics.DrawLine($mypen2, 10, 165, 800, 165)})
$mypen1 = new-object Drawing.Pen blue
$mypen1.width = 3
$formGraphics = $form.createGraphics()
$form.add_paint({$formGraphics.DrawLine($mypen1, 10, 265, 800, 265)})
#################################################################################################################################################################################################### End Lines Section
#################################################################################################################################################################################################### Start Buttons
$Stage1Button = New-Object System.Windows.Forms.Button
$Stage1Button.Location = New-Object System.Drawing.Point(10,95)
$Stage1Button.Size = New-Object System.Drawing.Size(180,60)
$Stage1Button.Text = "Stage 1"
$Stage1Button.Add_Click({Stage1}) #the action triggered by the button
$Form.Controls.Add($Stage1Button) #activating the button inside the primary window
$Stage2Button = New-Object System.Windows.Forms.Button
$Stage2Button.Location = New-Object System.Drawing.Point(210,95)
$Stage2Button.Size = New-Object System.Drawing.Size(180,60)
$Stage2Button.Text = "Stage 2"
$Stage2Button.Add_Click({Stage2}) #the action triggered by the button
$Form.Controls.Add($Stage2Button) #activating the button inside the primary window
$Stage3Button = New-Object System.Windows.Forms.Button
$Stage3Button.Location = New-Object System.Drawing.Point(410,95)
$Stage3Button.Size = New-Object System.Drawing.Size(180,60)
$Stage3Button.Text = "Stage 3"
$Stage3Button.Add_Click({Stage3}) #the action triggered by the button
$Form.Controls.Add($Stage3Button) #activating the button inside the primary window
$Stage4Button = New-Object System.Windows.Forms.Button
$Stage4Button.Location = New-Object System.Drawing.Point(610,95)
$Stage4Button.Size = New-Object System.Drawing.Size(180,60)
$Stage4Button.Text = "Stage 4"
$Stage4Button.Add_Click({Stage4}) #the action triggered by the button
$Form.Controls.Add($Stage4Button) #activating the button inside the primary window
$Stage5Button = New-Object System.Windows.Forms.Button
$Stage5Button.Location = New-Object System.Drawing.Point(10,195)
$Stage5Button.Size = New-Object System.Drawing.Size(180,60)
$Stage5Button.Text = "Stage 5"
$Stage5Button.Add_Click({Stage5}) #the action triggered by the button
$Form.Controls.Add($Stage5Button) #activating the button inside the primary window
$Stage6Button = New-Object System.Windows.Forms.Button
$Stage6Button.Location = New-Object System.Drawing.Point(210,195)
$Stage6Button.Size = New-Object System.Drawing.Size(180,60)
$Stage6Button.Text = "Stage 6"
$Stage6Button.Add_Click({Stage6}) #the action triggered by the button
$Form.Controls.Add($Stage6Button) #activating the button inside the primary window
$Stage7Button = New-Object System.Windows.Forms.Button
$Stage7Button.Location = New-Object System.Drawing.Point(410,195)
$Stage7Button.Size = New-Object System.Drawing.Size(180,60)
$Stage7Button.Text = "Stage 7"
$Stage7Button.Add_Click({Stage7}) #the action triggered by the button
$Form.Controls.Add($Stage7Button) #activating the button inside the primary window
$Stage8Button = New-Object System.Windows.Forms.Button
$Stage8Button.Location = New-Object System.Drawing.Point(610,195)
$StageButton.Size = New-Object System.Drawing.Size(180,60)
$Stage8Button.Text = "Stage 8"
$Stage8Button.Add_Click({Stage8}) #the action triggered by the button
$Form.Controls.Add($Stage8Button) #activating the button inside the primary window
$Stage9Button = New-Object System.Windows.Forms.Button
$Stage9Button.Location = New-Object System.Drawing.Point(10,295)
$Stage9Button.Size = New-Object System.Drawing.Size(180,60)
$Stage9Button.Text = "Stage 9"
$Stage9Button.Add_Click({Stage9}) #the action triggered by the button
$Form.Controls.Add($Stage9Button) #activating the button inside the primary window
$Stage10Button = New-Object System.Windows.Forms.Button
$Stage10Button.Location = New-Object System.Drawing.Point(210,295)
$Stage10Button.Size = New-Object System.Drawing.Size(180,60)
$Stage10Button.Text = "Stage 10"
$Stage10Button.Add_Click({Stage10}) #the action triggered by the button
$Form.Controls.Add($Stage10Button) #activating the button inside the primary window
$Stage11Button = New-Object System.Windows.Forms.Button
$Stage11Button.Location = New-Object System.Drawing.Point(410,295)
$Stage11Button.Size = New-Object System.Drawing.Size(180,60)
$Stage11Button.Text = "Stage 11"
$Stage11Button.Add_Click({Stage11}) #the action triggered by the button
$Form.Controls.Add($Stage11Button) #activating the button inside the primary window
$Stage12Button = New-Object System.Windows.Forms.Button
$Stage12Button.Location = New-Object System.Drawing.Point(610,295)
$Stage12Button.Size = New-Object System.Drawing.Size(180,60)
$Stage12Button.Text = "Stage 12"
$Stage12Button.Add_Click({Stage12}) #the action triggered by the button
$Form.Controls.Add($Stage12Button) #activating the button inside the primary window
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(245,370)
$OKButton.Size = New-Object System.Drawing.Size(90,23)
$OKButton.Text = "Test"
$OKButton.Add_Click({Test-Variables}) #the action triggered by the button
$Form.Controls.Add($OKButton) #activating the button inside the primary window
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(445,370)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
#################################################################################################################################################################################################### End Buttons
#################################################################################################################################################################################################### Start Label
$labelenv = New-Object System.Windows.Forms.Label
$labelenv.Location = New-Object System.Drawing.Point(10,10)
$labelenv.Size = New-Object System.Drawing.Size(280,20)
$labelenv.Text = "Please Select the Environment"
$labelenv.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 12, [System.Drawing.FontStyle]::Bold)
$form.Controls.Add($labelenv)
$labelsec1 = New-Object System.Windows.Forms.Label
$labelsec1.Location = New-Object System.Drawing.Point(300,60)
$labelsec1.Size = New-Object System.Drawing.Size(200,20)
$labelsec1.Text = "Section 1 - Pre-Reqs"
$labelsec1.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 10, [System.Drawing.FontStyle]::Bold)
$form.Controls.Add($labelsec1)
$labelsec2 = New-Object System.Windows.Forms.Label
$labelsec2.Location = New-Object System.Drawing.Point(300,160)
$labelsec2.Size = New-Object System.Drawing.Size(200,20)
$labelsec2.Text = "Section 2 - Infrastructure"
$labelsec2.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 10, [System.Drawing.FontStyle]::Bold)
$form.Controls.Add($labelsec2)
$labelsec3 = New-Object System.Windows.Forms.Label
$labelsec3.Location = New-Object System.Drawing.Point(300,260)
$labelsec3.Size = New-Object System.Drawing.Size(200,20)
$labelsec3.Text = "Section 3 - Application"
$labelsec3.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 10, [System.Drawing.FontStyle]::Bold)
$form.Controls.Add($labelsec3)
#################################################################################################################################################################################################### End Label
#################################################################################################################################################################################################### Start Output Box
$Global:OutputBox = New-Object System.Windows.Forms.RichTextBox
$OutputBox.Location = New-Object System.Drawing.Point(10,400)
$OutputBox.Size = New-Object System.Drawing.Size(800,200)
$form.Controls.Add($OutputBox)
#################################################################################################################################################################################################### End Label
#################################################################################################################################################################################################### Start ComboBox
$arrayCompany=@("Production", "Pre-Production", "Integration", "Training", "UK-QA", "UK-DEV", "SEA-QA", "SEA-DEV")
$comboBoxCompany = New-Object System.Windows.Forms.ComboBox
$comboBoxCompany.Location = New-Object System.Drawing.Size(535,10)
$comboBoxCompany.Size = New-Object System.Drawing.Size(240,30)
$comboBoxCompany.DropDownHeight = 200
$comboBoxCompany.Font = [System.Drawing.Font]::new("Microsoft Sans Serif", 10, [System.Drawing.FontStyle]::Bold)
$Form.Controls.Add($comboBoxCompany)
foreach ($company in $arraycompany) {
$comboBoxCompany.Items.Add($company)
}
$comboBoxCompany_SelectedIndexChanged=
{
If ($comboBoxCompany.text -eq "Production")
{
$global:InputJSON="azuredeploy.parameters.pro.json"
$global:ResourceINF="RG-INF-PRO"
$global:EnvCode="PRO"
$global:Location="UK South"
$global:Region="UK"
}
ElseIf ($comboBoxCompany.text -eq "Pre-Production")
{
$global:test="This has set correctly to Pre-Production"
}
ElseIf ($comboBoxCompany.text -eq "Integration")
{
$global:test="This has set correctly to Integration"
}
ElseIf ($comboBoxCompany.text -eq "Training")
{
$global:test="This has set correctly to Training"
}
ElseIf ($comboBoxCompany.text -eq "UK-QA")
{
$global:test="This has set correctly to UK-QA"
}
ElseIf ($comboBoxCompany.text -eq "UK-DEV")
{
$global:test="This has set correctly to UK-DEV"
}
ElseIf ($comboBoxCompany.text -eq "SEA-QA")
{
$global:test="This has set correctly to SEA-QA"
}
ElseIf ($comboBoxCompany.text -eq "SEA-DEV")
{
$global:test="This has set correctly to SEA-DEV"
}
$form.Controls.Add($listBoxClub)
}
################ MUST CREATE BEFORE ASSIGN ################
$comboBoxCompany.add_SelectedIndexChanged($comboBoxCompany_SelectedIndexChanged)
#################################################################################################################################################################################################### End ComboBox
$form.Add_Shown({$textBox.Select()})
[void] $form.ShowDialog()
答案 0 :(得分:1)
您没有在功能中与GUI TextBox交互。在任何情况下,文字Write-Host
都将使用PSHost控制台。
我认为使这些事情有用的一件事是声明一个别名:
function Out-Gui($s) {
$OutbutBox.AppendText($s);
}
Set-Alias -Name Write-Host -Value Out-Gui -Scope Global
这避免了相当多的copypasta(WinForms结构已经非常多风),并且允许函数/脚本在CLI和GUI使用之间保持不变。 ( - 我为WPF提供了更精细的Out-Gui function,顺便说一句。)
要捕获错误,您必须实施try
/ catch
块,TRAP
或捕获-ErrorVariable
。覆盖Out-Default
和流并不是那么简单。