none
PowerShell - Generated Log also Email as Attachment RRS feed

  • Question

  • I have a PowerShell script that generates a log file. Now I need the script to also email me the generated log file. I am lost on what I need to put behind the -attachments parameter. Below is my the bottom of my script that stats the log file on to the Send-Mailmessage.

        # Create Log File
        Write-Output "Creating Log File"
        $day = $today.Day
        $month = $today.Month
        $year = $today.Year
        $date = "$day-$month-$year"
        $logFileName = "$date-PasswordLog.csv"
        if(!($logPath.EndsWith("\")))
        {
           $logFile = $logPath + "\"
        }
        $logFile = $logFile + $logFileName
        Write-Output "Log Output: $logfile"
        $notifiedUsers | Export-CSV $logFile
    }
    $notifiedUsers | select UserName,Name,EmailAddress,PasswordSet,DaysToExpire,ExpiresOn | sort DaystoExpire | FT -autoSize


    $stop = [datetime]::Now
    $runTime = New-TimeSpan $start $stop
    Write-Output "Script Runtime: $runtime"
    Send-Mailmessage -smtpServer $smtpServer -from $from -to WindowsSupport@test.com -subject "Passwords Expiring" -attachments ???
    # End

    Thank You

    • Moved by Bill_Stewart Friday, July 7, 2017 7:14 PM Abandoned
    Monday, June 12, 2017 1:41 PM

All replies

  • You may want to replace "???" with "$logfile"

    There's no place like 127.0.0.1

    Monday, June 12, 2017 1:59 PM
  • I have -attachments"logFile"

    It sends me an email with no attachment but in the body of the email it says -attachments

    When I have it as -attachments "logfile"

    I get the error, Send-Mailmessage cannot validate argument on parameter 'Attachments' The argument is null or empty.

    Monday, June 12, 2017 2:09 PM
  • Ehm yeah ...

    Let's post the whole line for reference:

    # Bad
    Send-Mailmessage -smtpServer $smtpServer -from $from -to WindowsSupport@test.com -subject "Passwords Expiring" -attachments ???
    
    # Good
    Send-Mailmessage -smtpServer $smtpServer -from $from -to WindowsSupport@test.com -subject "Passwords Expiring" -attachments $logfile

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Monday, June 12, 2017 2:17 PM
  • Without the quotes around $logfile, I get the following error.

    Send-Mailmessage cannot validate argument on parameter 'Attachments' The argument is null or empty.

    Monday, June 12, 2017 2:26 PM
  • This is one reason to learn PowerShell:

    $logFileName = [datetime]::Today.ToString('MM-dd-yyyy')+'-PasswordLog.csv'
    $logFile = Join-Path $logpath $logFileName
    Write-Host "Log Output: $logfile"

    This will create a legitimate file pathname.

    Send-Mailmessage -smtpServer $smtpServer -from $from -to WindowsSupport@test.com -subject "Passwords Expiring" -attachments $logfile


    \_(ツ)_/



    • Edited by jrv Monday, June 12, 2017 3:54 PM
    Monday, June 12, 2017 3:52 PM
  • I got it to work. The earlier suggestion of the below line did the trick when I ran the script in Task Scheduler. Testing all morning, I was right clicking on the PowerShell file - Edit and testing that way. I am not sure why it would not work when testing it that way but it works fine when it runs in Task Scheduler.

    Send-Mailmessage -smtpServer $smtpServer -from $from -to WindowsSupport@test.com -subject "Passwords Expiring" -attachments "logFile"

    Monday, June 12, 2017 4:13 PM
  • You should not use quotes or unexpected things could happen.


    \_(ツ)_/

    Monday, June 12, 2017 4:21 PM