如何让第二个$scriptBlock
作为第一个$scriptBlock
工作? 2之间的区别是第一个从命令返回一个对象而第二个应该从多个命令返回一个对象数组?
要测试此代码,您只需填写$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;
(不工作)。
由于
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 100);
答案 0 :(得分:2)
在使用双引号定义脚本块时,您需要转义$
:
$scriptBlock = "import-pssession (new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri) -AllowClobber; $returnValue = @(); $returnValue += Get-MailboxDatabase -Server $server; return $returnValue;"; # NOT working
以字符串
结束import-pssession (new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri ) -AllowClobber; = @(); += Get-MailboxDatabase -Server ; return ;
使用`
:
$scriptBlock = "import-pssession (new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri) -AllowClobber; `$returnValue = @(); `$returnValue += Get-MailboxDatabase -Server $server; return `$returnValue;";