2017-09-14 137 views
-1

錯誤PowerShell來發送郵件

新對象:無法找到 「PSCredential的」 過載和 爭論數: 「2」。在d:\腳本\ gsend.ps1:12字符:15

代碼

#Create Password File (Only need once) 
#$Credential = Get-Credential 
#$Credential.Password | ConvertFrom-SecureString | Set-Content "D:\scripts\gsendcred.txt" 

#Send Email 

$EncryptedCredential = "D:\scripts\gsendcred.txt" 

$EmailUsername = "[email protected]" 

$EncryptedPW = Get-Content "D:\scripts\gsendcred.txt" 

$EncryptedCredential = ConvertTo-SecureString -String $EncryptedCredential - 
AsPlainText -Force 

$Credential = New-Object Management.Automation.PSCredential ($EmailUsername, 
$EncryptedPW) 

$EmailFrom = "[email protected]" 

$EmailTo = "[email protected]" 

$EmailSubject = "GSEND Test Subject" 

$EmailBody = "Test Body" 

$SMTPServer = "smtp.gmail.com" 

$SMTPPort = 587 

$SMTPSsl = $true 
+0

這是我收到的錯誤:新對象:無法找到「PSCredential的」和參數計數過載:「2」。 在D:\ Scripts \ gsend.ps1:12 char:15 –

+2

歡迎來到SO。請閱讀以下內容,然後再向SO發佈更多內容:https://stackoverflow.com/help/asking –

+1

使用您的$ EncryptedCredential而不是$ EncryptedPW,PSCredential對象需要SecureString –

回答

0

您可以使用以下PowerShell腳本用於發送郵件

發送- - 要MAILMESSAGE「ABC @ abc.com「-From」[email protected]「-Cc」[email protected]「,」[email protected]「 - 主題測試郵件-BodyAsHtml $ htmlbody -SmtpServer」mailhost.abc.com「

其中$ htmlbody是包含html的字符串變量。

0

我相信你的問題是處理憑據。下面是我如何處理SMTP憑證:

$smtpPwd = "password" 
$smtpCredentialsUsername = "[email protected]" 
$smtpCredentialsPassword = ConvertTo-SecureString -String $smtpPwd -AsPlainText -Force 

$Credentials = New-Object –TypeName System.Management.Automation.PSCredential 
    –ArgumentList $smtpCredentialsUsername, $smtpCredentialsPassword 

然後:

$smtp.Credentials = $Credentials 

或者,如果您使用的發送,MAILMESSAGE存儲您的電子郵件憑據本地通過自身事先運行這個簡短的腳本:

Get-Credential | Export-Clixml C:\fso\myemailcreds.xml 

它會提示您輸入您的憑證,然後將其存儲在本地(在本例中)名爲fso的文件夾中。然後,在任何發送-MAILMESSAGE cmdlet將使用本地存儲的憑據,如下所示:

Send-MailMessage -To 'Recipient <[email protected]>' -from 'John Donnelly <[email protected]>' 
    -cc 'whoever <[email protected]>' -Subject $subject -Body $body 
    -BodyAsHtml -smtpserver "smtp.office365.com" -usessl 
    -Credential (Import-Clixml C:\fso\myemailcreds.xml) -Port 587 
+0

感謝約翰,但它仍然無法與我自己的腳本改正我的問題。郵件仍然不會出去 –

+0

您可以發佈整個腳本嗎? –

+0

約翰,下面是腳本: –