miércoles, 8 de febrero de 2012

Send email from Powershell with attachment SQL Server

Este script puede ser útil para un SQL Server utilizado por Sharepoint

$emailFrom = "jvamvas@sqlserver-dba.com"
$emailTo = "jvamvas@sqlserver-dba.com"
$subject = "SQLServer-DBA.com: Powershell Function calling an SMTP server with attachment"
$body = "SQLServer-DBA.com : Send an email through SMTP in Powershell with attachment "
$smtpServer = "mysmtpserver"
$filePath = "C:\sql_server_health_sqlserver_jobs_2012-02.html"


Function sendEmail([string]$emailFrom, [string]$emailTo, [string]$subject,[string]$body,[string]$smtpServer,[string]$filePath)
{
#initate message
$email = New-Object System.Net.Mail.MailMessage
$email.From = $emailFrom
$email.To.Add($emailTo)
$email.Subject = $subject
$email.Body = $body
# initiate email attachment
$emailAttach = New-Object System.Net.Mail.Attachment $filePath
$email.Attachments.Add($emailAttach)
#initiate sending email
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($email)
}

#Call Function
sendEmail $emailFrom $emailTo $subject $body $smtpServer $filePath

 

Fuente:http://www.sqlserver-dba.com/

No hay comentarios:

Publicar un comentario