Parallelize powershell script execution

I have written the following script for SQL patching:

function install-patch {
    $computername = $env:computername

    $installString = "cmd.exe /c D:\SQL_PATCH\SQLServer2012SP2-KB2958429-x64-ENU.exe /quiet /action=patch /allinstances /IAcceptSQLServerLicenseTerms"
    ([WMICLASS]"\\$computername\ROOT\CIMV2:win32_process").Create($installString) | out-null            

    while (@(Get-Process SQLServer2012SP2-KB2958429-x64-ENU -computername $computername -ErrorAction SilentlyContinue).Count -ne 0) 
        Start-Sleep 3
        Write-Host "Waiting for update removal to finish ..."
write-host "Completed the installation of patch" -foregroundcolor Green


$servers = gc 'D:\Abhi\Server.txt'

foreach ($server in $servers) {
    Write-host "`n`nCurrently on Server $server`n=================================="
    install-patch -ComputerName $server


Open in new window

My doubt here is to parallelize this script execution on all the servers mentioned in the text file. Meaning, as soon I start the execution of the script, this should initiate the patching activity on the servers simultaneously and also to track the progess on all the servers, as this script is doing now only for one server. Kindly help me on this.
Eldo issacAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The most simple approach is to invoke the patching process with
   start-job { install-patch -Compiutername $Server }
That alone will not show any progress indicator, though. Potential ways to manage that is to have another loop, checking the status of jobs and providing corresponding output.
Eldo issacAuthor Commented:
Qlemo: Thanks for you suggestion. please ignore the above code as I have found another way to approach patching:

$computers =  Get-Content D:\Abhi\Server.txt
foreach ($line in $computers)
    psexec \\$line -s -u Adminuser -p AdminPassword msiexec /i D:\SQL_PATCH\rsSharePoint.msi SKIPCA=1 /qb

Open in new window

Can you please give your edit w.r.t this code. Psexec is the fastest way i think.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
That way you don't have any progress info, and then it is better to work detached and with the full power of PsExec - without PowerShell:
psexec @D:\Abhi\Server.txt -d -s -u Adminuser -p AdminPassword msiexec /i D:\SQL_PATCH\rsSharePoint.msi SKIPCA=1 /qb

Open in new window

This will spawn the installation process as fast as possible.
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Eldo issacAuthor Commented:
Hello Qlemo,

Please tell me how can i track the progress of the above command on both the servers. Coz there are 50+ servers in question and need information about the progress on each of them. Please suggest an edit.
Eldo issacAuthor Commented:
This piece of code did it. It helps in executing the .exe simultaneously on all the servers as well as track their status:
$servers = Get-Content 'D:\Abhi\Server.txt'

$servers | ForEach-Object {$comp = $_

           Start-Job -ScriptBlock {psexec \\$input -s -u Adminuser -p AdminPassword C:\SQL_PATCH\SQLServer2008R2SP3-KB2979597-x64-ENU.exe /quiet /action=patch /allinstances /IAcceptSQLServerLicenseTerms} -InputObject $comp}

While (Get-Job -State Running) 
    Get-Job | Receive-Job
    #Start-Sleep 2 
    #Write-Host "Waiting for update removal to finish ..."

Remove-Job *

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Interesting. The basic idea is not all related to what I suggested in http:#a40819678 ?
I would not have used that approach, but it is better than keeping your original function and call it in start-job.
Your progress text is not showing machine related info, so you won't know the output association to a PC.
Eldo issacAuthor Commented:
Perfect answer to what I required
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.