none
Something is crippling my output RRS feed

  • Question

  • I have a PowerShell script that I use to run multiple sub-scripts. I searched a way to handle the errors in the sub-scripts.

     Start.ps1
    <step1.ps1>
    <step2.ps1>
    All Scripts use a trap block to be able handover errors.
    To be able act on step that threw an error I came up with following solution:
    try {& .\installation\0_step0.ps1 -dotNetFrameworkExe $dotNetFrameworkExe} catch {$global:scripterror = $_.Exception.Message}

    This runs the script and show me the normal output, if there is an error in the script I break the current script and go back to the main script. With the catch argument I then can handle the exception message.

    The whole thing runs in a while with switch
    while ($global:scripterror -eq 0) {
    switch ($global:step) 
    { 
    1 {try ..}
    2 {try ..}
    }
    If ($global:scripterror -ne 0) {Scripthandling} 
    else {$global:step = $global:step +1}
    }

    In the end I call a function to either stop, restart or continue to the next step.
    function Scripthandling {
    
    $message = "Script threw an error: $global:scripterror `n Do you want to continue? stop or restart the step?"
    
    $result = $host.ui.PromptForChoice($title, $message, $options, 0)
    
    switch ($result)
        {
         0 {"You selected Stop. To restart the script at the failed step use <script> -step $global:step"
            Stop-Transcript
            exit}
            1 {"You chosed to restart step $global:step"
            $global:scripterror = 0
            }
            2 {"You selected to continue."
            $global:scripterror = 0
            $global:step = $global:step +1
            }
        }
    }

    The scripts are simplified, and I know they are quite basic. But it works OK this way.
    The issue I have with the script is that only the first script I execute returns the output as expected.

    Example: 

    PS F:\temp> .\testserver.ps1 -step 1 -ServerRole CD
    
    Transcript started, output file is F:\temp\20171019-011317.log
    
    Executing Step 1 - Check .net Framework
    
    PSChildName                   Version                       Release                       Product
    
    -----------                   -------                       -------                       -------
    
    v2.0.50727                    2.0.50727.4927
    
    v3.0                          3.0.30729.4926
    
    Windows Communication Foun... 3.0.4506.4926
    
    Windows Presentation Found... 3.0.6920.4902
    
    v3.5                          3.5.30729.4926
    
    Client                        4.7.02053                     460805
    
    Full                          4.7.02053                     460805
    
    Client                        4.0.0.0
    
    Confirm
    
    Is your .net Framework installation OK?
    
    [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):
    
    .Net Framework is OK, Continue with the installation
    
    Progressing with next step: 2
    
    Executing Step 2 - Install IIS Features
    
                                                    < here is not output of script step 2
    
    Progressing with next step: 3
    


    If I go directly to step 2, by defining at which step I'd like to start, I get the output (of step2, but not step3 and so on).
    PS F:\temp> .\testserver.ps1 -step 2 -ServerRole CD
    
    Transcript started, output file is F:\temp\20171019-011433.log
    
    Executing Step 2 - Install IIS Features
    
                                                    < This IST the Output of script step 2 I would expect
    
    Success Restart Needed Exit Code      Feature Result
    
    ------- -------------- ---------      --------------
    
    True    No             NoChangeNeeded {}
    
    True    No             NoChangeNeeded {}
    
    Progressing with next step: 3



    • Edited by matz-i Thursday, October 19, 2017 11:46 AM something was messing with my text
    Thursday, October 19, 2017 11:43 AM

Answers

  • I think I found the issue, It was actually caused by the script mentioned here: https://stackoverflow.com/questions/3487265/powershell-script-to-return-versions-of-net-framework-on-a-machine to find the .NetFramework installed.

    No idea why this happend - but with another script it works fine.


    • Edited by matz-i Thursday, October 19, 2017 12:02 PM
    • Marked as answer by matz-i Thursday, October 19, 2017 12:02 PM
    Thursday, October 19, 2017 12:01 PM