Ejemplo n.º 1
0
 private void ExecuteNow(PowerShellJob job)
 {
     if (!this.done)
     {
         job.Execute(runspacePool);
     }
 }
Ejemplo n.º 2
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(@"");
                            //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 != 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);
        }
Ejemplo n.º 3
0
 public void Execute(PowerShellJob job)
 {
     if (job != null)
     {
         if (job.ReOccuring)
         {
             ExecuteContinuous(job);
         }
         else
         {
             ExecuteNow(job);
         }
     }
 }
Ejemplo n.º 4
0
        private void ExecuteContinuous(PowerShellJob job)
        {
            Task.Run(() =>
            {
                do
                {
                    job.Done = job.Execute(runspacePool);
                    if (job.Done)
                    {
                        return;
                    }

                    Thread.Sleep(JobManager.delayInMilliSeconds);
                } while (!job.Done || this.done);
            });
        }
Ejemplo n.º 5
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(@"");
                            //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));
        }