首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Power Shell脚本SMTP电子邮件问题

Power Shell脚本SMTP电子邮件问题
EN

Stack Overflow用户
提问于 2018-06-10 02:40:34
回答 1查看 376关注 0票数 0

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

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

smtpMessage Send-MailMessage :无法解析远程名称:'System.Net.Mail.SmtpClient‘,第:21行字符:1+ Send-MailMessage @smtpMessage块引用

代码语言:javascript
复制
    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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 03:43:04

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

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

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

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

代码语言:javascript
复制
# 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

代码语言:javascript
复制
$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

代码语言:javascript
复制
# 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

这是一个常见的问题,因此您的post请求可以被认为是这些请求的重复:

Using PowerShell credentials without being prompted for a password

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50777442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档