I have a script that sends monthly AD reports as an SMTP attachment. I’ve specified the username, and the password is stored locally in a text file on my desktop, but a Credentials box still pops up. The script works perfectly as long as I provide the username/password in the credential box.
(Get-Credential).password | ConvertFrom-SecureString > password.txt
$pw = Get-Content .\password.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential “Myuser”, $pw
Send-MailMessage -SmtpServer mail.servermail.com -Port 587 -Credential $cred -to “email@email.com” -attachments “C:\users\myuser\desktop\activedirectoryreport.xlsx” -from “email@email.com” -Subject “Monthly AD Report for $date” -body “Test for Send-MailMessage”>
Separate question - I want add the date to the Subject line using . I thought I could do $date = (Get-Date).AddDays(-1).ToString(‘MM-dd-yyyy’), and pass $date into the subject line, but that isn’t working. Any suggestions?