PSTestLib.PSCmdletBase.runScriptBlocks C# (CSharp) Method

runScriptBlocks() public method

public runScriptBlocks ( List scriptblocks, PSCmdletBase cmdlet, bool eventHandlers, object parameters ) : void
scriptblocks List
cmdlet PSCmdletBase
eventHandlers bool
parameters object
return void
        public void runScriptBlocks(
            List<ScriptBlock> scriptblocks,
            PSCmdletBase cmdlet,
            bool eventHandlers,
            object[] parameters)
        {
            
            try {

                if (scriptblocks != null && scriptblocks.Count > 0) {
                    
//                    cmdlet.WriteVerbose(cmdlet, "there are " + scriptblocks.Count + " scriptblock(s) to run");
                    
                    foreach (var sb in scriptblocks) {

                        if (sb != null) {

                            try {
                                if (eventHandlers) {

//                                    cmdlet.WriteVerbose(cmdlet, "run event handler");
                                    
                                    var runner = new runScriptBlock(runSBEvent);

                                    runner(sb, cmdlet.EventSource, cmdlet.EventArgs);

                                } else {
                                    
//                                    cmdlet.WriteVerbose(cmdlet, "run action with parameters");

                                    //runScriptBlock runner = new runScriptBlock(runSBAction);
                                    //runner(sb, cmdlet.EventSource, cmdlet.EventArgs);
                                    var runnerWithParams = new runScriptBlockWithParameters(runSBActionWithParams);
                                    
//                                    cmdlet.WriteVerbose(cmdlet, "the scriptblock runner has been created");
                                    
                                    // 20130606
                                    try {
                                        cmdlet.WriteVerbose(cmdlet, "listing parameters");
                                        if (null == parameters || 0 == parameters.Length)
                                            cmdlet.WriteVerbose(cmdlet, "there are no parameters");
                                        else
                                            foreach (var singleParam in parameters)
                                                cmdlet.WriteVerbose(cmdlet, singleParam);
                                    }
                                    catch (Exception eListParameters) {
                                        cmdlet.WriteVerbose(cmdlet, eListParameters.Message);
                                    }
                                    
                                    runnerWithParams(sb, parameters);
                                    
//                                    cmdlet.WriteVerbose(cmdlet, "the scriptblock runner has finished");
                                }
                            } catch (Exception) {

                                // 20130318
//                                ErrorRecord err = 
//                                    new ErrorRecord(
//                                        eInner,
//                                        "InvokeException",
//                                        ErrorCategory.OperationStopped,
//                                        sb);
//                                err.ErrorDetails = 
//                                    new ErrorDetails("Error in " +
//                                                     sb.ToString());
//                                WriteError(this, err, false);
                                
//                                this.WriteError(
//                                    this,
//                                    "Error in " +
//                                    sb.ToString() +
//                                    ". " +
//                                    eInner.Message,
//                                    "InvokeException",
//                                    ErrorCategory.OperationStopped,
//                                    // 20130318
//                                    //false);
//                                    true);
                                    
//                                    cmdlet.WriteVerbose(cmdlet, eInner.Message);
                                    throw new Exception("Failed to run scriptblock");
                            }
                        }
                    }
                }
            } catch (Exception) {
//                WriteError(this, 
//                           new ErrorRecord(eOuter, "runScriptBlocks", ErrorCategory.InvalidArgument, null),
//                           true);
                
//                this.WriteError(
//                    this,
//                    eOuter.Message,
//                    "runScriptBlocks",
//                    ErrorCategory.InvalidArgument,
//                    true);
                    
//                cmdlet.WriteVerbose(cmdlet, eOuter.Message);
                throw new Exception("Failed to run scriptblocks");
            }
        }
        //#endregion Invoke-UiaScript