【发布时间】:2021-05-30 02:09:18
【问题描述】:
我直接在脚本块中的写入输出调用显示在运行空间输出中。但是我从该脚本块调用的 cmdlet 也会进行写入输出调用,但它们不会最终出现在运行空间输出中。
为了获得输出,我调用了 PowerShell.EndInvoke(Job)。
下面是一些示例代码,相同的代码刚刚从 powershell 运行 一些示例代码
非运行空间代码
Function Do-Output{
[CmdletBinding()]
Param(
[int]$n)
Process{
write-output "Output from inside Do-Output($($n))"
}
}
write-output "Output from before Do-Output(1) call"
Do-Output -n 1
write-output "Output from before Do-Output(2) call"
Do-Output -n 2
write-output "Output from before Do-Output(3) call"
Do-Output -n 3
write-output "Done"
非运行空间输出
Output from before Do-Output(1) call
Output from inside Do-Output(1)
Output from before Do-Output(2) call
Output from inside Do-Output(2)
Output from before Do-Output(3) call
Output from inside Do-Output(3)
Done
从运行空间运行的脚本块中的代码
Function Do-Output{
[CmdletBinding()]
Param(
[int]$n)
Process{
write-output "Output from inside Do-Output($($n))"
}
}
$Scriptblock = {
write-output "Output from before Do-Output(1) call"
Do-Output -n 1
write-output "Output from before Do-Output(2) call"
Do-Output -n 2
write-output "Output from before Do-Output(3) call"
Do-Output -n 3
write-output "Done"
}
从运行空间输出运行脚本块
Do-Output cmdlet 内部的写入输出调用均未显示在输出中。
Output from before Do-Output(1) call
Output from before Do-Output(2) call
Output from before Do-Output(3) call
Done
我知道 cmdlet 正在被调用,就好像我在其中抛出异常一样,该异常会显示出来。
【问题讨论】:
标签: powershell runspace