Сценарий Powershell - Отправить аутентифицированную электронную почту - PullRequest
1 голос
/ 21 апреля 2020

Итак, у меня есть скрипт отчета, который работает, и я добавляю скрипт ниже, чтобы он отправлял электронное письмо. Все это работает отдельно, не проходит аутентификацию.

Пароль хранится в файле, который работает, и часть аутентификации работает для -Sendmail версии, но я не могу получить его для аутентификации при добавлении в скрипт.

Спасибо за вашу помощь

Я думаю, что это та часть, где мне нужно пройти аутентификацию для "$ cred" "IF ($ SendEmail -eq $ TRUE) {$ smtp.Send ($ msg)} "

$msg = new-object Net.Mail.MailMessage
$msg.IsBodyHTML = $TRUE    
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Pass
$AdminName = "alerts@alkait.co.uk"
$Pass = Get-Content "C:\Masters\WIN SERVER BACKUP SCRIPT\cred.txt" | ConvertTo-SecureString
$msg.From = $FromAddress
$msg.To.Add($ToAddress)
$msg.Subject = $ReportSubject
$msg.Body = $sb

# Format the date and time for the file.
$VarDateTime = (Get-Date -format "yyyy-MMMM-dd-dddd-HHmm")
# Note the extension for the $CleanOldReports
$OutPut = "$OutPutPath\$VarDateTime.html"
# The output folder - The parent folder of the $OutPut.
$TARGETDIR = (split-path $OutPut -Parent)
# If the output folder does not exist then create it.
IF (!(Test-Path -Path $TARGETDIR )) {New-Item -ItemType directory -Path $TARGETDIR}
#Output the report to a file.
"$sb" | Out-File "$OutPut"
# If $TRUE then open the backup report in your default web browser.
IF ($ShowReport -eq $TRUE) {"$OutPut" | invoke-expression}
# If $TRUE then email the report to backup operators.
IF ($SendEmail -eq $TRUE) {$smtp.Send($msg)}
Write-Host "Report Successfully Generated." -ForegroundColor green

1 Ответ

1 голос
/ 21 апреля 2020

Поэтому я добавил Send-MailMessage вместо того, чтобы тянуть сверху и он работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...