Esempio n. 1
0
        internal Collection <PSObject> ExecuteShellCommand(Runspace runSpace, Command cmd, out object[] errors)
        {
            ExchangeLog.LogStart("ExecuteShellCommand");
            List <object> errorList = new List <object>();

            ExchangeLog.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.ToString());
                        ExchangeLog.LogWarning(errorMessage);
                    }

                    throw new Exception(errorList[0].ToString());
                }
            }
            pipeLine = null;
            errors   = errorList.ToArray();
            ExchangeLog.LogEnd("ExecuteShellCommand");
            return(results);
        }