在wpf事件脚本中执行脚本时的写输出

时间:2017-10-06 11:31:53

标签: powershell

我有一个接受大量参数的脚本,而且相当大。我被要求构建一个用于执行脚本的GUI,因此用户可以轻松地执行脚本而不会出错。因此,我们的想法是GUI脚本收集所有参数,然后执行脚本。

“main”脚本使用write-outputstart-transcript来记录shell和文件。这也有效。 但是当我从GUI脚本执行脚本时,我没有得到每个输出到shell或日志。我认为这是因为write-output因为Write-Host确实有效,但我到处都听到人们说你不应该使用write-host(例如:https://youtu.be/SSJot1ycM70?t=24m1s) 。 那么我该如何让它发挥作用呢?

目前我用它来执行gui:

中的脚本
& $PSscriptroot\guitest2.ps1 -switchparam1:$localvar1 -switchparam2:$localvar2 -stringparam $localvar3

我试图通过cmd运行一个新的PowerShell实例,但我似乎没有让它工作。我也不知道如何以这种方式将参数“发送”到脚本中。

Invoke-Expression 'cmd /c powershell.exe -file C:\...\script.ps1 -paramters.

Invoke-Expression 'cmd /c powershell.exe -command {C:\..\script.ps1 -paramters..}

下面的两个脚本完全符合我的要求。但是当我在wpf事件中执行脚本时(即在按下开始按钮之后),第二个脚本只显示write-warningwrite-errorwrite-host输出,所以不显示{{ 1}}。

SCRIPT1:

write-output

SCRIPT2:

#gui script

$boolean = $true
$string = "test"

& $PSscriptroot\script2.ps1 -switch:$boolean -string $string

编辑: 做了一些测试。显然param( [switch]$switch, [string]$string ) Start-Transcript write-output "output" write-host "host" $a = "Variable" $a write-error "error" Write-Warning "warning" Write-Debug "debug" Stop-Transcript write-output事件中调用时完全不起作用。例如:

wpf

不确定这是否是一个错误

1 个答案:

答案 0 :(得分:0)

Write-Output将对象通过管道传递给另一个命令。基本上是声明

Write-Output <expression>

在很多情况下与

完全相同
<expression>

其他Write-* cmdlet的行为略有不同。在您的情况下,您有一个事件处理程序,无论如何都没有返回值。您的示例与

相同
"This message is not shown"

甚至

return "This message is not shown"

因为字符串是该脚本块的返回值的一部分,所以没有人看过。

如果您想在主机应用程序中输出,请使用Write-Host。这就是它的用途。 Write-Output是你在剧本中很少需要的东西。

相关问题