2017-01-30 29 views
1

私はpowershellで電子メールを送信したいと思います。私の資格情報を手作業で入力すれば、スクリプトはうまく動作します。しかし、私はスクリプト内に資格情報のパラメータを与えたい。私のスクリプトは次のようになります。powershellで電子メールを送信

$From = "[email protected]" 
$To = "[email protected]" 
$Cc = "[email protected]" 
$Attachment = "C:\Users\test\test\test.ini" 
$Subject = "Email Subject" 
$Body = "Insert body text here" 
$SMTPServer = "smtp.mail.yahoo.com" 
$SMTPPort = "587" 
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject ` 
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl ` 
-Credential ( 
$MyClearTextUsername=’[email protected]’ 
$MyClearTextPassword=’test123’ 

$SecurePassword=Convertto-SecureString –String $MyClearTextPassword –AsPlainText –force 

$MyCredentials=New-object System.Management.Automation.PSCredential $MyClearTextPassword,$SecurePassword) -Attachments $Attachment 

答えて

1

ここでは、オブジェクトの資格情報を作成することができます方法は次のとおりです。

$cred = ([pscredential]::new('[email protected]',(ConvertTo-SecureString -String 'test123' -AsPlainText -Force))) 

ので、あなたのケースの使用中:

Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject ` 
    -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl ` 
    -Credential $cred -Attachments $Attachment 

私はフィットしようとしているにはポイントを参照してくださいませんSend-MailMessageの中にそれを作成して参照してください。読むのが簡単です。

+0

ありがとうございます! – Tibi

+0

問題はない、答えがあなたを助けたなら、答えとしてそれをマーク;)@ Tibi – 4c74356b41

+1

マークされた!今まで私にさせてくれないだろう。あなたはあまりにも速い答え:D – Tibi

関連する問題