Beispiel #1
0
        public static PowerShellJob MakePowerShellJob(Action <PowerShell> configure,
                                                      Action <dynamic> process, Action <Exception> errors = null)
        {
            var pShell = new PowerShellJob((runspacePool) =>
            {
                try
                {
                    using (var localRunspacePool = RunspaceFactory.CreateRunspacePool())
                    {
                        localRunspacePool.Open();
                        using (var engine = PowerShell.Create())
                        {
                            engine.RunspacePool = localRunspacePool;
                            engine.AddScript(@"Import-Module RemoteDesktop");
                            var importResult = engine.BeginInvoke();
                            var result       = engine.EndInvoke(importResult);
                            configure(engine);

                            var invokeResult = engine.BeginInvoke();
                            var commandIter  = engine.EndInvoke(invokeResult);

                            if (engine.Streams.Error != null &&
                                engine.Streams.Error.Count > 0)
                            {
                                ErrorHelper.WriteErrorToEventLog(PowerShellJob.GetErrorMessage(engine.Streams.Error));
                                throw new Exception(PowerShellJob.GetErrorMessage(engine.Streams.Error));
                            }

                            foreach (dynamic psObject in commandIter)
                            {
                                if (process != null)
                                {
                                    process(psObject);
                                }
                            }
                        }
                    }

                    return(true);
                }
                catch (Exception ex)
                {
                    ErrorHelper.WriteErrorToEventLog(ex.Message);
                    if (errors != null)
                    {
                        errors(ex);
                    }
                }

                return(false);
            });

            pShell.ReOccuring = false;
            return(pShell);
        }
Beispiel #2
0
        public static PowerShellJob MakePowerShellJob <T>(Action <PowerShell> Configure,
                                                          Action <dynamic, List <T> > NewFunc, Func <List <T>, bool> SetList)
        {
            return(new PowerShellJob((runspacePool) =>
            {
                try
                {
                    var curList = new List <T>();
                    using (var localRunspacePool = RunspaceFactory.CreateRunspacePool())
                    {
                        localRunspacePool.Open();
                        using (var engine = PowerShell.Create())
                        {
                            engine.RunspacePool = localRunspacePool;
                            engine.AddScript(@"Import-Module RemoteDesktop");
                            engine.Commands.AddCommand("Import-Module").AddArgument("ActiveDirectory");
                            var importResult = engine.BeginInvoke();
                            var result = engine.EndInvoke(importResult);
                            Configure(engine);
                            var invokeResult = engine.BeginInvoke();
                            var commandIter = engine.EndInvoke(invokeResult);

                            if (engine.Streams.Error.Count > 0)
                            {
                                ErrorHelper.WriteErrorToEventLog(PowerShellJob.GetErrorMessage(engine.Streams.Error));
                                throw new Exception(PowerShellJob.GetErrorMessage(engine.Streams.Error));
                            }

                            foreach (dynamic psObject in commandIter)
                            {
                                NewFunc(psObject, curList);
                            }
                        }
                    }

                    return SetList(curList);
                }
                catch (Exception ex)
                {
                    ErrorHelper.WriteErrorToEventLog(ex.Message);
                }

                return false;
            }, true));
        }