Link to home
Start Free TrialLog in
Avatar of Eldo issac
Eldo issac

asked on

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.
Avatar of Qlemo
Qlemo
Flag of Germany image

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.
Avatar of Eldo issac
Eldo issac

ASKER

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.
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Eldo issac
Eldo issac

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Perfect answer to what I required