-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendMailNotification.ps1
More file actions
49 lines (35 loc) · 1.74 KB
/
sendMailNotification.ps1
File metadata and controls
49 lines (35 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Importar la clase System.Net.Mail
Add-Type -AssemblyName System.Net.Mail
# Cargar las credenciales desde el archivo seguro
$credentials = Import-Clixml -Path "/home/adminos/dev/github/prj-linuxpowershell/credenciales.xml"
# Configuración de variables para el script
$folderPath = "/home/adminos/Documents" # Ruta del directorio donde se buscarán los archivos
$expectedFileCount = 4 # Número esperado de archivos ZIP con el prefijo "g1_"
$daysDifference = -1 # Diferencia de días para obtener la fecha anterior (valor negativo para retroceder en el tiempo)
# Obtener la fecha actual y calcular la fecha objetivo basada en la diferencia de días
$currentDate = Get-Date
$targetDate = $currentDate.AddDays($daysDifference).ToString("yyyy-MM-dd")
# Filtrar archivos que tengan el prefijo "g1_" y pertenezcan a la misma fecha
$matchingFiles = Get-ChildItem -Path $folderPath `
-Filter "g1_*.zip" | Where-Object { $_.CreationTime.Date -eq $targetDate }
# Configuración del correo
$recipientEmail = "csigua@emov.gob.ec"
$senderEmail = "carlos.sigua@gmail.com"
$subject = "INFRA NOTIF 1492"
$body = "Este es por ahora el mensaje del correo con los archivos adjuntos encontrados. "+$matchingFiles
# Crear el objeto MailMessage
$mailMessage = New-Object System.Net.Mail.MailMessage
$mailMessage.From = $senderEmail
$mailMessage.To.Add($recipientEmail)
$mailMessage.Subject = $subject
$mailMessage.Body = $body
# Configurar el servidor SMTP
$smtpServer = "mail.emov.gob.ec"
$smtpPort = 587
$smtpCredentials = $credentials
# Crear el objeto SmtpClient
$smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer, $smtpPort)
$smtpClient.EnableSsl = $true
$smtpClient.Credentials = $smtpCredentials
# Enviar el correo electrónico
$smtpClient.Send($mailMessage)