Exemple #1
0
        /// <summary> Executes shell command.</summary>
        /// <param name="runspace"> The runspace.</param>
        /// <param name="command"> The command.</param>
        /// <param name="useDomainController"> True - if domain controller should be used.</param>
        /// <param name="errors"> Errors list.</param>
        /// <returns> The result.</returns>
        internal Collection <PSObject> ExecuteShellCommand(Runspace runspace, Command command, bool useDomainController, out object[] errors)
        {
            HostedSolutionLog.LogStart("ExecuteShellCommand");
            var errorList = new List <object>();

            if (useDomainController)
            {
                var dc = new CommandParameter("DomainController", PrimaryDomainController);
                if (!command.Parameters.Contains(dc))
                {
                    command.Parameters.Add(dc);
                }
            }

            HostedSolutionLog.DebugCommand(command);
            Collection <PSObject> results;
            Pipeline pipeLine = runspace.CreatePipeline();

            using (pipeLine)
            {
                pipeLine.Commands.Add(command);
                results = pipeLine.Invoke();

                if (pipeLine.Error != null && pipeLine.Error.Count > 0)
                {
                    foreach (object item in pipeLine.Error.ReadToEnd())
                    {
                        errorList.Add(item);
                        string errorMessage = string.Format("Invoke error: {0}", item);
                        HostedSolutionLog.LogWarning(errorMessage);
                    }
                }
            }

            errors = errorList.ToArray();
            HostedSolutionLog.LogEnd("ExecuteShellCommand");

            return(results);
        }
Exemple #2
0
        internal Collection <PSObject> ExecuteShellCommand(Runspace runSpace, Command cmd, bool useDomainController, out object[] errors)
        {
            HostedSolutionLog.LogStart("ExecuteShellCommand");
            List <object> errorList = new List <object>();

            HostedSolutionLog.DebugCommand(cmd);
            Collection <PSObject> results = null;
            // Create a pipeline
            Pipeline pipeLine = runSpace.CreatePipeline();

            using (pipeLine)
            {
                // Add the command
                pipeLine.Commands.Add(cmd);
                // Execute the pipeline and save the objects returned.
                results = pipeLine.Invoke();

                // Log out any errors in the pipeline execution
                // NOTE: These errors are NOT thrown as exceptions!
                // Be sure to check this to ensure that no errors
                // happened while executing the command.
                if (pipeLine.Error != null && pipeLine.Error.Count > 0)
                {
                    foreach (object item in pipeLine.Error.ReadToEnd())
                    {
                        errorList.Add(item);
                        string errorMessage = string.Format("Invoke error: {0}", item);
                        HostedSolutionLog.LogWarning(errorMessage);
                    }
                }
            }
            pipeLine = null;
            errors   = errorList.ToArray();
            HostedSolutionLog.LogEnd("ExecuteShellCommand");
            return(results);
        }