Esempio n. 1
0
        public int RunPowerShellScript(string powerShellScriptFile, string[] arguments, bool runInNativeMode, bool hideArguments)
        {
            var powerShellArguments = new StringBuilder();

            powerShellArguments.AppendFormat("-ExecutionPolicy Unrestricted -Command \"& {{ ");
            powerShellArguments.AppendFormat(" . \\\"{0}\\\" ", powerShellScriptFile);
            foreach (var argument in arguments)
            {
                if (!string.IsNullOrEmpty(argument))
                {
                    powerShellArguments.AppendFormat("\"{0}\" ", argument);
                }
            }
            powerShellArguments.Append("; exit $LASTEXITCODE");
            powerShellArguments.Append("}\"");
            var directoryInfo = new FileInfo(powerShellScriptFile).Directory;

            if (directoryInfo != null)
            {
                var powerShellExe = GetPowerShellExe(runInNativeMode);
                _logger.InfoFormat("Executing: \"{0}\" {1}", powerShellExe, hideArguments? "***hidden***arguments***" : powerShellArguments.ToString());
                _cmdProcessor.Execute(powerShellExe, powerShellArguments.ToString(), directoryInfo.FullName, true);
                _logger.InfoFormat("Finished executing: \"{0}\" {1}", powerShellExe, hideArguments? "***hidden***arguments***" : powerShellArguments.ToString());
                return(_cmdProcessor.ExitCode);
            }
            _logger.ErrorFormat("Could not derive parent directory from PowerShell script file '{0}'", powerShellScriptFile);
            return(1);
        }
Esempio n. 2
0
        public bool TryProcess(CommandProcessorContext context, string[] args, out int exitCode)
        {
            var commandName = args[0].ToUpper();
            var commandArgs = args.Skip(1).ToArray();

            ICmdProcessor commandProcessor;

            if (!_processors.TryGetValue(commandName, out commandProcessor))
            {
                _log.Info("Unknown command: {0}.", commandName);
                if (_regCommandsProcessor != null)
                {
                    _regCommandsProcessor.Execute(context, new string[0]);
                }
                exitCode = 1;
                return(false);
            }

            int exitC         = 0;
            var executedEvent = new AutoResetEvent(false);

            ThreadPool.QueueUserWorkItem(_ =>
            {
                try
                {
                    var syntaxOk = commandProcessor.Execute(context, commandArgs);
                    if (!syntaxOk)
                    {
                        _log.Info("Usage of {0}:{1}{2}", commandName, Environment.NewLine, commandProcessor.Usage);
                    }
                    exitC = context.ExitCode;
                    executedEvent.Set();
                }
                catch (Exception exc)
                {
                    _log.ErrorException(exc, "Error while executing command {0}.", commandName);
                    exitC = -1;
                    executedEvent.Set();
                }
            });

            executedEvent.WaitOne(1000);
            context.WaitForCompletion();

            if (!string.IsNullOrWhiteSpace(context.Reason))
            {
                _log.Error("Error during execution of command: {0}.", context.Reason);
            }
            if (context.Error != null)
            {
                _log.ErrorException(context.Error, "Error during execution of command");

                var details = new StringBuilder();
                BuildFullException(context.Error, details);
                _log.Error("Details: {0}", details.ToString());
            }

            exitCode = exitC == 0 ? context.ExitCode : exitC;
            return(true);
        }