Scripts and results\output file handling

Options
mjones
mjones Member Posts: 138 ✭✭✭✭
edited December 2023 in Scripts

Hey everyone,

I am trying to figure out a way to get files resulting from a script run back to me, either by email or cloud service, preferably something that would be available via powershell without a lot of legwork. I can see this being very handy for a number of situations.

In my past life we used CW Automate (Labtech) and you could easily and natively pull a file back from a script to the ticketing system or a shared drive.

For Example, I have a script that will run Wiztree and outputs a CSV and a JPG file (I will be submitting this to the script repo when it's polished). I would like to be able to have the script send these files to me automatically.

Tagged:

Comments

  • tanderson
    tanderson Member Posts: 198 ✭✭✭✭
    Options

    @mjones

    PowerShell can natively send emails using the Send-MailMessage cmdlet. You can use this cmdlet to email the files to yourself.

    Example PowerShell Script:

    $PSEmailServer = "your-smtp-server.com"
    $from = "script@yourdomain.com"
    $to = "you@yourdomain.com"
    $subject = "Script Output"
    $body = "See attached files for script output."
    $attachments = @("C:\path\to\output.csv", "C:\path\to\output.jpg")
    Send-MailMessage -From $from -To $to -Subject $subject -Body $body -Attachments $attachments

  • mjones
    mjones Member Posts: 138 ✭✭✭✭
    Options

    This work well with Office 365, or something like SMTP2Go better ?

  • kim
    kim Member Posts: 113 ✭✭✭
    Options

    Thank you for sharing! This script looks great and I’ll have to try this in my environment.

  • nina
    nina Administrator Posts: 428 admin
    Options
  • mjones
    mjones Member Posts: 138 ✭✭✭✭
    Options

    Here is what I ended up using for SMTP2Go.

    I ended up having to beef up the script a bit for use with SSL and a password.

    I used a double Base64 encoded string to prevent prying eyes for the email password, though it should be noted that this is in no way "encrypted", it just looks like trash unless you know what you are looking for.

    I have a very small send limit set on my sub account in SMTP2Go, so not worried about it being used maliciously.

    $smtpServer = "mail.smtp2go.com"
    $from = "script@yourdomain.com"
    $to = "you@yourdomain.com"
    $subject = "Script Output"
    $body = "See attached files for script output."
    $port = 2525
    $encrypted = [Text.Encoding]::Utf8.GetString([Convert]::FromBase64String([Text.Encoding]::Utf8.GetString([Convert]::FromBase64String('Double_Base64_String_Goes_Here')))) $smtpPassword = ConvertTo-SecureString -String $encrypted -AsPlainText -Force
    $smtpCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $from, $smtpPassword $attachments = @("C:\File.csv", "C:\File.png")
    Send-MailMessage -SmtpServer $smtpServer -From $from -To $to -Subject $subject -Body $body -Attachments $attachments -UseSsl -Port $port -Credential $smtpCredential -Encoding "utf8"