Jobs and Variables

I am trying to get a report from a mailboxsearch from all servers from several dags each running as a job

I am close but need some help tuning it. The Jobs are being created but seem to be running in series.


$USADAGS = Get-MailboxServer | Where-Object { $_.DatabaseAvailabilityGroup -like ‘USA-*’ } | Select-Object $DatabaseAvailabilityGroup -Unique

Foreach ($Dag in $GDAGS){Start-Job -Name $Dag.DatabaseAvailabilityGroup -ScriptBlock {
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer -auto -AllowClobber

$USADAGS = Get-MailboxServer | Where-Object { $_.DatabaseAvailabilityGroup -like ‘USA-*’ } | Select-Object $DatabaseAvailabilityGroup -Unique

Foreach ($Dag in $USADAGS){
$StartDate = ((Get-Date).AddDays(-1))
$resultsize = 250000
$servers = Get-MailboxServer | Where-Object { $_.DatabaseAvailabilityGroup -eq $Dag.DatabaseAvailabilityGroup }
$AllServers = foreach ($server in $servers){
Get-Mailbox -server $server.DistinguishedName -ResultSize ‘unlimited’
ForEach ($mbx in $AllServers){try{
Search-MailboxAuditLog $mbx.alias -LogonTypes Admin, Delegate -ShowDetails -StartDate $StartDate -resultsize $resultsize -ea ‘stop’ |Select-Object PSComputername, Operation, LogonType, FolderPathName, ClientInfoString, ClientIPAddress, MailboxResolvedOwnerName, MailboxOwnerUPN, ItemSubject, LogonUserDisplayName, LogonUserSid, OriginatingServer, LastAccessed |Export-Csv “C:\exports</span>$($Dag.DatabaseAvailabilityGroup)Search.csv" -NoTypeInformation -Append
{ Write-Output "$($mbx) on $($Dag.DatabaseAvailabilityGroup) had the error $($
).Exception.Message” |
Out-File :\exports</span>$($Dag.DatabaseAvailabilityGroup)_error.log" -Append


You cannot trigger background jobs in Parallel using Start-Job. Start-Job creates a PowerShell.exe process for each job, so its not recommended to use it if you have more items. You can use PoshRSJob module instead. You can have a check with Invoke-Parallel for parallel executions. Both of these are available in PowerShellGallery.

So what would be the best method to do a mailbox audit search nightly against 200K + mailboxes across 60 DAGs
in a four hour window?

I have not read the code so I am not commenting on the code here more a possible answer to your question…Have you looked into Workflows? This may help you do what you want.

So, I would look into workflows (using the parameter -pspersist $true) and also foreach -parallel