//This function allows use to use await - see http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx
 private static async Task<ExecutionResult> RunScriptfileAsync(PSScriptRunner Runner, string Scriptfile)
 {
     ExecutionResult result;
     result = await Runner.RunScriptFileAsync(Scriptfile);
     Console.WriteLine("Async finished!");
     return result;
 }
        //This function allows use to use await - see http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx
        private static async Task <ExecutionResult> RunScriptfileAsync(PSScriptRunner Runner, string Scriptfile)
        {
            ExecutionResult result;

            result = await Runner.RunScriptFileAsync(Scriptfile);

            Console.WriteLine("Async finished!");
            return(result);
        }
Example #3
0
        protected async Task RunInternalAsync(PSScriptRunner scriptRunner, string scriptDirectory, IProgress <RunnerProgressDetail> progress = null)
        {
            //Assign progress reporter and set it that it can be used after calling Report()
            _reporter = new ProgressReporter <RunnerProgressDetail>(progress, createNewInstanceAfterReport: true);

            //Report that we are about to start
            _reporter.Content.Action = RunnerAction.Starting;
            _reporter.Report();


            string[] allScripts = Directory.GetFiles(scriptDirectory, Xteq5EngineConstant.ScriptFilePattern);
            NaturalSort.Sort(allScripts);

            foreach (string scriptFilename in allScripts)
            {
                //Set the values we already have
                BaseRecord record = new BaseRecord();
                record.ScriptFilePath = scriptFilename;
                record.Name           = Path.GetFileNameWithoutExtension(scriptFilename);

                try
                {
                    //Report back status
                    ReportProgressScript(scriptFilename);

                    using (ExecutionResult execResult = await scriptRunner.RunScriptFileAsync(scriptFilename))
                    {
                        record.ProcessMessages = execResult.ToString();

                        if (execResult.Successful == false)
                        {
                            ProcessFailure(record);
                        }
                        else
                        {
                            //The script was run OK
                            record.ScriptSuccessful = true;


                            //Search the output
                            Hashtable table          = new Hashtable(); //new() is required or the compiler will complain about an unassigned local variable
                            bool      foundHashtable = false;

                            //Search the output collection for a hashtable
                            foreach (PSObject psobj in execResult.StreamOutput)
                            {
                                if (psobj.BaseObject != null && psobj.BaseObject is Hashtable)
                                {
                                    foundHashtable = true;
                                    table          = psobj.BaseObject as Hashtable;
                                    break;
                                }
                            }

                            if (foundHashtable == false)
                            {
                                //No hashtable was found within output stream. Add this to messages
                                record.AddLineToProcessMessages("No Hashtable was returned");

                                ProcessFailure(record);
                            }
                            else
                            {
                                ProcessHashtableOutputInternal(record, table);
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    //That didn't go well
                    record.ProcessMessages = exc.ToString();

                    //Let the actual implementation decide what to do next
                    ProcessFailure(record);
                }


                //MTH: End of processing with this file. Next one please!
            }


            //Report status that this entire run has finished
            _reporter.Content.Action = RunnerAction.Ended;
            _reporter.Report();
        }