【问题标题】:Using encrypted password to send mail is failing使用加密密码发送邮件失败
【发布时间】:2020-03-19 20:07:00
【问题描述】:

我正在尝试在发送电子邮件的脚本中使用存储的加密密码,但我不断收到错误消息:

send-mailmessage : SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.57 SMTP;在 MAIL FROM 期间,客户端未通过身份验证发送匿名邮件 [DM6PR66666019.namprd456.prod.outlook.com]

我使用以下代码创建文本文件:

$passwordtostore = 'NotTheRealPassword$9gv8z6VHnPfDd8zc'
$secureStringPWD = $passwordtostore | ConvertTo-SecureString -AsPlainText -Force
$secureStringText = $secureStringPWD | ConvertFrom-SecureString
Set-Content "c:\temp\scriptsencrypted_password1.txt" $secureStringText

我使用以下导入密码:

$passwordFile = "c:\temp\scriptsencrypted_password1.txt"
$password = Get-Content $passwordFile | ConvertTo-SecureString

这是我正在使用的 sendmail 功能:

function SendMail($ToEmails,$FromEmail,$Subj,$Body,$UserName,$Password){
    $cred = New-Object System.Management.Automation.PSCredential $UserName,$Password

    $MailParams=@{"from"=$FromEmail; "to"=$ToEmails;"body"=$Body;"subject"=$Subj;"smtpserver"="smtp.office365.com"}

    send-mailmessage @MailParams -Credential $cred -UseSsl $true -port 587
}

这是调用该函数的代码:

$alertEmail = "me.stillme@mydomain.com"
$username="psemail@mydomain.com"
$passwordFile = "c:\temp\scriptsencrypted_password1.txt"
$password = Get-Content $passwordFile | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($username, $password)

Import-Module -Name "..\SendMail.psm1"

... Doing some stuff

SendMail $alertEmail $username "This is the subject" "this is the body" $credential.UserName $credential.Password

【问题讨论】:

    标签: powershell sendmail


    【解决方案1】:

    就我个人而言,我不明白为什么要在需要为 Send-MailMessage cmdlet 导入的模块中创建一个函数。 这使事情变得更难使用。

    此外,您似乎正在函数内部切换电子邮件地址 To 和 From。

    无论如何,当您创建凭据时出现问题,将其拆分为用户名和(安全)密码以作为参数发送给函数并将它们重新组合成一个凭据对象。

    为什么不跳过那个模块功能,干脆做:

    $password = Get-Content 'c:\temp\scriptsencrypted_password1.txt' -Raw | ConvertTo-SecureString -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PsCredential('YourLoginName', $password)
    $MailParams=@{
        From       = 'me.stillme@mydomain.com'
        To         = 'psemail@mydomain.com'
        Body       = "this is the body"
        Subject    = "This is the subject"
        SmtpServer = 'smtp.office365.com'
        Port       = 587
        UseSsl     = $true
        Credential = $credential
    }
    
    Send-MailMessage @MailParams
    

    这将使您的脚本更具可读性/可维护性

    【讨论】:

    • 收到错误“ConvertTo-SecureString:输入字符串的格式不正确:”。如果我在 ConvertTo-SecureString 之后添加“-AsPlainText -Force”,它就会消失,但我仍然会遇到与以前相同的错误。
    • @Michael Then.. 你确定文本文件中的密码正确并且属于你使用的用户名吗?那里没有空格或空行?
    • 是的,不幸的是,我是。这个问题一定与加密密码有关,因为如果我使用纯文本,它就可以工作。上面可以看到我用来加密的方法;你觉得有什么问题吗?我正在生成文件的同一台计算机上进行测试。我什至尝试在脚本中生成文件,以防 Windows 用户对于正在运行的脚本与我登录的用户不同;没有快乐。
    • @Michael 我无法重现这个.. 尝试了不同的方法:$credential = New-Object System.Management.Automation.PsCredential -ArgumentList 'LoginName', $password$credential = [System.Management.Automation.PsCredential]::new('LoginName', $password) 都有效。但是,由于您正在从文件中读取,这可能与该文件的编码有关。您是如何将密码保存到文件中的?您是否想过使用 Export-CliXml 保存凭据?
    【解决方案2】:

    我在上面的代码中发现了两个问题:

    1. 密码中未转义的 $。
    2. 在设置内容中添加“-NoNewline”后,它就开始工作了。

    所以,要创建加密文件:

    $passwordtostore = "7K9CBgvc4rttvfctrsef6PVHqnP6fDdwhatevervtfdscttzSc"
    $secureStringPWD = $passwordtostore | ConvertTo-SecureString -AsPlainText -Force
    $secureStringText = $secureStringPWD | ConvertFrom-SecureString
    Set-Content "D:\Powershell Scripts\encrypted.hash" $secureStringText -NoNewline
    

    然后检索密码并在凭据中使用它:

    $password = Get-Content $passwordFile -Raw | ConvertTo-SecureString
    $credential = New-Object System.Management.Automation.PsCredential($username, $password)
    

    这不是问题的根源,但我确实实施了@Theo 的建议:使用发送邮件消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-19
      • 2015-05-21
      • 2017-10-27
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      相关资源
      最近更新 更多