我已經編寫了一個腳本,用於從多個服務器收集日誌,然後將它們壓縮並通過電子郵件發送。由於這些電子郵件的收件人可能有所不同,具體取決於我選擇使用用戶輸入而不是硬編碼電子郵件地址的情況。這是我有:如何在send-mailmessage的-To參數中使用read-host變量
$to = '"' + (read-host -prompt "Send to") + '"'
do {
Write-host "Add another recipient?"
$choice = read-host -prompt "[y/n]"
switch ($choice) {
'y'{
$add = ',"' + (read-host -prompt "Send to") + '"'
$to = -Join ($to, $add)
}
}
}
until ($choice -eq 'n')
$issuenum = read-host -prompt "Issue Number (enter if none)"
$title = read-host -prompt "Email Title"
$body = read-host -prompt "Message"
Send-MailMessage -To $to -From "[email protected]" -SMTPServer smtpserver.com -Subject "[$issuenum] $title" -body $body -attachments $attachments
}
但是,在運行該腳本,我得到以下錯誤時:
Send-MailMessage : An invalid character was found in the mail header: '"'.
At C:\PATH\SCRIPT.ps1:431 char:1
+ Send-MailMessage -To $to -From "[email protected]" -SMTPServer ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [Send-MailMessage], FormatException
+ FullyQualifiedErrorId : FormatException,Microsoft.PowerShell.Commands.SendMailMessage
Send-MailMessage : A recipient must be specified.
At C:\PATH\SCRIPT.ps1:431 char:1
+ Send-MailMessage -To $to -From "[email protected]" -SMTPServer ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.SendMailMessage
當我展示我的$變量的值,它輸出什麼,我希望看到: sendmailmessage-to參數的正確格式。
"[email protected]","[email protected]"
我在做什麼錯?
你需要傳遞給發送-一個MAILMESSAGE String []數組。 [string []] $ recipients =「[email protected]」,「[email protected]」 –