Powershell运行空间:在从scriptblock';不显示为输出

Powershell运行空间:在从scriptblock';不显示为输出,powershell,runspace,Powershell,Runspace,直接在scriptblock中的写入输出调用显示在运行空间输出中。但是,我从该脚本块调用的cmdlet也会进行写输出调用,但它们不会在运行空间输出中结束 为了获得输出,我调用PowerShell.EndInvoke(Job) 下面是一些示例代码,与刚刚从powershell运行的代码相同 一些示例代码 非运行空间代码 Function Do-Output{ [CmdletBinding()] Param( [int]$n) Process{ write-out

直接在scriptblock中的写入输出调用显示在运行空间输出中。但是,我从该脚本块调用的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"
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"
}
非运行空间输出

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))"
    }
}
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"
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"
}
从运行空间输出运行脚本块

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
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正在被调用,就好像我在其中抛出了一个异常一样,该异常就会出现。

您必须在ScriptBlock中包含该函数。

我的解决方案是创建一个ScriptBlock包装器。这个脚本块包装器将在启动真正的脚本块之前启动一个转录本(我无法控制那个)。然后使用包装器脚本块中的Invoke命令启动真正的脚本块

这有几个好处。 然后我继续在模块中使用写主机。

它还捕获详细的输出。

这是否回答了您的问题?它位于我在ScriptBlock中导入的模块中,我知道函数正在被调用。好像我在其中抛出了一条消息,它确实显示为输出。该模块是否安装在目标计算机的本地?你的问题没有说函数在模块中。它是在脚本块上方声明的。你也可以分享例外情况吗?另外,请分享您正在使用的确切代码。。。如果你不这么做就没用了。更奇怪的行为。如果该函数返回一个值,而调用方没有将该值赋给变量,则所有写入输出都会显示在输出中。如果调用方确实将值分配给变量,则cmdlet中的写入输出不会显示在输出中。如果我将该函数从更大的模块(6000多行代码)移到自己的模块中,则cmdlet中的所有写入输出在所有场景中都能正常工作。但目标是让更大的模块在运行空间中工作。所有内容都在本地系统上运行。需要注意的是,我当前仅使用较大模块的1个函数。当我将1个函数放入自己的模块中,该函数返回一个值,调用方将其分配给一个值时,它也会显示非cmdlet内部写入输出。如果删除该变量赋值,所有写入输出将再次显示。我在上面错误地指出,它在所有情况下都有效。所以我很确定接下来该怎么办。我使用的是PSVersion5.1.19041.906