Send-MailMessage Access to path is denied - powershell

I am trying to send files by email using powershell. I am able to pop up a prompt for password for credential to run. But the send-mailmessage come back saying the path is denied. Also is there a way to include the password in the script to bypass entering the password so that I can run this through task scheduler on the server?
Send-MailMessage -From 'David Brierton <Davidb#test.com>' -To 'David Brierton <Davidb#test.com>' -Subject "Website deployment" -Body "See attached file" -Attachments #("\\server\d$\Mdrive\test\test\Book1.csv", "\\server\d$\Mdrive\test\test\ExampleBook1.csv") -SmtpServer test.test.test -Credential DomainName\User

Remove the # and the bracket in the -attachement and try with only your email
Send-MailMessage -From "Davidb#test.com" -To "Davidb#test.com" -Subject "Website deployment" -Body "See attached file" -Attachments "\\server\d$\Mdrive\test\test\Book1.csv", "\\server\d$\Mdrive\test\test\ExampleBook1.csv" -SmtpServer test.test.test -Credential DomainName\User

Related

Trying to send email (gmail) using PowerShell

I'm trying to send an email using PowerShell. For testing purposes, I'm trying to send the email to myself first. I've looked at a couple online references, and here is what I have so far:
$from = my#email.com
$to = my#email.com
$subject = "Test"
$body = "Test"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -from $from -to $to -Subject $subject -Body $Body -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credentials (Get-Credential)
Once ran, I get the prompt to enter credentials. After entering credentials, I get an error reading:
Send-MailMessage : A parameter cannot be found that matches parameter name 'Credentials'.
So I'm led to believe that the error is thrown when I enter in my credentials. When using this feature and the username is the email, am I supposed to type in my entire email, or just the identifier prior to the "#....com"? Either way, it doesn't seem to like either and I'm getting an error all the same. Am I using this correctly?

run the powershell script so it checks and mails the faults

I am running an office 365 database of a lot of members, each in one of a bunch of groups. I used to check the groups and users by hand but there are too many members now to be able to do that efficiently. now I have found a powershell script somewhere on github but it looks as if it just does checking and displaying in powershell.
Is there a way to have a script compare the users and groups and if a user is in a group he does not belong in send a mail to the admin mail in the domain?
if this makes any sense...
You can use this piece of code to send an email in powershell.
$smtpServer = "mailserver.com"
$From = "from#mailserver.com"
$To = "to#example.com"
$Subject = "example"
$body = "test"
send-mailmessage -to $To -from $From -subject $Subject -SmtpServer $smtpServer -Body $body -BodyAsHtml -Port 25

$Env:ComputerName doesnt work with powershell Send-MailMessage in batch file

After a lot of errors and edits I got the Send-MailMessage to work through a command prompt. Below iteration sends out the emails perfect.
powershell Send-MailMessage -From " `<user#domain>`" -To " `<user#domain.com>`" -Subject 'Some subject goes here' -Body 'Some body with alert regarding Host: $env:computername. List of deleted files is attached.' -Attachments 'C:\somefile.txt' -Priority High -dno onSuccess, onFailure -SmtpServer 'smtp.domain.com'
However, this does not fetch the computername in the body. I have tried running this command in powershell directly and it works with the computername variable in body.
To get it to simply send out mails, I have already tried doing
powershell -command "command" or powershell -command "{command} or
powershell -command "& {command}" and so on and it doesnt even send
out emails.
As I am now successful sending out emails, I need to have the Computername inside the body text.
Use double quotes around the argument value to have variables contained expanded.
I.e.:
-Body "Some body with alert regarding Host: **$env:computername**. ..."
Relevant reading.
Your command line in the question and your own answer relies on the fact that powershell currently has the default (position 0) argument -Command,
but as this changes from PowerShell 6.0.0beta3 on to -File
you should explicitly use at least -C as the shortest
abbreviviation for -Command.
to speed up execution I'd use the additionl parameters -NoProfile or short -NoP and -NonInteractive or -NonI
to stop cmd from trying to interpret any parameters/arguments you should double quote them all - and escape any necessary inner double quotes with a backslash \" while also replacing double quotes with single ones if ever possible.
So I'd suggest:
powershell -NoP -NonI -C "Send-MailMessage -From \"SomeWeb-Prod#domain.com\" -To \"fromuser#domain.com\" -Subject 'Some notification for SomeWeb-Prod' -Body 'Some Alert for Host: %computername% with IP: %NetworkIP% at %time%. details in attached file.' -Attachments 'C:\somefile.txt' -Priority High -dno onFailure -SmtpServer 'smtp.domain.com'"
Or, (as you partly discovered yourself):
powershell -NoP -NonI -C "Send-MailMessage -From 'SomeWeb-Prod#domain.com' -To 'fromuser#domain.com' -Subject 'Some notification for SomeWeb-Prod' -Body 'Some Alert for Host: %computername% with IP: %NetworkIP% at %time%. details in attached file.' -Attachments 'C:\somefile.txt' -Priority High -dno onFailure -SmtpServer 'smtp.domain.com'"
"$env:computername" doesnot work for me. It simply prints it as $env:computername
%computername% worked.
Here's what's working for me right now.
powershell Send-MailMessage -From " `<user#domain.com>`" -To " `<user#domain.com>`" -Subject 'Some subject goes here' -Body 'Some body with alert regarding Host: %computername%. Some file is attached.' -Attachments 'C:\somefile.txt' -Priority High -dno onSuccess, onFailure -SmtpServer 'smtp.domain.com'
Update:
The above command worked on Win7 fine, but gave errors on server 2012. Finally this is what worked on win 2012
powershell "Send-MailMessage -From "SomeWeb-Prod#domain.com" -To "fromuser#domain.com" -Subject 'Some notification for SomeWeb-Prod' -Body 'Some Alert for Host: %computername% with IP: %NetworkIP% at %time%. details in attached file.' -Attachments 'C:\somefile.txt' -Priority High -dno onFailure -SmtpServer 'smtp.domain.com'"
It works for me, by using double quotes around variables.
I am using batch script to call powershell Send-MailMessage
Batch Script:send_email.bat
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -windowstyle hidden -command 'E:\path\send_email.ps1
Pwershell Script send_email.ps1
Send-MailMessage -From "noreply#$env:computername" -To '<target_email#example.com>' -Subject 'Blah Blah' -SmtpServer 'smtp.domain.com' -Attachments 'E:\path\file.log' -BODY "Blah Blah on Host: $env:computername "

Email credentials when using send-mailmessage command

I have searched through many many forums and they do explain how to do this but the technical language is just too difficult to understand as I'm very new to powershell. I would like this explained to me step by step (baby steps). I would like to run this powershell command in a batch file (.bat). I have a batch file that does robocopy backups weekly and I want the batch file to send me a email when the backup is complete. The only issue I have is the credentials, I get a pop-up box asking for the user name and password. When I eneter this information the email will send successfully. Here is what I have;
Using: powershell V2.0 Windows 7 Ultimate
Powershell -command send-mailmessage -to emailadress#provider.com -from emailaddress#provider.com -smtp smtp.broadband.provider.com -usessl -subject 'backup complete'
$from = "example#mail.com"
$to = "example#mail.com"
$smtp = "smtpAddress.com"
$sub = "hi"
$body = "test mail"
$secpasswd = ConvertTo-SecureString "yourpassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($from, $secpasswd)
Send-MailMessage -To $to -From $from -Subject $sub -Body $body -Credential $mycreds -SmtpServer $smtp -DeliveryNotificationOption Never -BodyAsHtml
You could pass the credential object in your same command - which would avoid the popup:
Powershell -Command 'Send-MailMessage -to "emailadress#provider.com" -from "emailaddress#provider.com" -smtp "smtp.broadband.provider.com" -usessl -subject "backup complete" -credential (new-object System.Net.NetworkCredential("user","pass","domain"))'
I'd recommend storing the username/password in a somewhat more safer format, but this should do your trick.
I'm not sure you can do SMTP authentication using the send-mailmessage command. But, you can send a message through an SMTP server that requires authentication using the Net.Mail.SmtpClient object and the System.Net.Mail.MailMessage object. See How to pass credentials to the Send-MailMessage command for sending emails for a good example.
look at the last exemple of send-mailmessage helppage
you will see you can pass credential whith the parameter -credential domain01\admin01
look here Using PowerShell credentials without being prompted for a password if you dont want any prompt (save your cred in a text file)

Trouble sending email using Powershell

I've run the following script:
PS C:\> Send-MailMessage -To <EmailAddress1> -From <EmailAddress2> -Body "This is a test" -Subject "TEST MAIL" -SmtpServer <INTERNAL IP OF SMTP SERVER>
And I receive the following error:
Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.
At line:1 char:17
+ Send-MailMessage <<<< -To <EmailAddress1> -From <EmailAddress2> -Body "This is a test" -Subject "TEST MAIL" -SmtpServer <INTERNAL IP OF SMTP SERVER>
+ CategoryInfo: InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-Mail Message], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage`
I was made aware of the fact that I need permissions to send email from my local machine through the SMTP server, and as far as I know, I've been granted those rights.
Would somebody please help point me in the right direction on this one?
The ultimate goal is to be able to send emails as part of some Powershell scripts.
Thanks!
I prefer the Net.Mail.SmtpClient method of sending email. This script would send the contents of a file passed as a parameter.
$emailFrom = "AUTOMATED_PRERUN#somehost.com"
$emailTo = "somebody#somehost.com"
$subject = "TEST"
get-content $Args[0] | %{$Body+= " {0} `n" -f $_}
$smtpServer = "mailserver.somehost.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
Though, your error sounds more like a networking issue of some type.
You might want to check that you can reach the SMTP port on the server:
https://learn.microsoft.com/en-us/Exchange/mail-flow/test-smtp-with-telnet
Probably your antivirus file service is locking mail delivery.