将Powershell结果输出到RichTextBox

将Powershell结果输出到RichTextBox,powershell,Powershell,提前谢谢你的帮助 我有一个带有多个按钮的Powershell表单,这些按钮调用单独的函数。 这些功能将执行一系列任务 我似乎无法将执行结果(错误和所有错误)显示到我的Outputbox文本框中 相反,它进入了用于启动应用程序的powershell控制台,尝试了我在internet上能找到的所有方法,但我显然不理解如何做到这一点 [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [S

提前谢谢你的帮助

我有一个带有多个按钮的Powershell表单,这些按钮调用单独的函数。 这些功能将执行一系列任务

我似乎无法将执行结果(错误和所有错误)显示到我的Outputbox文本框中

相反,它进入了用于启动应用程序的powershell控制台,尝试了我在internet上能找到的所有方法,但我显然不理解如何做到这一点

[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()

您没有在函数中与GUI文本框交互。文本
写入主机
在任何情况下都将使用PSHost控制台

然而,我发现有一件事很有用,那就是声明一个别名:

function Out-Gui($s) {
    $OutbutBox.AppendText($s);
}

Set-Alias -Name Write-Host  -Value Out-Gui  -Scope Global
这避免了相当多的复制(WinForms构造已经相当多了),并允许函数/脚本在CLI和GUI使用之间保持不变。(-顺便说一句,我对WPF有了更详细的说明。)


要捕获错误,您必须实现
try
/
catch
块、
陷阱
或捕获
-error变量
。覆盖默认值和流并不是那么简单。

覆盖。。。流并不是那么简单。
如果可能的话。非常感谢。然而,我认为这是我在#$Outputbox.Appendtext=New AzureRMResourceGroupDeployment-Name deployment-ResourceGroupName$ResourceINF-NET-01-TemplateFile行中尝试过的-Verbose@JoeAppendText是一个方法,不是要分配给的变量。如果您需要添加一些对象,也可以先通过
| Out String
将其强制转换。简单到$outputbox.appendtext='some stuff'| Out-String@Joe方法调用,不是赋值。非常感谢。然而,我认为这就是我在“#$Outputbox.Appendtext=New AzureRMResourceGroupDeployment-Name deployment-ResourceGroupName$ResourceINF-NET-01-TemplateFile-Verbose”行中尝试的但是它不起作用,因此#虽然失败了,但write host命令更多的是占位符,我实际上是在执行powershell命令,因此如果我能将控制台放在同样有效的窗体上。再次感谢