【问题标题】:Power Shell Script SMTP Email IssuesPowershell 脚本 SMTP 电子邮件问题
【发布时间】:2018-06-09 18:40:33
【问题描述】:

1) 我如何在这个脚本中存储我的凭据 - 它提示我输入凭据

2) 最后两行应该在底部还是在参数之前?我收到此错误 - 我认为我没有正确传递我的凭据

Send-MailMessage:无法解析远程名称:'System.Net.Mail.SmtpClient' 在行:21 字符:1 + 发送邮件消息@smtpMessage 块引用

    param (
    [string]$Path = "C:\Users\me\Desktop\ScanFolder",
    $From = "me@msn.com",
    $To = "you@msn.com",
    $Subject1 = "Changes Found",
    $Subject2 = "No Changes in last x minutes",
    $Body = "anything",
    $SMTPServer = "smtp.live.com",
    $SMTPPort = "587",
    [PSCredential]$Credential #pass in credential
)
$smtpMessage = @{
    To = $To
    From = $From
    Subject = $Subject
    Smtpserver = $SMTPClient
    Credential = $Credential
    BodyAsHtml = $true
}

Send-MailMessage @smtpMessage

$secpasswd = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("me@msn.com", $secpasswd)

【问题讨论】:

标签: powershell smtp powershell-2.0 sendmail


【解决方案1】:

无论是电子邮件还是其他方式,您都可以在脚本中以纯文本形式输入您的信用(就像您现在所做的那样)但这确实是一种不明智的做法 或将它们存储在安全文件中(更好的选择)并根据需要调用它们。

整个网络和这个论坛都有大量关于这种思维过程的例子。例如:

快速安全地存储您的凭据 - PowerShell

https://www.jaapbrasser.com/quickly-and-securely-storing-your-credentials-powershell

# To get a credential object we can either manually create one or use the Get-Credential cmdlet to prompt for the account details:

$Credential = Get-Credential

# To store the credentials into a .cred file:

$Credential | Export-CliXml -Path "${env:\userprofile}\Jaap.Cred"

# And to load the credentials from the file and back into a variable:

$Credential = Import-CliXml -Path "${env:\userprofile}\Jaap.Cred"
Invoke-Command -Computername 'Server01' -Credential $Credential {whoami}

以 JSON 格式存储 PowerShell 凭据

https://jdhitsolutions.com/blog/powershell/5396/storing-powershell-credentials-in-json

$secure = ConvertTo-SecureString -String 'P@$$w0rd' -AsPlainText -Force
$cred = New-Object -typename PSCredential -ArgumentList @('company\admin',$secure)
$cred | Export-clixml c:\work\admin.xml

在磁盘上安全地存储凭据

http://powershellcookbook.com/recipe/PukO/securely-store-credentials-on-disk

# The first step for storing a password on disk is usually a manual one. There is nothing mandatory about the filename, but we’ll use a convention to name the file CurrentScript.ps1.credential. Given a credential that you’ve stored in the $credential variable, you can safely use the Export-CliXml cmdlet to save the credential to disk. Replace CurrentScript with the name of the script that will be loading it:
PS > $credPath = Join-Path (Split-Path $profile) 

# CurrentScript.ps1.credential
PS > $credential | Export-CliXml $credPath

# In PowerShell version 2, you must use the ConvertFrom-SecureString cmdlet:

PS > $credPath = Join-Path (Split-Path $profile) CurrentScript.ps1.credential
PS > $credential.Password | ConvertFrom-SecureString | Set-Content $credPath

这是一个常见问题,因此您的发帖请求可能被视为与以下问题重复:

Using PowerShell credentials without being prompted for a password

How to pass credentials to the Send-MailMessage command for sending emails

【讨论】:

  • 感谢您,设法让凭据在没有提示的情况下工作。将尝试按照建议将凭据存储在文件中。
  • 更改为使用我的 gmail 帐户时似乎出现问题:使用“1”参数调用“发送”时出现异常:“SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.5.1 Authentication Required。可能是因为我有双重身份验证吗?
  • 确保您在代码中使用了“-Usessl true”,正如我在回复的最后一个链接中所指出的那样,2FA 是完全不同的东西。
  • 开始工作 - 必须为 gmail 帐户创建应用密码。
  • 很高兴你成功了。 2FA 是一件好事,但我们都必须计划好使用它的影响。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
相关资源
最近更新 更多