Beispiel #1
0
        /// <summary>
        /// Asynchronously executes a script using the current Context.
        /// </summary>
        /// <param name="script">Script to execute.</param>
        /// <returns>An InvocationResult object representing the results of the execution.</returns>
        public PSInvocationResult GetResult(string script)
        {
            if (script == null)
            {
                throw new ArgumentNullException("script");
            }

            if (_variableKeys != null)
            {
                throw new InvalidOperationException();
            }

#if PSLEGACY
            _variableKeys = LinqEmul.ToArray <object>(LinqEmul.Cast <object>(Variables.Keys));
#else
            _variableKeys = Variables.Keys.Cast <object>().ToArray();
#endif
            try
            {
                using (Runspace runspace = (Host == null) ?
                                           ((_configuration == null) ? RunspaceFactory.CreateRunspace() : RunspaceFactory.CreateRunspace(_configuration)) :
                                           ((_configuration == null) ? RunspaceFactory.CreateRunspace(Host) : RunspaceFactory.CreateRunspace(Host, _configuration)))
                {
                    if (ApartmentState.HasValue)
                    {
                        runspace.ApartmentState = ApartmentState.Value;
                    }
                    if (ThreadOptions.HasValue)
                    {
                        runspace.ThreadOptions = ThreadOptions.Value;
                    }
                    runspace.Open();

                    foreach (object key in _variableKeys)
                    {
                        string s = (key is string) ? key as string : key.ToString();
                        if (Variables[key] != null && String.Compare(s, "this", true) != 0 && String.Compare(s, "SynchronizedData", true) != 0)
                        {
                            runspace.SessionStateProxy.SetVariable(s, Variables[key]);
                        }
                    }
                    runspace.SessionStateProxy.SetVariable("this", This);
                    runspace.SessionStateProxy.SetVariable("SynchronizedData", SynchronizedData);
                    if (InitialLocation.Length > 0)
                    {
                        runspace.SessionStateProxy.Path.SetLocation(InitialLocation);
                    }

                    using (PowerShell powerShell = PowerShell.Create())
                    {
                        powerShell.Runspace = runspace;
                        return(new PSInvocationResult(script, this, powerShell, _variableKeys));
                    }
                }
            }
            finally { _variableKeys = null; }
        }
Beispiel #2
0
        /// <summary>
        /// Method which is intended for handling source <seealso cref="EventHandler{TEventArgs}"/> events.
        /// </summary>
        /// <param name="sender">Object which raised the event.</param>
        /// <param name="e">Information about the event.</param>
        public void EventHandler(object sender, TEventArgs e)
        {
#if PSLEGACY
            object[] variableKeys = LinqEmul.ToArray <object>(LinqEmul.Cast <object>(Variables.Keys));
#else
            object[] variableKeys = Variables.Keys.Cast <object>().ToArray();
#endif
            Dictionary <object, object> variables  = new Dictionary <object, object>();
            IDictionaryEnumerator       enumerator = Variables.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    variables.Add(enumerator.Key, enumerator.Value);
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            PSHost host = Host;
            RunspaceConfiguration configuration  = Configuration;
            ApartmentState?       apartmentState = ApartmentState;
            PSThreadOptions?      threadOptions  = ThreadOptions;
            string initialLocation = InitialLocation;

            using (Runspace runspace = (host == null) ?
                                       ((configuration == null) ? RunspaceFactory.CreateRunspace() : RunspaceFactory.CreateRunspace(configuration)) :
                                       ((configuration == null) ? RunspaceFactory.CreateRunspace(host) : RunspaceFactory.CreateRunspace(host, configuration)))
            {
                if (apartmentState.HasValue)
                {
                    runspace.ApartmentState = apartmentState.Value;
                }
                if (threadOptions.HasValue)
                {
                    runspace.ThreadOptions = threadOptions.Value;
                }
                runspace.Open();

                foreach (object key in variables.Keys)
                {
                    string s = (key is string) ? key as string : key.ToString();
                    if (Variables[key] != null && String.Compare(s, "this", true) != 0 && String.Compare(s, "SynchronizedData", true) != 0)
                    {
                        runspace.SessionStateProxy.SetVariable(s, Variables[key]);
                    }
                }

                runspace.SessionStateProxy.SetVariable("this", This);
                runspace.SessionStateProxy.SetVariable("SynchronizedData", SynchronizedData);

                if (initialLocation.Length > 0)
                {
                    runspace.SessionStateProxy.Path.SetLocation(initialLocation);
                }

                using (PowerShell powerShell = PowerShell.Create())
                {
                    powerShell.Runspace = runspace;
                    RaiseEventHandlerInvoked(sender, e, new PSInvocationResult(HandlerScript.ToString(), this, powerShell, variableKeys, sender, e));
                }
            }
        }