コード例 #1
0
        public static OutputQueue RunInPowerShell(string command, bool spCmdlet)
        {
            var output = new OutputQueue();

            output.Add(EnsurePowerShellExecutable());

            using (var runspace = RunspaceFactory.CreateRunspace())
            {
                runspace.Open();
                var ps = runspace.CreatePipeline();

                var assembly     = Path.Combine(ExecutionPath(), "NewsGator.Install.Common.dll");
                var commandToRun = string.Format(CultureInfo.InvariantCulture, FormatLoad, assembly);
                commandToRun += AddSharePointSnapIn;
                commandToRun += string.Format(CultureInfo.InvariantCulture, FormatCD, ExecutionPath());
                if (spCmdlet)
                {
                    commandToRun += command + "; ";
                }
                else
                {
                    commandToRun += string.Format(CultureInfo.InvariantCulture, FormatCommand, command);
                    commandToRun += OutputSubCmd;
                }

#if DEBUG
                output.Add(commandToRun);
#endif

                var encoded = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(commandToRun));
                ps.Commands.AddScript(string.Format(CultureInfo.InvariantCulture, FormatLoad, assembly));
                ps.Commands.AddScript(string.Format(CultureInfo.InvariantCulture, FormatCD, ExecutionPath()));
                ps.Commands.AddScript(string.Format(CultureInfo.InvariantCulture, FormatCommand, string.Format(CultureInfo.InvariantCulture, FormatNGPS, PowerShellVersion(), encoded)));
                ps.Commands.AddScript(OutputCmd);

#if DEBUG
                foreach (var cmd in ps.Commands)
                {
                    if (cmd.IsScript)
                    {
                        output.Add(cmd.CommandText);
                    }
                }
#endif

                var cmdOutput         = ps.Invoke();
                var outputQueueString = string.Empty;

#if DEBUG
                var debugOutput = string.Empty;
                if (cmdOutput != null)
                {
                    for (var i = 0; i < cmdOutput.Count; i++)
                    {
                        if (cmdOutput[i] != null)
                        {
                            var outputString = cmdOutput[i].BaseObject.ToString().Trim();
                            if (!string.IsNullOrEmpty(outputString))
                            {
                                debugOutput += outputString;
                            }
                        }
                    }
                }

                output.Add(string.Format(CultureInfo.CurrentUICulture, UserDisplay.PSOutput, command, debugOutput));
#endif

                if (spCmdlet)
                {
                    if (cmdOutput != null)
                    {
                        for (var i = 0; i < cmdOutput.Count; i++)
                        {
                            if (cmdOutput[i] != null)
                            {
                                var outputString = cmdOutput[i].BaseObject.ToString().Trim();
                                if (!string.IsNullOrEmpty(outputString))
                                {
                                    outputQueueString += outputString;
                                }
                            }
                        }
                    }

                    output.Add(string.Format(CultureInfo.CurrentUICulture, UserDisplay.PSOutput, command, outputQueueString));
                }
                else
                {
                    var tempFilePath = Path.Combine(ExecutionPath(), "NGPSOutput.temp");

                    if (File.Exists(tempFilePath))
                    {
                        var tempContent = File.ReadAllText(tempFilePath, UTF8Encoding.UTF8);
                        try
                        {
                            var queue = new OutputQueue();
                            queue.Add(queue.Deserialize(tempContent));
                            output.Add(queue);
                        }
                        catch (Exception exception)
                        {
                            exception.Data.Add("NGDescription", tempContent);
                            output.Add(string.Format(CultureInfo.CurrentUICulture, NewsGator.Install.Resources.Exceptions.DeserializationError, exception.Message), OutputType.Error, exception.ToString(), exception);
                        }
                        File.Delete(tempFilePath);
                    }
                    else
                    {
                        var getRemaining = false;

                        if (cmdOutput != null)
                        {
                            for (var i = 0; i < cmdOutput.Count; i++)
                            {
                                if (cmdOutput[i] != null)
                                {
                                    if (!getRemaining)
                                    {
                                        if (cmdOutput[i].BaseObject.ToString().IndexOf(Prefix, StringComparison.OrdinalIgnoreCase) > -1)
                                        {
                                            outputQueueString = cmdOutput[i].BaseObject.ToString().Trim();
                                            getRemaining      = true;
                                        }
                                    }
                                    else
                                    {
                                        outputQueueString += cmdOutput[i].BaseObject.ToString().Trim();
                                    }
                                }
                            }
                        }

                        if (!string.IsNullOrEmpty(outputQueueString))
                        {
                            try
                            {
                                var queue = new OutputQueue();
                                queue.DecodeAndAdd(outputQueueString);
                                output.Add(queue);
                            }
                            catch (Exception exception)
                            {
                                output.Add(string.Format(CultureInfo.CurrentUICulture, NewsGator.Install.Resources.Exceptions.DeserializationError, exception.Message), OutputType.Error, exception.ToString(), exception);
                            }
                        }

                        if (!getRemaining)
                        {
                            var unexpectedOutput = string.Empty;
                            for (var i = 0; i < cmdOutput.Count; i++)
                            {
                                if (cmdOutput[i] != null)
                                {
                                    if (i == 0)
                                    {
                                        unexpectedOutput = string.Format(CultureInfo.CurrentUICulture, NewsGator.Install.Resources.Exceptions.RunspaceUnexpectedOutput, cmdOutput[i].BaseObject.ToString().Trim());
                                    }
                                    else
                                    {
                                        unexpectedOutput += Environment.NewLine + cmdOutput[i].BaseObject.ToString().Trim();
                                    }
                                }
                            }

                            if (!string.IsNullOrEmpty(unexpectedOutput))
                            {
                                output.Add(unexpectedOutput, OutputType.Warning);
                            }
                        }
                    }
                }

#if DEBUG
                output.Add(string.Format(CultureInfo.CurrentUICulture, UserDisplay.PSOutput, command, outputQueueString));
#endif
            }

            return(output);
        }