-
-
Save asensionacher/636b436c6a189b621ee08e5ce5b11cf0 to your computer and use it in GitHub Desktop.
Enviar-Nominas.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dni | ||
---|---|---|
12345678X | empleado1@dominio.com | |
87654321Y | empleado2@dominio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Función de pedir carpeta | |
Function Get-Folder($initialDirectory) | |
{ | |
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null | |
$foldername = New-Object System.Windows.Forms.FolderBrowserDialog | |
$foldername.Description = "Select a folder" | |
$foldername.rootfolder = "MyComputer" | |
if($foldername.ShowDialog() -eq "OK") | |
{ | |
$folder += $foldername.SelectedPath | |
} | |
return $folder | |
} | |
#Función de pedir fichero | |
Function Get-FileName($initialDirectory) | |
{ | |
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | | |
Out-Null | |
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog | |
$OpenFileDialog.initialDirectory = $initialDirectory | |
$OpenFileDialog.filter = “CSV files (*.csv)| *.csv” | |
$OpenFileDialog.ShowDialog() | Out-Null | |
$OpenFileDialog.filename | |
} | |
#Pedimos las credenciales de envío de email | |
$mycredentials = Get-Credential -Message "Usuario y password desde donde se enviaran los emails" | |
#RELLENAR | |
$fromaddress = "usuario@dominio.com" | |
#RELLENAR | |
$Subject = "Envío nómina" | |
#RELLENAR | |
$smtpserver = "smtp.dominio.com" | |
#Obtenemos el fichero CSV | |
$csvFile = Get-FileName | |
#Convertimos el CSV en un array de objetos | |
$empleados = Import-Csv -Path $csvFile | |
#Obtenemos la carpeta donde estan las nóminas | |
$folderPath = Get-Folder | |
#Obtenemos los ficheros dentro de la carpeta | |
$files = ls $folderPath | |
################################## | |
#Para cada fichero | |
foreach ($file in $files) | |
{ | |
#Teniendo en cuenta que el nombre del fichero es 12345678X.pdf donde 12345678X es el DNI | |
#Obtenemos el núemero de DNI | |
$DNI = $file.Name.Split('.')[0] | |
#Buscamos el DNI en el CSV y obtenemos su email | |
$toaddress = ($empleados | where {$_.DNI -like $DNI}).email | |
#Añadimos el pdf del attachment | |
$attachment = $file.FullName | |
#Enviamos el email | |
Send-MailMessage -smtpServer smtp.office365.com -Credential $mycredentials ` | |
-from $fromaddress -to $toaddress -subject $subject ` | |
-attachment $attachment -UseSsl -Body "Aquí tienes tu nómina" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment