Parallelize powershell script execution

I have written the following script for SQL patching:

cls
function install-patch {
[cmdletbinding()]
param(
    $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?
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.

QlemoBatchelor, 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.
0
Eldo issacAuthor Commented:
Qlemo: Thanks for you suggestion. please ignore the above code as I have found another way to approach patching:

cls
$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.
0
QlemoBatchelor, 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.
0
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

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.
0
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:
cls
$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

0

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
QlemoBatchelor, 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.
0
Eldo issacAuthor Commented:
Perfect answer to what I required
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.