public PSObject Execute(string script)
        {
            using (Runspace runspace = CreateRunspace())
              {
            runspace.Open();

            using (var powerShell = System.Management.Automation.PowerShell.Create())
            {
              powerShell.Runspace = runspace;

              powerShell.AddScript(script);

              powerShell.Streams.Error.DataAdded += OnError;
              powerShell.Streams.Debug.DataAdded += OnDebug;
              powerShell.Streams.Warning.DataAdded += OnWarning;
              powerShell.Streams.Progress.DataAdded += OnProgress;
              powerShell.Streams.Verbose.DataAdded += OnVerbose;

              var outputCollection = new PSDataCollection<PSObject>();

              outputCollection.DataAdded += OnOutput;

              IAsyncResult invokeResult = powerShell.BeginInvoke<PSObject, PSObject>(null, outputCollection);

              powerShell.EndInvoke(invokeResult);

              if (_errorCount != 0)
              {
            throw new PowerShellScriptExecutionException(powerShell.Streams.Error);
              }

              return outputCollection.LastOrDefault();
            }
              }
        }
Example #2
0
        public PSObject Execute(string script)
        {
            using (Runspace runspace = CreateRunspace())
            {
                runspace.Open();

                using (var powerShell = System.Management.Automation.PowerShell.Create())
                {
                    powerShell.Runspace = runspace;

                    powerShell.AddScript(script);

                    powerShell.Streams.Error.DataAdded    += OnError;
                    powerShell.Streams.Debug.DataAdded    += OnDebug;
                    powerShell.Streams.Warning.DataAdded  += OnWarning;
                    powerShell.Streams.Progress.DataAdded += OnProgress;
                    powerShell.Streams.Verbose.DataAdded  += OnVerbose;

                    var outputCollection = new PSDataCollection <PSObject>();

                    outputCollection.DataAdded += OnOutput;

                    IAsyncResult invokeResult = powerShell.BeginInvoke <PSObject, PSObject>(null, outputCollection);

                    powerShell.EndInvoke(invokeResult);

                    if (_errorCount != 0)
                    {
                        throw new PowerShellScriptExecutionException(powerShell.Streams.Error);
                    }

                    return(outputCollection.LastOrDefault());
                }
            }
        }
        public dynamic Run(string script)
        {
            using (var runspace = CreateRunspace())
            {
                runspace.Open();

                using (var powerShell = PowerShell.Create())
                {
                    powerShell.Runspace = runspace;

                    var command = new Command(script);
                    if (_runnerConfiguration.Parameters != null)
                    {
                        foreach (var parameter in _runnerConfiguration.Parameters)
                        {
                            command.Parameters.Add(new CommandParameter(parameter.Key, parameter.Value));
                        }
                    }

                    powerShell.Commands.AddCommand(command);

                    powerShell.Streams.Error.DataAdded    += OnError;
                    powerShell.Streams.Debug.DataAdded    += OnDebug;
                    powerShell.Streams.Warning.DataAdded  += OnWarning;
                    powerShell.Streams.Progress.DataAdded += OnProgress;
                    powerShell.Streams.Verbose.DataAdded  += OnVerbose;

                    var outputCollection = new PSDataCollection <PSObject>();

                    outputCollection.DataAdded += OnObjectOutput;

                    var invokeResult = powerShell.BeginInvoke <PSObject, PSObject>(null, outputCollection);

                    powerShell.EndInvoke(invokeResult);

                    if (_errorCount != 0)
                    {
                        throw new RunnerException(powerShell.Streams.Error);
                    }

                    return(outputCollection.LastOrDefault());
                }
            }
        }