none
Parallel scripts execution

    Question

  • Dear All,

    Is there someone to bring me some light in what i'm trying to do:

    launch 3 powershell scripts in parallel through a master script, and get the correct result of the children scripts

    (without using runspaces, or dedicated imported module).

    Running 3 scripts from a master one -> ok i know how to do that, but my problem is how to  get logs of the children scripts.

    Every children script begin by 'Start-Transdcript' to collect their own logs.

    Let's say:

    master script -> launch script child 1, child2 , child3

    script child 1 -> launch write-host "tralalala"

    script child 2 -> launch collect some vmware informations

    script child 3 -> launch" test-connectik -computername tralala (instead of test-connection)

                       -> test-connectik doesn't exist and the script will end in a terminating error (that great for me)

    My master script will return the below result

    Name   State    
    ----   -----   
    tralala   Completed

    Ping   Failed  

    vmware Completed

      -> and that is fine and that is what I want.

    BUT

    the transcript of my script child 2 will be empty, because the script stops execution on a terminating error, and i will never know what went wrong in the script child 2 as the transcript output text file is empty.

    NB: of courses if I set $Erroractionpreference= "continue", the transcript will ok and full of informations but the result of my master script will be

    Name   State    
    ----   -----   
    tralala   Completed

    Ping   Completed (instead of Fail) 

    vmware Completed

    So guys any idea how to deal with that ?

    Thank you for your ideas

    RS

    Wednesday, May 22, 2019 1:02 PM

Answers

  • Hi,

    Instead of setting the $ErrorActionPreference to Continue for the whole script try using error handling with the problematic script and send that output to a file. Then if needed from the master script you can check that file for any logged errors (Get-Content) and do something with them.

    $error_logfile = "C:\Scripts\ErrorLog.txt"
    Try {
            Get-Content C:\test.txt -ErrorAction Stop
    }
    Catch {
            $_.Exception.Message | Out-File $error_logfile -Append
            $error.Clear()
    
    }


    v/r LikeToCode....Mark the best replies as answers.

    • Marked as answer by doublevue Friday, May 24, 2019 9:02 AM
    Thursday, May 23, 2019 6:19 PM
    Moderator

All replies

  • Hi,

    Instead of setting the $ErrorActionPreference to Continue for the whole script try using error handling with the problematic script and send that output to a file. Then if needed from the master script you can check that file for any logged errors (Get-Content) and do something with them.

    $error_logfile = "C:\Scripts\ErrorLog.txt"
    Try {
            Get-Content C:\test.txt -ErrorAction Stop
    }
    Catch {
            $_.Exception.Message | Out-File $error_logfile -Append
            $error.Clear()
    
    }


    v/r LikeToCode....Mark the best replies as answers.

    • Marked as answer by doublevue Friday, May 24, 2019 9:02 AM
    Thursday, May 23, 2019 6:19 PM
    Moderator
  • Hi LikeToCode,

    Thank you for your answer, which was the "of course" one which i didn't think about (damned) !

    So I added a try/catch in each child script, and

    I just add a little trick:

    $error_logfile = "C:\Scripts\ErrorLog.txt" Try { Get-Content C:\test.txt -ErrorAction Stop } Catch { $_.Exception.Message | Out-File $error_logfile -Append $error.Clear()

    stop-transcript NON EXISTING command (test-connectik for example) }

    I add a non exisiting command at the end of the Catch, so that :

    - i catch all the previous errors in the logs, transcript stop and in my master script, the child script is in failed status (that's what I want):

    Name   State    
    ----   -----   
    tralala   Completed

    Ping   Failed  

    vmware Completed

    Without this little trick, the catch would have ended correctly (providing logs), but my master script would have return:

    Name   State    
    ----   -----   
    tralala   Completed

    Ping   Completed (instead of Fail) 

    vmware Completed

    Anyway, thank you for having waked up my brain :-).

    I mark this topic as answered.

    Friday, May 24, 2019 9:02 AM