How to run two powershell functions at the same time in the same script?

时间:2018-03-09 19:02:40

标签: powershell dynamics-crm

I've done a lot of research on async calls within powershell and I haven't gotten anything to work correctly.

I need to find a way to have two separate database queries run at the same time. I have one onpremise connection and one online connection through crm dynamics.

function Get-OnPremiseDBQuery {
    $onPremResultsLocation = "..\results\onPremRecordsCountByEntity.txt"

        $onPremRecordSummary = @()
        foreach ($entityGroup in $entitiesArray) {
            $onPremRecordSummaryByGroup = Get-OnPremiseRecordsCountSummary -onPremConnection $onPremCRMConnection -entityNames $entityGroup
            $onPremRecordSummary += $onPremRecordSummaryByGroup

            Write-Host "ON PREM"
            Write-Host $onPremRecordSummaryByGroup | Format-Table -Property * -AutoSize | Out-String -Width 4096
        }
        Write-Output $onPremRecordSummary | Format-Table -Property * -AutoSize | Out-String -Width 4096 | Out-File $onPremResultsLocation -Append

}

function Get-OnlineDBQuery {
    $onlineResultsLocation = "..\results\onlineRecordsCountByEntity.txt"

        $onlineRecordSummary = @()
        foreach ($entityGroup in $entitiesArray) {
            $onlineRecordSummaryByGroup = Get-OnLineRecordsCountSummary -onlineConnection $onlineCRMConnection -entityNames $entityGroup
            $onlineRecordSummary += $onlineRecordSummaryByGroup

            Write-Host "ONLINE"
            Write-Host $onlineRecordSummaryByGroup | Format-Table -Property * -AutoSize | Out-String -Width 4096
        }
        Write-Output $onlineRecordSummary | Format-Table -Property * -AutoSize | Out-String -Width 4096 | Out-File $onlineResultsLocation -Append
    }

#Async Query Calls to onpremis/online DBs
##Looking to find a way to run
Get-OnPremiseDBQuery
and
Get-OnlineDBQuery
AT THE SAME TIME

Please help if anyone can!

1 个答案:

答案 0 :(得分:1)

This sort of effort is what PowerShell Jobs and RunSpaces are designed for.

As for...

I've done a lot of research on async calls within powershell and I haven't gotten anything to work correctly.

Are you saying, you have leverage the below, and none of the options worked for you?

You are also not defining what you mean by..

I haven't gotten anything to work correctly.

  • What's not working?
  • What response / errors are you getting?

The discussions:

How do I run my PowerShell scripts in parallel without using Jobs?

If I have a script that I need to run against multiple computers, or with multiple different arguments, how can I execute it in parallel, without having to incur the overhead of spawning a new PSJob with Start-Job?

https://serverfault.com/questions/626711/how-do-i-run-my-powershell-scripts-in-parallel-without-using-jobs

Parallel processing with PowerShell

Working in parallel

Whichever approach you end up taking you will be getting PowerShell to run tasks in parallel. That will often require you to have additional instances of PowerShell running. The resources on your admin machine – CPU, memory and network bandwidth – are finite. Keep those in mind so you don’t overload the machine and end up getting nothing back.

https://blogs.technet.microsoft.com/uktechnet/2016/06/20/parallel-processing-with-powershell

See also:

Invoke-Async - Allows you to run any cmdlet/function/scriptblock asynchronously

This has been tested in V2 and V3. You just provide the data set (-set) such as a list of servers of configuration settings etc. The param that the set belongs to (-setparam) such as ComputerName. You have the ability to provide any other params with a hash via -params (see examples.) The number of threads or jobs (concurrent instances) can be controlled via ThreadCount

https://gallery.technet.microsoft.com/scriptcenter/Invoke-Async-Allows-you-to-83b0c9f0