Simultaneous PowerShell script execution

258 views Asked by At

I have written the following script for SQL 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
}

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.

1

There are 1 answers

0
Lilly123 On BEST ANSWER

Thanks Ansgar Wiechers. 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 *