Example #1
0
        private static bool Compare(ExecutionParameters a, ExecutionParameters b)
        {
            if (object.ReferenceEquals(a, b))
            {
                return(true);
            }

            if ((object)a == null || (object)b == null)
            {
                return(false);
            }

            if (a.Exclusive != b.Exclusive)
            {
                return(false);
            }

            if (a.RunProfileName != null)
            {
                return(string.Equals(a.RunProfileName, b.RunProfileName, StringComparison.InvariantCultureIgnoreCase));
            }

            if (a.RunProfileType != b.RunProfileType)
            {
                return(false);
            }

            if (a.PartitionName != null)
            {
                return(string.Equals(a.PartitionName, b.PartitionName, StringComparison.InvariantCultureIgnoreCase));
            }

            return(a.PartitionID == b.PartitionID);
        }
        private void CheckTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.Log($"Timer elapsed. Next event at {DateTime.Now.Add(this.Interval)}");
            ExecutionParameters p = new ExecutionParameters();

            p.RunProfileName = this.RunProfileName;
            p.Exclusive      = this.Exclusive;
            p.RunImmediate   = this.RunImmediate;
            this.Fire(p);
        }
        private void CheckTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            ExecutionParameters p = new ExecutionParameters
            {
                RunProfileName = this.RunProfileName,
                Exclusive      = this.Exclusive,
                RunImmediate   = this.RunImmediate
            };

            this.Fire(p);

            if (!this.HasFired)
            {
                this.RevertToStandardInterval();
            }
        }
        public override bool Equals(object obj)
        {
            ExecutionParameters p2 = obj as ExecutionParameters;

            return(ExecutionParameters.Compare(this, p2));
        }
 public static bool operator !=(ExecutionParameters a, ExecutionParameters b)
 {
     return(!ExecutionParameters.Compare(a, b));
 }
Example #6
0
        private void Run()
        {
            try
            {
                Thread.CurrentThread.SetThreadName($"{this.DisplayName} on {this.ManagementAgentName}");

                this.powershell = PowerShell.Create();
                this.powershell.AddScript(System.IO.File.ReadAllText(this.ScriptPath));
                this.powershell.Invoke();

                if (this.powershell.Runspace.SessionStateProxy.InvokeCommand.GetCommand("Get-RunProfileToExecute", CommandTypes.All) == null)
                {
                    this.LogError($"The file '{this.ScriptPath}' did not contain a function called Get-RunProfileToExecute and will be ignored");
                    return;
                }

                while (!this.cancellationToken.Token.IsCancellationRequested)
                {
                    this.cancellationToken.Token.ThrowIfCancellationRequested();

                    this.powershell.ResetState();

                    this.powershell.AddCommand("Get-RunProfileToExecute");

                    Collection <PSObject> results;

                    try
                    {
                        results = this.powershell.Invoke();
                        this.cancellationToken.Token.ThrowIfCancellationRequested();
                        this.powershell.ThrowOnPipelineError();
                    }
                    catch (OperationCanceledException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        bool shouldTerminate = this.ExceptionBehaviour == ExecutionErrorBehaviour.Terminate;

                        this.LogError($"The PowerShell execution trigger '{this.DisplayName}' encountered an error", ex);

                        if (MessageSender.CanSendMail())
                        {
                            string messageContent = MessageBuilder.GetMessageBody(this.ManagementAgentName, this.Type, this.Description, DateTime.Now, shouldTerminate, ex);
                            MessageSender.SendMessage($"{this.ManagementAgentName}: {this.Type} trigger error", messageContent);
                        }

                        if (shouldTerminate)
                        {
                            this.Log("The PowerShell trigger has been terminated as specified by config");
                            break;
                        }
                        else
                        {
                            if (!this.cancellationToken.IsCancellationRequested)
                            {
                                this.cancellationToken.Token.WaitHandle.WaitOne(this.Interval);
                            }

                            continue;
                        }
                    }

                    foreach (PSObject result in results)
                    {
                        string runProfileName = result.BaseObject as string;

                        if (runProfileName != null)
                        {
                            this.Fire(runProfileName);
                            continue;
                        }

                        ExecutionParameters p = result.BaseObject as ExecutionParameters;

                        if (p == null)
                        {
                            continue;
                        }

                        this.Fire(p);
                    }

                    this.cancellationToken.Token.ThrowIfCancellationRequested();
                    this.cancellationToken.Token.WaitHandle.WaitOne(this.Interval);
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception ex)
            {
                this.LogError("The PowerShell execution trigger encountered an error and has been terminated", ex);

                if (MessageSender.CanSendMail())
                {
                    string messageContent = MessageBuilder.GetMessageBody(this.ManagementAgentName, this.Type, this.Description, DateTime.Now, true, ex);
                    MessageSender.SendMessage($"{this.ManagementAgentName}: {this.Type} trigger error", messageContent);
                }
            }
        }
        protected void Fire(ExecutionParameters p)
        {
            ExecutionTriggerEventHandler registeredHandlers = this.TriggerFired;

            registeredHandlers?.Invoke(this, new ExecutionTriggerEventArgs(p));
        }
        private void Run()
        {
            try
            {
                Thread.CurrentThread.SetThreadName($"{this.DisplayName} on {this.ManagementAgentName}");

                this.powershell = PowerShell.Create();
                this.powershell.AddScript(System.IO.File.ReadAllText(this.ScriptPath));
                this.powershell.Invoke();

                CommandInfo c = this.powershell.Runspace.SessionStateProxy.InvokeCommand.GetCommand("Get-RunProfileToExecute", CommandTypes.All);

                if (c == null)
                {
                    this.LogError($"The file '{this.ScriptPath}' did not contain a function called Get-RunProfileToExecute and will be ignored");
                    return;
                }

                bool cmdletRequiresCredentials = c.Parameters.ContainsKey("credentials");

                PSCredential creds = this.GetCredentialPackage();

                if (creds != null && !cmdletRequiresCredentials)
                {
                    this.LogError("Credentials were provided for the PowerShell script, but the Get-RunProfileToExecute function did not contain a 'credentials' parameter. See the wiki topic (https://github.com/lithnet/miis-autosync/wiki/Powershell-script-trigger) for more information");
                }

                while (!this.cancellationToken.Token.IsCancellationRequested)
                {
                    this.cancellationToken.Token.ThrowIfCancellationRequested();

                    this.powershell.ResetState();

                    if (cmdletRequiresCredentials)
                    {
                        this.powershell.AddCommand("Get-RunProfileToExecute").AddParameter("credentials", creds);
                    }
                    else
                    {
                        this.powershell.AddCommand("Get-RunProfileToExecute");
                    }

                    Collection <PSObject> results;

                    try
                    {
                        results = this.powershell.Invoke();
                        this.cancellationToken.Token.ThrowIfCancellationRequested();
                        this.powershell.ThrowOnPipelineError();
                    }
                    catch (OperationCanceledException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        bool shouldTerminate = this.ExceptionBehaviour == ExecutionErrorBehaviour.Terminate;

                        this.LogError($"The PowerShell execution trigger '{this.DisplayName}' encountered an error", ex);

                        if (MessageSender.CanSendMail())
                        {
                            string messageContent = MessageBuilder.GetMessageBody(this.ManagementAgentName, this.Type, this.Description, DateTime.Now, shouldTerminate, ex);
                            MessageSender.SendMessage($"{this.ManagementAgentName}: {this.Type} trigger error", messageContent);
                        }

                        if (shouldTerminate)
                        {
                            this.Log("The PowerShell trigger has been terminated as specified by config");
                            break;
                        }
                        else
                        {
                            if (!this.cancellationToken.IsCancellationRequested)
                            {
                                this.cancellationToken.Token.WaitHandle.WaitOne(this.Interval);
                            }

                            continue;
                        }
                    }

                    foreach (PSObject result in results)
                    {
                        string runProfileName = result.BaseObject as string;

                        if (runProfileName != null)
                        {
                            this.Fire(runProfileName);
                            continue;
                        }

                        ExecutionParameters p = result.BaseObject as ExecutionParameters;

                        if (p == null)
                        {
                            continue;
                        }

                        this.Fire(p);
                    }

                    this.cancellationToken.Token.ThrowIfCancellationRequested();
                    this.cancellationToken.Token.WaitHandle.WaitOne(this.Interval);
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception ex)
            {
                this.LogError("The PowerShell execution trigger encountered an error and has been terminated", ex);

                if (MessageSender.CanSendMail())
                {
                    string messageContent = MessageBuilder.GetMessageBody(this.ManagementAgentName, this.Type, this.Description, DateTime.Now, true, ex);
                    MessageSender.SendMessage($"{this.ManagementAgentName}: {this.Type} trigger error", messageContent);
                }
            }
        }
 public ExecutionTriggerEventArgs(MARunProfileType runProfileType, string partitionName)
 {
     this.Parameters = new ExecutionParameters(runProfileType, partitionName, false, false);
 }
 public ExecutionTriggerEventArgs(MARunProfileType runProfileType, Guid partitionID)
 {
     this.Parameters = new ExecutionParameters(runProfileType, partitionID);
 }
 public ExecutionTriggerEventArgs(string runProfileName, bool exclusive)
 {
     this.Parameters = new ExecutionParameters(runProfileName, exclusive);
 }
 public ExecutionTriggerEventArgs(ExecutionParameters p)
 {
     this.Parameters = p;
 }
 public ExecutionTriggerEventArgs()
 {
     this.Parameters = new ExecutionParameters();
 }