Dicas e informações sobre Windows Server, Powershell

Enviar e-mails via PowerShell?!

Post rápido hoje pessoal!

Sabe aquela tarefa em poweshell que você criou, ela gera um arquivinho de log? Você o lê após a execução? Que tal o próprio script te enviar uma notificação via e-mail após a execução? Bacana, né?

Há algum tempo atrás um colega me pediu para criar um script que copiasse arquivos de um diretório para outro, eliminasse arquivos com data maior que 7 dias e após isso, enviar um e-mail contendo o nome dos arquivos eliminados. Esse script iria rodar todos os dias, então era muito importante que soubéssemos o comportamento dele.

A primeira parte foi muito simples, mas o envio de e-mail…. Nem sabia que tinha como rsrs

Pesquisei por algum tempo e encontrei o código abaixo!

Comentei ele para ficar mais simples para vocês =D

Agora é só adaptar o código em seus script! Muito simples!

#Aqui você irá inserir as credenciais da conta que irá enviar o e-mail
$Username = "";
$Password = "";
#Nesta linha você define o diretório que contém o seu anexo
#Caso não tenha interesse, basta a apagar
$path = "";

Function Send-ToEmail([string]$email, [string]$attachmentpath){

    $message = New-object Net.Mail.MailMessage;
    $message.From = "";
    $message.Subject = "ASSUNTO DO E-MAIL";
    $message.Body = "SUA MENSAGEM";
    #Parâmetros para envio do anexo, caso não for utilizar envio de anexo.
    #Basta apagar linhas abaixo
    $attachment = New-Object Net.Mail.Attachment($attachmentpath);

    #Configurações para envio de e-mail
    $smtp = new-object Net.Mail.SmtpClient("SMTP SERVER", "PORTA");
    $smtp.EnableSSL = $true;
    $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
    Write-host "E-mail enviado com Sucesso!" -ForegroundColor Cyan; 

Send-ToEmail  -Email "Destinatário" -Attachmentpath $path;
Imagem 1 – E-mail enviado =D


  1. April 8, 2019    

    Fantastic post however I was wanting to know if you could write a
    litte more on this topic? I’d be very thankful
    if you could elaborate a little bit more. Kudos!

  2. May 6, 2019    

    This is truly useful, thanks.

  3. August 10, 2019    

    I have to get across my admiration for your generosity supporting persons who really want assistance with the area. Your very own dedication to getting the message all over has been extraordinarily functional and has in every case enabled ladies like me to reach their endeavors. Your new helpful publication signifies a great deal to me and far more to my colleagues. With thanks; from each one of us.

  4. August 22, 2019    

    I am just writing to make you be aware of what a superb encounter my friend’s princess found reading your site. She picked up such a lot of details, most notably what it’s like to possess an incredible coaching nature to make other people just grasp chosen specialized matters. You really did more than my expected results. Thanks for delivering those good, safe, edifying and even easy guidance on the topic to Lizeth.

  5. September 9, 2019    

    Thank you a lot for giving everyone such a superb chance to discover important secrets from this web site. It can be very enjoyable and stuffed with a good time for me personally and my office friends to search your web site not less than three times every week to find out the latest tips you will have. Of course, I’m also actually fulfilled for the striking points you serve. Some 1 facts on this page are essentially the most impressive I’ve ever had.

  6. September 9, 2019    

    Hi , I do believe this is an excellent blog. I stumbled upon it on Yahoo , i will come back once again. Money and freedom is the best way to change, may you be rich and help other people.

  7. September 15, 2019    

    We absolutely love your blog and find many of your post’s to be just what I’m looking for. Would you offer guest writers to write content in your case? I wouldn’t mind writing a post or elaborating on many of the subjects you write about here. Again, awesome site!

  8. October 14, 2019    

    Hi there, just wanted to tell you, I enjoyed this blog post.
    It was helpful. Keep on posting!

  9. November 21, 2019    

    Wow that was strange. I just wrote an really long comment but after I clicked submit
    my comment didn’t show up. Grrrr… well I’m not writing all that
    over again. Regardless, just wanted to say fantastic blog!

    • December 12, 2019    

      Sorry about that!
      I use comments moderate, because of the spams…

  10. November 21, 2019    

    Thanks for the good writeup. It in truth was a amusement account it.
    Look complex to far added agreeable from you! However, how could we keep up a correspondence?

    • December 12, 2019    

      Send me a friend request on my Facebook, LinkedIn or Instagram.
      all of them are in my page.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.