1
我怎樣才能將第二個$scriptBlock
作爲第一個$scriptBlock
工作? 2的區別在於第一個從命令返回單個對象,第二個應該從多個命令返回一個對象數組?如何在Powershell運行空間線程中一起返回多個返回值?
要測試這個代碼,你只需要填入服務器名稱爲$servers
和$connectionUri
的Exchange PowerShell的超級鏈接和備用註釋掉第一$scriptBlock
(工作)和第二$scriptBlock
(沒有工作)之間。
感謝
cls
Write-Host "Aynchronous";
# Create session state
$stopWatch = [System.Diagnostics.Stopwatch]::StartNew();
$myString = "this is session state!";
$sessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault();
$sessionstate.Variables.Add((New-Object -TypeName System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList "myString" ,$myString, "example string"));
# Create runspace pool consisting of $numThreads runspaces
$minimumAmountOfThreads = 1;
$maximumAmountOfThreads= 15;
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, 15, $sessionState, $Host);
$RunspacePool.Open();
$servers = @("server1","server2","server3");
$connectionUri = "http://[something]/powershell";
$threads = @();
$Jobs = @();
$asynchronousThreadCount = 0;
foreach ($server in $servers)
{
$scriptBlock = "import-pssession (new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri) -AllowClobber; get-exchangeserver $server;"; # working
#$scriptBlock = "import-pssession (new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri) -AllowClobber; $returnValue = @(); $returnValue += Get-MailboxDatabase -Server $server; return $returnValue;"; # NOT working
#$scriptBlock = "import-module 'activedirectory'; Get-ADDomain"; # working
#$scriptBlock = "import-module 'activedirectory'; $returnValue = @(); $returnValue += Get-ADDomain; return $returnValue;" # NOT working
$asynchronousThreadCount ++;
$runspaceObject = [PSCustomObject] @{
Runspace = [PowerShell]::Create()
Invoker = $null
}
$runspaceObject.Runspace.RunSpacePool = $runspacePool;
$runspaceObject.Runspace.AddScript($scriptBlock) | Out-Null;
$runspaceObject.Runspace.AddArgument($c) | Out-Null;
$runspaceObject.Invoker = $runspaceObject.Runspace.BeginInvoke();
$threads += $runspaceObject;
$elapsed = $StopWatch.Elapsed;
Write-Host "A synchronous created thread $asynchronousThreadCount " $elapsed;
}
Write-Host $threads.Count;
Write-Host "";
Write-Host "Waiting.." -NoNewline;
Do {
Write-Host "." -NoNewline;
Start-Sleep -Seconds 1;
} While ($runspaceObject.Invoker.IsCompleted -contains $false);
$resultsAsynchronous = @();
foreach ($tr in $threads)
{
$resultsAsynchronous += $tr.Runspace.EndInvoke($tr.Invoker);
$tr.Runspace.Dispose();
}
$procCountMultiThread = $resultsAsynchronous.Count;
$runspacePool.Close();
$runspacePool.Dispose();
$elapsed = $StopWatch.Elapsed;
Write-Host "Multithread elapsed time: $elapsed";
Write-Host "Asynchronous return value count " $resultsAsynchronous.Count;
$resultsAsynchronous #| fl fqdn, AdminDisplayVersion;
請更具體說明「不工作」。你檢查過'$ tr.Runspace.Streams.Error'嗎? –
是的,我沒有錯誤。被標記爲不工作的唯一返回值是具有「模塊」,「名稱」和「導出的命令」的pssession哈希表。使用工作版本,您也會收到該散列表和cmdlet返回值。我試圖讓它可以嘗試代碼,如果你這樣做,你會明白我的意思。 – Jay
我沒有交換環境來測試 –