将cmdlet的详细程度输出到GUI文本框Powershell

时间:2017-07-19 13:33:29

标签: winforms powershell user-interface scripting automation

我们说我有一个带有1个按钮和1个文本框的表单。所有按钮都是使用copy-item。如何将该副本的输出到文本框?更进一步,我希望它能够实时完成,就像我在看控制台一样。如果那不可能,那么在执行命令后更新文本框也没问题。

示例:

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 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.Text = "Complete"
  $TextWindow.Name = $name
  $TextWindow.Multiline = $true
  $Form.Controls.Add($TextWindow) 
}

$path1 = "C:\SomePath\"
$path2 = "C:\SomePath\"

function call_Clean
{
  #Edit
  #Copy-Item "$Path1\Unit" -Destination $Path2 -Recurse -Force  
  Copy-Item "$Path1\Unit" -Destination $Path2 -Recurse -Force -Verbose
}

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



CreateFormButton 315 100 120 40 'Local Deploy' ${function:call_Clean}
CreateTextWindow 150 160 300 375 'TextBox'

1 个答案:

答案 0 :(得分:0)

Copy-Item不会返回任何值。复制项目后,您希望执行的操作为Test-Path。然后,您可以根据Test-Path

的True或False返回值在文本框中设置自定义值

示例:

Copy-Item C:\myfolder\myfile.txt -Destination C:\myfolder2\myfile.txt
$result = Test-Path C:\myfolder2\myfile.txt
if($result)
{
    $MyTextBox.Text = "Item moved"
}
Else
{
    $MyTextBox.Text = "Item failed to move"
}

您还可以添加-PassThru并将该响应分配给变量。