Пример #1
0
 public InvokeParallelTests()
 {
     _iss = CreateInitialSessionState();
     m_runspacePool = RunspaceFactory.CreateRunspacePool(_iss);
     m_runspacePool.SetMaxRunspaces(10);
     m_runspacePool.Open();
 }
Пример #2
0
        // ---------- CONSTRUCTORS ----------
        /// <summary>
        /// Inititalizes the PowerShellScript environment.
        /// </summary>
        /// <param name="log">An event log to log exceptions to. May be null if no exception logging is desired.</param>
        /// <param name="pool">A runspace pool for the scripting environment to use. May be null if a new Runspace pool is desired.</param>
        public PowerShellScript(EventLog.EventLog log, RunspacePool pool)
        {
            RunspacePool = null;
            Log = log;
            if (pool != null)
            {
                RunspacePool = pool;

                // Open the Runspace Pool so it's ready for use.
                if (RunspacePool.RunspacePoolStateInfo.State != RunspacePoolState.Opened)
                {
                    RunspacePool.Open();
                }
            }
            else
            {
                InitializeRunspacePool();
            }
        }
Пример #3
0
        /// <summary>
        /// Creates a new pool of Runspaces for this script to utilize.
        /// </summary>
        public void InitializeRunspacePool()
        {
            RunspacePool = RunspaceFactory.CreateRunspacePool();

            // Open the Runspace Pool so it's ready for use.
            RunspacePool.Open();
        }
Пример #4
0
        internal RestService(string serviceName, List<string> urls, List<RestCommand> commands, IEnumerable<string> modules)
            : base(serviceName, typeof(DynamicPowershell).Assembly)
        {
            // we have to give it at least one assembly, even if there isn't any services in it.
            // so I'm giving it a really small assembly

            _serviceName = serviceName;
            _activeCommands = commands;
            _listenOnUrls = urls;
            ReverseLookup.Clear();

            var ss = InitialSessionState.CreateDefault();
            ss.ImportPSModule(modules.ToArray());

            RunspacePool = RunspaceFactory.CreateRunspacePool(ss);
            RunspacePool.Open();
        }
Пример #5
0
        /// <summary>
        /// Runs a PowerShell command/script asynchronously.
        /// </summary>
        /// <param name="commandText">The text of the command to run.</param>
        /// <param name="pool">An open PowerShell Runspace pool this script will use to invoke its pipeline.</param>
        /// <param name="callback">The callback function used to process the results of the asynchronous run.</param>
        /// <param name="log">[Optional] The event log to log execptions to. May be null for no logging.</param>
        /// <param name="input">[Optional] A collection of strings representing command-line input sent to the script during execution.</param>
        /// <param name="stateValues">[Optional] A collection of named state values that should be passed through the invocation to the callback function.</param>
        /// <param name="parameterList">An array of key/value objects defining additional parameters to supply to the PowerShell script.</param>
        /// <returns>An WaitHandle object that can be used to determine when the scrip has completed execution. Null if an error occurred while processing the command / script.</returns>
        public static WaitHandle RunAsynchronously(string commandText, ref RunspacePool pool, ProcessResults callback, EventLog.EventLog log = null, PSDataCollection<string> input = null, Dictionary<String, Object> stateValues = null, params KeyValuePair<String, Object>[] parameterList)
        {
            try
            {
                // Create the script object.
                PS script = PS.Create();

                // Use the runspace pool supplied or create a new one if not supplied.
                if (pool == null)
                {
                    pool = RunspaceFactory.CreateRunspacePool(DEFAULT_MIN_RUNSPACES_IN_POOL, DEFAULT_MAX_RUNSPACES_IN_POOL, CreateRunspaceConnectionInfo());
                }

                // Verify that the pool is open, otherwise open it.
                if (pool.RunspacePoolStateInfo.State != RunspacePoolState.Opened)
                {
                    pool.Open();
                }

                // Add the runspace pool to the script object.
                script.RunspacePool = pool;

                // Create the PowerShell command object.
                Command command = new Command(commandText, true);

                // Add parameters to the command.
                if (parameterList != null)
                {
                    foreach (KeyValuePair<string, object> param in parameterList)
                    {
                        command.Parameters.Add(new CommandParameter(param.Key, param.Value));
                    }
                }

                // Add the command to the script object.
                script.Commands.AddCommand(command);

                // Initialize the script input object if nothing was supplied.
                if (input == null)
                {
                    input = new PSDataCollection<string>();
                }

                // Initialize the state object to maintain data across the invocation.
                PowerShellScriptState state = new PowerShellScriptState(script);
                // Add the callback function used to process the results of the script invocation.
                state.StateVariables.Add(PROCESS_RESULTS_CALLBACK, callback);
                // Add any state values passed into the method.
                if (stateValues != null)
                {
                    foreach (string key in stateValues.Keys)
                    {
                        state.StateVariables.Add(key, stateValues[key]);
                    }
                }

                // Invoke the command asyncronously.
                return (script.BeginInvoke(input, new PSInvocationSettings(), ProcessAsynchronousResults, state)).AsyncWaitHandle;
            }
            catch (Exception e)
            {
                LogException(e, log);
                return null;
            }
        }
Пример #6
0
        internal RestService(string serviceName, List<string> urls, List<RestCommand> commands, IEnumerable<string> modules) 
            : base(serviceName, GetActiveAssemblies().ToArray()) {
            _serviceName = serviceName;
            _activeCommands = commands;
            _listenOnUrls = urls;
            ReverseLookup.Clear();


            var ss = InitialSessionState.CreateDefault();
            ss.ImportPSModule(modules.ToArray());

            RunspacePool = RunspaceFactory.CreateRunspacePool(ss);
            RunspacePool.Open();
        }