0

This is my first time using start-jobs, it is not working properly, am I using it wrong?

$dagName = "dag Name";
$dagObject = Get-DatabaseAvailabilityGroup -Identity $dagName;

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

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

Start-Job -ScriptBlock {foreach ($server in $servers) {$getExchangeServers += get-exchangeserver $server;}}
Start-Job -ScriptBlock {foreach ($server in $servers) {$getMailboxDatabases += Get-MailboxDatabase -Server $server;}}
Start-Job -ScriptBlock {foreach ($server in $servers) {$getMailboxDatabasesWithStatus += Get-MailboxDatabase -Server $server -Status;}}
Bbb
  • 517
  • 6
  • 27
  • Yes, you are. Jobs are like completely separate PowerShell instances. They have no clue what `$servers` is, nor can they access any of the `$getMailboxDatabases` variables. – TessellatingHeckler Jun 28 '17 at 18:34
  • Ok thank you. How would you correct the code? Could I do something like this? $results1 = Start-Job -ScriptBlock {foreach ($server in $servers) {$getExchangeServers += get-exchangeserver $server;}} -ArgumentList $servers; – Bbb Jun 28 '17 at 18:36
  • You would need a [`param` statement](https://stackoverflow.com/questions/16347214/pass-arguments-to-a-scriptblock-in-powershell) – BenH Jun 28 '17 at 18:44
  • No you can't do that; You need to pass parameters in with `-ArgumentList` and change the scriptblocks to take parameters, and use `$AllResults = Get-Job | Receive-Job -Wait -AutoRemoveJob` to get their output and remove the jobs so they don't hang around after. – TessellatingHeckler Jun 28 '17 at 18:45
  • Ok, so I've tried this: (but to no avail) . It tells me it can't find the job. Would you mind providing a working example so I can build off of that? $job1 = Start-Job -ScriptBlock {foreach ($server in $servers) {$getExchangeServers += get-exchangeserver $server;}} -ArgumentList $servers; $results1 = Get-Job -Name $job1 | Receive-Job -Wait -AutoRemoveJob – Bbb Jun 28 '17 at 19:02
  • "Try this" "I tried {something different} but it didn't work". hmm. `$server = 'a','b','c'; Start-Job -ScriptBlock { param($p) foreach ($server in $p) { $server } } -ArgumentList @(,$Servers); $AllResults = Get-Job | Receive-Job -Wait -AutoRemoveJob; $AllResults` is a working example to just put some "server names" into a job and get them back. – TessellatingHeckler Jun 28 '17 at 19:34
  • So I tried Start-Job -ScriptBlock { param($p) foreach ($server in $p) { $getExchangeServers += get-exchangeserver $server; } } -ArgumentList @(,$Servers); $AllResults = Get-Job | Receive-Job -Wait -AutoRemoveJob; And it gave me the following error. "The term 'get-exchangeserver' is not recognized as the name of a cmdlet" Does the script block also have to initialize a connection to the Exchange server? – Bbb Jun 28 '17 at 19:57
  • I used this: Start-Job -ScriptBlock { param($p) $session1 = new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri 'http://xxxxxx/powershell'; import-pssession $session1 -AllowClobber; $getExchangeServers = @(); foreach ($server in $p) { $getExchangeServers += get-exchangeserver $server; } } -ArgumentList @(,$Servers); $AllResults = Get-Job | Receive-Job -Wait -AutoRemoveJob; But $allResults equaled the name of the temporary Exchange powershell session that is established aka $session1 – Bbb Jun 28 '17 at 20:04
  • That's completely unhelpful Ansgar Wiechers, nicely done – Bbb Jun 29 '17 at 15:20

0 Answers0