powershell - 从工作中获取输出

标签 powershell

我很痛苦地无法从作业中获得任何输出-以下代码有什么问题?

$test = {
    DIR
}

$mjob = Start-Job -ScriptBlock {$test}
while (Get-Job -State Running){}
Receive-Job -Job $mjob -OutVariable $otest

Write-Host($otest)

最佳答案

当您使用-OutVariable时,仅提供变量名称,例如:

... -OutVariable otest

除非$otest恰好包含要将输出保存到的变量的名称。

其他一些建议。 $test代表一个脚本块,因此您无需在其周围放置{}。而不是等待使用while循环,只需使用Wait-Job例如:
$test = { get-childitem }
$job = Start-Job $test
Wait-Job $job
Receive-Job $job -OutVariable otest

$otest

关于powershell - 从工作中获取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12897823/

相关文章:

powershell - "("字符在 PowerShell 中不起作用

powershell - 以编程方式从安装程序中提取证书(.cer)

powershell - 如何编写对象的单个属性的值?

c# - 我可以指定一个 dll 以在 Powershell 中使用 New-Object 吗?

powershell - 在Powershell中等效于* Nix 'cut'命令?

sql-server - 使用powershell将dacpac转换为数据库对象的文件夹结构

powershell - 当 TLD 托管在 O365 上时,如何使用 POWERSHELL 创建 CNAME 记录?

ruby - rspec --init 不工作/`mkd ir':无效参数 - ./C: (Errno::EINVAL)

powershell - 从 Windows CMD 运行 PowerShell 命令的命令

powershell - 如何关闭控制台中的语法突出显示? Windows 10 2020