2017-04-04 9 views
0

私は電子メールの送信をテストしようとしていますが、私はスクリプト内に平文のパスワードを持っていません。安全なパスワードを使用してPowershell経由で電子メールを送信

は、ここで私が持っているものだが、これは動作します:

$SmtpServer = 'smtp.office365.com' 
$SmtpUser = '[email protected]' 
$smtpPassword = 'Hunter2' 
$MailtTo = '[email protected]' 
$MailFrom = '[email protected]' 
$MailSubject = "Test using $SmtpServer" 
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force) 
Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -Credential $Credentials 

これは動作します。

this stackoverflow threadのアドバイスに従っています。このスクリプトを資格情報の入力を求めずに実行する(またはプレーンテキストを入力する)ので、スケジュールされたタスクとして実行できます。

私は、私が実行して作成した安全なパスワードがあります。

read-host -assecurestring | convertfrom-securestring | out-file C:\Users\FubsyGamr\Documents\mysecurestring_fubsygamr.txt 

をしかし、私は提案したエントリと私の$ smtpPasswordエントリ置き換える場合:

$SmtpServer = 'smtp.office365.com' 
$SmtpUser = '[email protected]' 
$smtpPassword = cat C:\Users\FubsyGamr\Documents\mysecurestring_fubsygamr.txt | convertto-securestring 
$MailtTo = '[email protected]' 
$MailFrom = '[email protected]' 
$MailSubject = "Test using $SmtpServer" 
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force) 
Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -Credential $Credentials 

を次に電子メールが送信されません。もう次のエラーが表示されます。

Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

ヒントを教えてください。私はスケジュールされたタスクとしてこの電子メールスクリプトを実行したいですが、パスワードを平文で保存したくありません。

答えて

1

同僚は$Credentialsオブジェクトが私のパスワードを平文に変換しようとしていたことに気付きました。 ConvertTo-SecureSTring -AsPlainText -Force修飾子を削除しました。メールが正常に送信されました。

機能スクリプト:

$SmtpServer = 'smtp.office365.com' 
$SmtpUser = '[email protected]' 
$smtpPassword = cat C:\Users\FubsyGamr\Documents\mysecurestring_fubsygamr.txt | convertto-securestring 
$MailtTo = '[email protected]' 
$MailFrom = '[email protected]' 
$MailSubject = "Test using $SmtpServer" 
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $smtpPassword 
Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -Credential $Credentials 
関連する問題