Automation of distribution that can be corrected?

Good day! Is the problem that the letter was sent on behalf of the shared mailbox, the script needs to take attachments from a folder, to set the date a day early and a gun to send out. I came up with this, but I can not overcome the 2 moment - how to make an attachment of all files in the folder without listing them by name and how to make the date correct on the day ago no min in the context of this task. Do not judge strictly, I'm new, by Google using...

#Incoming message data:
$From = "123@123.ru"
$To = "234@234"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "234@123u"
$Password = "12345555"
$subject = "Summary of {0}" -f $dateBegin
$body = "Report for region {0}" -f $dateBegin
$dateBegin = ((get-date).adddays(-1).toshortdatestring())

#the generated message is in html format:
$message = New-Object System.Net.Mail.MailMessage $From, $To
$message.Subject = $subject
$message.IsBodyHTML = $true
$message.Body = $body
$file = "C:\12\30.09.2017\30.09.2017.csv"

#Create two instances of a class
$att = New-object Net.Mail.Attachment($file)
$mes = New-Object System.Net.Mail.MailMessage
$mes.Attachments.Add($att)

#Send:
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$smtp.Send($message)
June 14th 19 at 20:53
1 answer
June 14th 19 at 20:55
how to make the date correct on day ago without minutes

(get-date).adddays(-1).Date.toshortdatestring()

how to make an attachment of all files in the folder without listing them by name

$mes = New-Object System.Net.Mail.MailMessage
(Get-ChildItem -Path 'C:\MyFolder').FullName | foreach { $mes.Attachments.Add( New-object Net.Mail.Attachment($_) ) }
$mes = New-Object System.Net.Mail.MailMessage
(Get-ChildItem -Path 'C:\12\30.09.2017\30.09.2017').FullName | foreach { $mes.Attachments.Add( New-object Net.Mail.Attachment($_))}

Unexpected token "(" in expression or statement.
Sign is missing a closing "}" in statement block or type definition.

will check back - rene87 commented on June 14th 19 at 20:58

Find more questions by tags PowerShell