Power Shell脚本SMTP电子邮件问题

时间:2018-06-09 18:40:34

标签: powershell smtp powershell-v2.0 sendmail

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

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

  

Send-MailMessage:无法解析远程名称:' System.Net.Mail.SmtpClient'   在行:21 char:1   + Send-MailMessage @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)

1 个答案:

答案 0 :(得分:3)

无论是否发送电子邮件,您都可以在脚本中以纯文本形式输入您的信用卡(正如您现在所做的那样) 但实际上是一种不明智的做法< / em> 或将它们存储在安全文件中(更好的选项)并根据需要调用它们。

在整个网络和本论坛上,有很多关于这个思维过程的例子。例如:

  

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

     

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

 this.timer = setTimeout(this.timedOut.bind(this), this.ROUND_TIME);
        this.prompter.getUserResponse(this.checkAnswer.bind(this));
  

以JSON格式存储PowerShell凭据

     

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

# 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}
  

在磁盘上安全存储凭据

     

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

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

这是一个常见问题,因此您的帖子请求可能会被认为是这些问题的重复:

  

Using PowerShell credentials without being prompted for a password

     

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