Execute SQLs in Parallel from directory

  • Hello - 
    I am newbie to powershell. I am able to execute all the .SQL files from directory on to the server serially using below command. But, we want to execute all these files in parallel. CAn someone please help? 

    My code for serial execution -

    $PSScriptRoot = 'Y:\ODS Consolidation\ODSRefresh\test\'

    $ServerName = $env:COMPUTERNAME

    #Invoke-Sqlcmd -InputFile $DBListScript -ServerInstance $ServerName

    foreach ($f in Get-ChildItem -path $PSScriptRoot -Filter *.sql | sort-object -desc )
    {
    invoke-sqlcmd -InputFile $f.fullname -ServerInstance $ServerName
    }

  • Please find answer below if anyone is looking for it - 

    foreach ($f in Get-ChildItem -path $PSScriptRoot -Filter *.sql | sort-object -desc) {   Start-Job -ScriptBlock { invoke-sqlcmd -InputFile $args[0].fullname -ServerInstance $args[1] } -ArgumentList $f, $ServerName}

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply