Scripts and results\output file handling

mjones
mjones Member Posts: 185 ✭✭✭✭
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: 279 ✭✭✭✭

    @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: 185 ✭✭✭✭

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

  • kim
    kim Member Posts: 113 ✭✭✭

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

  • nina
    nina Internal Posts: 428 ✭✭✭✭✭
  • mjones
    mjones Member Posts: 185 ✭✭✭✭

    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"

  • akaspar
    akaspar Member Posts: 9

    Doesn't Atera have it's own email system or relay built-in? Is there any way to leverage this?

  • mjones
    mjones Member Posts: 185 ✭✭✭✭

    You could create an Alert, but then it would need to be closed and when I last looked you weren't able to feed much onto it.

    Things may have changed since I last looked, and your needs might be different than mine.

  • mbudke
    mbudke Member Posts: 137 ✭✭✭

    You could use e.g. PowerAutomate and just trigger the workflow via your script. This would allow you to write the data into a file, send via email or feed it back via API into Atera.