Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell运行空间、输出和变量_Powershell - Fatal编程技术网

PowerShell运行空间、输出和变量

PowerShell运行空间、输出和变量,powershell,Powershell,我试图理解PowerShell中的运行空间。我知道PoshRSJob模块,但我想自己创建运行空间作业 这是我的代码,主要取自: 我脑子里有三个问题: 我是否能够在变量中返回一个值,并在完成作业后在脚本中进一步使用它 为什么我的$Data变量为空 到目前为止,在我的脚本中,创建以下输出的是什么?如果我$null像这样调用$Invoke=$PowerShell.BeginInvoke()>$null ,脚本不再正常工作,仍然创建此输出 命令:System.Management.Automatio

我试图理解PowerShell中的运行空间。我知道PoshRSJob模块,但我想自己创建运行空间作业

这是我的代码,主要取自:

我脑子里有三个问题:

  • 我是否能够在变量中返回一个值,并在完成作业后在脚本中进一步使用它
  • 为什么我的
    $Data
    变量为空
  • 到目前为止,在我的脚本中,创建以下输出的是什么?如果我
    $null
    像这样调用
    $Invoke=$PowerShell.BeginInvoke()>$null
    ,脚本不再正常工作,仍然创建此输出
命令:System.Management.Automation.PSCommand
流:System.Management.Automation.PSDataStreams
实例ID:3b91cfda-028e-4cec-9b6d-55bded5d9d3c
InvocationStateInfo:System.Management.Automation.PSInvocationStateInfo
IsNested:错误
HadErrors:错误
运行空间:
RunspacePool:System.Management.Automation.Runspaces.RunspacePool
所有者:False

历史字符串:

我不明白你的第一个问题


对于第二个问题,我认为这是因为您使用的是
$PowerShell.AddParameter($Computer)

请尝试使用
$PowerShell.AddArgument($Computer)
AddArgument
用于添加隐式(位置)绑定到参数的值
AddParameter
用于添加命名参数。只需要一个
字符串的
AddParameter
重载用于
[Switch]
参数

对于第三个问题,我认为是
$RunspacePool.Open()
提供了输出


当试图确定这些事情时,寻找没有左手赋值的行,尤其是方法调用的行;因此,您没有分配给变量的内容,因为这通常是将这些值放入输出流的方式。

$PowerShell.AddParameter($Computer)
->
[void]$PowerShell.AddArgument($Computer)
$Computer = "somename"
[runspacefactory]::CreateRunspacePool() > $null
$SessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$RunspacePool = [runspacefactory]::CreateRunspacePool(1,5)
$RunspacePool.Open()
1..2 | % {
    $PowerShell = [powershell]::Create()
    $PowerShell.RunspacePool = $RunspacePool
    $PowerShell.AddScript({
        param(
            $Computer
        )
        $Computer
    }) > $null
    $PowerShell.AddParameter($Computer)
    $Invoke = $PowerShell.BeginInvoke()
    while (!($Invoke.IsCompleted)) {sleep -Milliseconds 2}
    $Data = $PowerShell.EndInvoke($Invoke)
    Write-Host $Data -f Red
}