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