private static void PreloadPowershellRunspace()
        {
            RunspaceGroup group = RunspaceGroup.Instance;

            using (RunspaceGroup.Token runspaceToken = group.Take())
            {
            }
        }
Esempio n. 2
0
        public PSDataCollection <PSObject> ExecutePowerShellCommand(Command command, string script)
        {
            try
            {
                TestabilityTrace.TraceSource.WriteInfo(
                    TraceSource,
                    "Executing powershell command \"{0}\" with parameters listed below",
                    command.ToString());

                foreach (var param in command.Parameters)
                {
                    TestabilityTrace.TraceSource.WriteInfo(
                        TraceSource,
                        "Parameter: {0} Value {1}",
                        param.Name,
                        param.Value);
                }

                var timer = new System.Diagnostics.Stopwatch();
                timer.Start();
                using (RunspaceGroup.Token runspaceToken = this.runspaceGroup.Take())
                    using (PowerShell shell = PowerShell.Create())
                    {
                        timer.Stop();
                        if (timer.Elapsed > TimeSpan.FromSeconds(1))
                        {
                            TestabilityTrace.TraceSource.WriteWarning(TraceSource, "ExecutePowerShellCommand took {0} to start: \"{1}\"", timer.Elapsed.ToString(), command.ToString());
                        }

                        // Initial action to be executed before any operation. In this case it should be a connect cluster
                        this.initialAction(runspaceToken.Runspace);

                        shell.Runspace = runspaceToken.Runspace;
                        if (!string.IsNullOrEmpty(script))
                        {
                            shell.Commands.AddScript(script);
                        }

                        shell.Commands.AddCommand(command);

                        IEnumerable <PSObject> results = shell.Invoke();
                        LogResults(command, results);
                        ThrowIfError(shell);

                        return(Disposable.Guard(() => new PSDataCollection <PSObject>(results), (d) => { }));
                    }
            }
            catch (Exception ex)
            {
                LogFailure(command, ex);
                throw;
            }
        }