question

MrFlinstone-1451 avatar image
0 Votes"
MrFlinstone-1451 asked MrFlinstone-1451 edited

Powershell workflow - error control and ensuring the order of items in an array

Hi All.

I have a script below, the purpose of the script is as follows.

  1. Pass a file to the script containing a list of SQL instances.

  2. Pass an array of files to the script.

  3. Run each and every script on all the SQL instances defined in the file from step 1



    param(
    [string[]] $file_list,
    [string] $in_sql_server_instances_file_path
    )

      if (!(test-path $in_sql_server_instances_file_path)){
             write-output "The file does not exist"
             return 1
         }
        
         $file_server_list = get-content -path $in_sql_server_instances_file_path
         
         workflow execute-parallel
         {
             param(
                 [string []]
                 $sql_instances,
                 [string []]
                 $files_to_execute
             )
         
             foreach -parallel -throttlelimit 8 ($sql_instance in $sql_instances){
        
                 inlineScript{
                     $wf_sql_instance = $using:sql_instance
                     $wf_files_to_execute = $using:files_to_execute
        
                     foreach ($file_to_execute in $wf_files_to_execute){
                         Invoke-Sqlcmd -ServerInstance "$wf_sql_instance" -Database "tempdb" -InputFile $file_to_execute
                     }
                 }
             }
         }
         execute-parallel -sql_instances $file_server_list -files_to_execute $file_list
    


To execute the script.

  .\execute.ps1 -file_list 'C:\Scripts\a.sql','C:\Scripts\b.sql' -in_sql_server_instances_file_path 'C:\Scripts\sql_servers.txt'




I am trying to achieve the following.

  1. If a file fails, stop execution on the server but move onto the next server in the loop.

  2. Handle error control

  3. Deploy the files in the order specified in the -file_list array parameter.
    4.How can i be certain that a.sql will always run before b.sql ?


The file sql_servers.txt contains a list of SQL instances.

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers