public void HandleNotification(object sender, NotifyEventArgs eventArgs) { var config = ServiceRuntime.Instance.Configuration.ServiceConfiguration.SmtpConfiguration; if (config == null) { Logger.Error("SmtpNotifier: No configuration provided"); return; } if (eventArgs.UpsContext.UpsConfiguration.EnableEmailNotification == false) { Logger.Info( "SmtpNotifier: Notifications are disabled for UPS {0}", eventArgs.UpsContext.QualifiedName); return; } string[] notificationTypes = config.NotificationTypes.Split(','); if (!notificationTypes.Any( n => string.Equals( n, eventArgs.NotificationType.ToString(), StringComparison.OrdinalIgnoreCase))) { Logger.Info( "Notification type {0} not enabled for SMTP notification", eventArgs.NotificationType); return; } string messageSubject = string.Format( "UPS {0} has status {1}", eventArgs.UpsContext.Name, eventArgs.NotificationType); // TODO string messageBody = "TODO"; using (SmtpClient client = new SmtpClient()) { client.Host = config.Host; client.Port = config.Port; client.EnableSsl = config.EnableSSL; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Credentials = new NetworkCredential() { SecurePassword = config.Password, UserName = config.Username }; MailMessage message = new MailMessage( config.FromAddress, config.ToAddresses, messageSubject, messageBody) { BodyEncoding = Encoding.UTF8 }; client.Send(message); } }
public void HandleNotification(object sender, NotifyEventArgs eventArgs) { if (!File.Exists(this.scriptPath)) { Logger.NotificationScriptNotFound(this.scriptPath); Logger.Error( "The PowerShell notification script was not found at the path '{0}'", this.scriptPath); return; } string scriptContent = File.ReadAllText(this.scriptPath); Logger.Info( "PowerShellNotifier: Calling Receive-WingnutNotification with Type={0}, Ups={1}, ScriptPath={2}", eventArgs.NotificationType, eventArgs.UpsContext.QualifiedName, this.scriptPath); if (eventArgs.UpsContext.UpsConfiguration.EnablePowerShellNotification == false) { Logger.Info( "PowerShellNotifier: PowerShell notifications are disabled for UPS {0}", eventArgs.UpsContext.QualifiedName); return; } // Reference: https://docs.microsoft.com/en-us/archive/blogs/kebab/executing-powershell-scripts-from-c using (PowerShell pipeline = PowerShell.Create()) { pipeline.Streams.Error.DataAdded += (o, args) => Logger.Error( "PowerShellNotifier: {0}", pipeline.Streams.Error[args.Index].ToString()); pipeline.Streams.Warning.DataAdded += (o, args) => Logger.Warning( "PowerShellNotifier: {0}", pipeline.Streams.Warning[args.Index].ToString()); pipeline.Streams.Information.DataAdded += (o, args) => Logger.Info( "PowerShellNotifier: {0}", pipeline.Streams.Information[args.Index].ToString()); pipeline.Streams.Verbose.DataAdded += (o, args) => Logger.Debug( "PowerShellNotifier: {0}", pipeline.Streams.Verbose[args.Index].ToString()); pipeline.Streams.Debug.DataAdded += (o, args) => Logger.Debug( "PowerShellNotifier: {0}", pipeline.Streams.Debug[args.Index].ToString()); // Add the contents of the script to the pipeline to be invoked pipeline.AddScript(scriptContent); pipeline.Invoke(); pipeline.Commands.Clear(); var command = pipeline.AddCommand("Receive-WingnutNotification"); command.AddParameter("notificationType", eventArgs.NotificationType); command.AddParameter("ups", eventArgs.UpsContext.State); pipeline.Invoke(); } Logger.Info("PowerShellNotifier: Finished Receive-WingnutNotification"); }