如何使用脚本将输出写入powershell GUI

时间:2017-07-06 16:37:09

标签: powershell user-interface scripting automation

Powershell新手在这里。我正在尝试编写一个有3个按钮和一个文本窗口的有用工具。每个按钮调用一个不同的PowerShell( .ps1)脚本来执行各种操作。如果我可以,我想将这些脚本的输出写入GUI文本窗口,但这似乎比人们想象的更难。如果那太多了,那么至少我希望至少显示脚本已在文本窗口中运行完成。像“脚本已经运行”的东西。

这是我到目前为止所拥有的。

Add-Type -AssemblyName System.Windows.Forms

function call_Clean 
{
      # Here the path to call your script
      . "C:\Scripts\Script1.ps1"     
}

function call_CreateTestLabConfig 
{
      # Here the path to call your script
      . "C:\Scripts\Script2.ps1"   

}

function call_LocalDeploy 
{
      # Here the path to call your script
      . "C:\Scripts\Script3.ps1"     
}

function CreateFormButton ( $locationheight, $locationwidth, $sizeheight, $sizewidth, $fieldname, $functionname ) {
  $Button = New-Object System.Windows.Forms.Button 
  $Button.Location = New-Object System.Drawing.Size($locationheight, $locationwidth) 
  $Button.Size = New-Object System.Drawing.Size($sizeheight, $sizewidth) 
  $Button.Text = $fieldname 
  $Button.Add_Click( $functionname ) 
  $Form.Controls.Add($Button) 
}

function CreateStartPosition ( $FormSize, $FormLocation ) {
 $Form.Size = New-Object System.Drawing.Size ($varFrmMSizeWidth, $varFrmMSizeHeight)
 $Form.Location = New-Object System.Drawing.Point($varFrmMLocationX, $varFrmMLocationY)
  $Form.Controls.Add($TextWindow) 
}


function CreateTextWindow ( $locationHeight, $LocationWidth, $TextBoxHeight, $TextBoxWidth ) {
  $TextWindow = new-object System.Windows.Forms.ListView
  $TextWindow.Size = New-Object System.Drawing.Size($textBoxHeight,$textBoxWidth)
  $TextWindow.location = new-object system.drawing.point($locationHeight,$LocationWidth)
  $Form.Controls.Add($TextWindow) 
}


$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Test Lab Tool"
#$Form.TopMost = $true
$Form.Size = New-Object System.Drawing.Size (475, 600)
$Form.Location = New-Object System.Drawing.Point(4000, 300)


CreateFormButton 20 100 120 40 'Clean' ${function:call_Clean}
CreateFormButton 170 100 120 40 'Create TestLab Config' ${function:call_CreateTestLabConfig}
CreateFormButton 315 100 120 40 'Local Deploy'
CreateTextWindow 20 160 415 375 'Test'

$Form.ShowDialog()

2 个答案:

答案 0 :(得分:0)

Powershell非常线性。当你调用一个脚本时,它会冻结GUI直到它完成。您可以使用基于脚本返回的信息更新GUI,或者只是等到脚本完成并使用脚本完成的信息更新GUI。

例如:

function call_Clean 
{
      # Here the path to call your script
      . "C:\Scripts\Script1.ps1"
      $TextWindow.Text = "Call Clean Script Completed"
}

或者:

脚本完成后,您可以在按钮的Click事件中使用相同的GUI更新$TextWindow.Text = "Call Clean Script Completed"

我已经更新了一些代码,以显示如何访问textwindow的text属性。 ================================================== =========================

Add-Type -AssemblyName System.Windows.Forms

function call_Clean 
{
      # Here the path to call your script
      . "C:\Scripts\Script1.ps1"
        $Form.Controls | % {
            if($_ -is [System.Windows.Forms.TextBox] -and $_.Name -eq "TestTextBox")
            {
                $_.Text = "test"
            }
        }
}

function call_CreateTestLabConfig 
{
      # Here the path to call your script
      . "C:\Scripts\Script2.ps1"   

}

function call_LocalDeploy 
{
      # Here the path to call your script
      . "C:\Scripts\Script3.ps1"     
}

function CreateFormButton ( $locationheight, $locationwidth, $sizeheight, $sizewidth, $fieldname, $functionname ) {
  $Button = New-Object System.Windows.Forms.Button 
  $Button.Location = New-Object System.Drawing.Size($locationheight, $locationwidth) 
  $Button.Size = New-Object System.Drawing.Size($sizeheight, $sizewidth) 
  $Button.Text = $fieldname 
  $Button.Add_Click( $functionname ) 
  $Form.Controls.Add($Button) 
}

function CreateStartPosition ( $FormSize, $FormLocation ) {
 $Form.Size = New-Object System.Drawing.Size ($varFrmMSizeWidth, $varFrmMSizeHeight)
 $Form.Location = New-Object System.Drawing.Point($varFrmMLocationX, $varFrmMLocationY)
 $Form.Controls.Add($TextWindow) 
}


function CreateTextWindow ( $locationHeight, $LocationWidth, $TextBoxHeight, $TextBoxWidth, $name) {
  $TextWindow = new-object System.Windows.Forms.TextBox
  $TextWindow.Size = New-Object System.Drawing.Size($textBoxHeight,$textBoxWidth)
  $TextWindow.location = new-object system.drawing.point($locationHeight,$LocationWidth)
  $TextWindow.Name = $name
  $TextWindow.Multiline = $true
  $Form.Controls.Add($TextWindow) 
}


$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Test Lab Tool"
#$Form.TopMost = $true
$Form.Size = New-Object System.Drawing.Size (475, 600)
$Form.Location = New-Object System.Drawing.Point(4000, 300)


CreateFormButton 20 100 120 40 'Clean' ${function:call_Clean}
CreateFormButton 170 100 120 40 'Create TestLab Config' ${function:call_CreateTestLabConfig}
CreateFormButton 315 100 120 40 'Local Deploy'
CreateTextWindow 20 160 415 375 'TestTextBox'

$Form.ShowDialog()

答案 1 :(得分:0)

我建议将这些脚本的输出分配给变量,并通过该信息进行解析(或者只要知道输出就实现逻辑)以发送到GUI。例如,如果使用写入输出,则在分配给变量时将捕获它,例如

$Out = & "Script.ps1"

然后你可以做类似

的事情
ForEach ($o in $Out)
{
    $str[] += $o
    $TextWindow.Text = $str
    Update()
}

注意:我没有WindowsForms经验