【问题标题】:How to write output to powershell GUI with Scripts如何使用脚本将输出写入 powershell GUI
【发布时间】:2017-12-10 19:07:07
【问题描述】:

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

【问题讨论】:

  • 替换你的函数来做& "Script.ps1"。所有 dot-sourcing 所做的就是将脚本的内容转储到这些功能块中。
  • 这对输出到文本窗口有帮助吗?
  • 它将帮助您的脚本运行。除非您重定向或捕获输出,否则这些脚本的输出将转到输出流。

标签: powershell user-interface scripting automation


【解决方案1】:

我建议将这些脚本的输出分配给一个变量并解析该信息(或者如果您知道输出,则仅实现逻辑)以发送到 GUI。例如,如果您使用 Write-Output,它将在分配给变量时被捕获,例如

$Out = & "Script.ps1"

然后你可以做类似的事情

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

注意:我没有 WindowsForms 经验

【讨论】:

    【解决方案2】:

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

    例如:

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

    或者:

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

    我已经稍微更新了您的代码,以展示如何访问文本窗口的文本属性。 ==================================================== ==========================

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

    【讨论】:

    • 嗯脚本正在执行,但我仍然没有收到任何输出到文本窗口。另外,当我使用 $TextWindow.Text 时,它没有显示 .Text 是一个挂在 $TextWindow 的属性
    • 我注意到您正在为您的 TextWindow 使用 ListView。这是故意的吗?还是您想使用 TextBox 之类的东西?
    • 感谢您的评论,我实际上也意识到了这一点,如果我在函数 CreateTextWindow 中执行它,我就能够写入文本框。如果我尝试在函数 call_clean 中使用 $textWindow.Text,它不会写入文本框。它给出了这个错误......
    • 在此对象上找不到属性“文本”。验证该属性是否存在并且可以设置。
    • 为你的构造函数添加一个 Name 属性。由于您正在动态构建 GUI 按钮和文本框,因此您仍然需要能够在某些时候引用它们的名称。或者,您可以单独创建每个按钮,然后直接引用变量名(例如,$textwindow、$button1、$button2 等)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2021-09-30
    • 2017-01-09
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    相关资源
    最近更新 更多