为什么我的powershell多运行空间代码花费的时间与同步代码相同?

时间:2017-07-11 20:37:40

标签: multithreading powershell

要测试此代码,您必须填写前2个变量。我无法理解的是为什么顶级代码集(伪多线程)与底部代码集(标准同步处理)相同的处理时间?使用start-jobs我遇到了同样的事情。

我在这里遇到了什么错误的运行空间?

cls

$dagName = "XXXXXXX";
$connectionUri = "XXXXXXX";

Write-Host ([DateTime]::Now).ToString();
Write-Host "";

$time = New-Object system.Diagnostics.Stopwatch  
Start-Sleep 1
$time.Start() 
$system = $time.Elapsed.TotalMilliseconds

    $session = new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri;
    import-pssession $session -AllowClobber; 



set-adserversettings -viewentireforest $true;

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null;

if (($dagName -eq $null) -or ($dagName -eq ""))
{
    $dagName = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a dag name", "Dag");
}


$dagObject = Get-DatabaseAvailabilityGroup -Identity $dagName;

$servers = New-Object System.Collections.ArrayList;
foreach($server in $dagObject.Servers)
{
    $servers.Add($server.ToString()) | Out-Null;
}

$getExchangeServers = @();
$getMailboxDatabases = @();
$getMailboxDatabasesWithStatus = @();
$getWinEvents = @();


$maxThreads = 7

$runspacePool = [RunspaceFactory ]::CreateRunspacePool(1, $maxThreads)
$runspacePool.Open()

$scriptBlock1 = { param($p) $session1 = new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri; import-pssession $session1 -AllowClobber; $getExchangeServers = @(); foreach ($server in $p) { $getExchangeServers += get-exchangeserver $server; } return $getExchangeServers; }
$scriptBlock2 = { param($p) $session2 = new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri; import-pssession $session2 -AllowClobber; $getMailboxDatabases = @(); foreach ($server in $p) { $getMailboxDatabases += Get-MailboxDatabase -Server $server; } return $getMailboxDatabases; }
$scriptBlock3 = { param($p) $session3 = new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri; import-pssession $session3 -AllowClobber; $getMailboxDatabasesWithStatus = @(); foreach ($server in $p) { $getMailboxDatabasesWithStatus += Get-MailboxDatabase -Server $server -Status; } return $getMailboxDatabasesWithStatus; } 

$jobs = @()

$job1 = [powershell ]::Create().AddScript($scriptBlock1 ).AddArgument($servers)
$job1.RunspacePool = $runspacePool
$jobs += New-Object PSObject -Property @{
   Pipe = $job1
   Result = $job1.BeginInvoke()
}

$job2 = [powershell ]::Create().AddScript($scriptBlock2 ).AddArgument($servers)
$job2.RunspacePool = $runspacePool
$jobs += New-Object PSObject -Property @{
   Pipe = $job2
   Result = $job2.BeginInvoke()
}


$job3 = [powershell ]::Create().AddScript($scriptBlock3 ).AddArgument($servers)
$job3.RunspacePool = $runspacePool
$jobs += New-Object PSObject -Property @{
   Pipe = $job3
   Result = $job3.BeginInvoke()
}

Do {

   Write-Host $Jobs.Result.IsCompleted;
   Start-Sleep -Seconds 7

} While ( $Jobs.Result.IsCompleted -contains $false )
Write-Host "All jobs completed!"
Write-Host $Jobs.Result.IsCompleted;


Foreach ($Job in $Jobs)
{
    Write-Host $job.Pipe.EndInvoke($job.Result);
}


$time.Stop();
$totalTime = $time.Elapsed.TotalMilliseconds - $system;
[float]$runTimeSeconds = [math]::Round($totalTime/1000, 2);
[float]$runTimeMinutes = [math]::Round($totalTime/60000, 2);
Write-Host 
Write-Host "This function took " $runTimeSeconds.ToString() " seconds (or " $runTimeMinutes.ToString() " minutes) to run";








<# Code set 2#>

Write-Host ([DateTime]::Now).ToString();
Write-Host "";

$time = New-Object system.Diagnostics.Stopwatch  
Start-Sleep 1
$time.Start() 
$system = $time.Elapsed.TotalMilliseconds



    $session = new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri $connectionUri;
    import-pssession $session -AllowClobber; 



set-adserversettings -viewentireforest $true;

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null;

if (($dagName -eq $null) -or ($dagName -eq ""))
{
    $dagName = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a dag name", "Dag");
}


$dagObject = Get-DatabaseAvailabilityGroup -Identity $dagName;

$servers = New-Object System.Collections.ArrayList;
foreach($server in $dagObject.Servers)
{
    $servers.Add($server.ToString()) | Out-Null;
}

$getExchangeServers = @();
$getMailboxDatabases = @();
$getMailboxDatabasesWithStatus = @();
$getWinEvents = @();


foreach ($server in $servers) { $getExchangeServers += get-exchangeserver $server; } Write-Host $getExchangeServers;
foreach ($server in $servers) { $getMailboxDatabases += Get-MailboxDatabase -Server $server; } Write-Host $getMailboxDatabases;
foreach ($server in $servers) { $getMailboxDatabasesWithStatus += Get-MailboxDatabase -Server $server -Status; } Write-Host $getMailboxDatabasesWithStatus;

$time.Stop();
$totalTime = $time.Elapsed.TotalMilliseconds - $system;
[float]$runTimeSeconds = [math]::Round($totalTime/1000, 2);
[float]$runTimeMinutes = [math]::Round($totalTime/60000, 2);
Write-Host 
Write-Host "This second function took " $runTimeSeconds.ToString() " seconds (or " $runTimeMinutes.ToString() " minutes) to run";

0 个答案:

没有答案