如何在 smtp 凭据中使用加密的安全密码
How can I use encrypted secure password in smtp credentials
在我的 .ps1 文件中,我想执行一个作业并通过电子邮件发送结果。我希望密码不可读,所以我事先通过这样做来加密它。
$password = read-host -prompt "Enter your Password"
write-host "$password is password"
$secure = ConvertTo-SecureString $password -force -asPlainText
$bytes = ConvertFrom-SecureString $secure
$bytes | out-file .\securepassword.txt
到目前为止工作正常。
然后我想在 .ps1 中使用安全密码,这是我遇到困难的地方。我尝试了很多变体,但我认为这是我迄今为止最可靠的尝试:
$usercred = "myemail@gmail.com"
$encryptedpw = Get-Content .\securepassword.txt
$pwcred = $ecryptedpw | ConvertTo-SecureString
当我尝试像这样在 $smtp.credentials
中使用它时:
$smtp.Credentials = New-Object System.Net.NetworkCredential($usercred, $pwcred);
它在 $smtp.send($msg)
处输出以下错误 The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
而且我知道密码是问题所在(至少我很确定),因为如果我手动输入密码 - 它就可以正常工作。有什么想法吗?
_ 编辑 1 _
如果有人想知道,下面是整个发送邮件部分:
# Send Notification if alert $i is greater then 0
if ($i -gt 0)
{
foreach ($user in $users)
{
$smtpServer = "smtp.gmail.com"
$port = "587"
$smtp.EnableSSL = $true
$smtp = New-Object Net.Mail.SmtpClient($smtpServer, $port)
$smtp.EnableSsl = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($usercred, $pwcred);
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($user)
$msg.From = "myemail@gmail.com"
$msg.Subject = "Environment DiskSpace Report for $titledate"
$msg.IsBodyHTML = $true
$msg.Body = get-content $blablabla
$smtp.Send($msg)
$body = ""
}
}
根据这个 link 的答案,您可以尝试以下操作:
$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $FileWhereYouIsStored | ConvertTo-SecureString)
$smtp.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
希望对您有所帮助
多亏了这里的一些输入,我才能够制定出解决方案。我认为还有一个因素在其中发挥了作用。
1) 我从另一台机器加密了密码,我显然不应该这样做。我注意到加密的结果根据进行加密的计算机而有所不同(有人可以随意解释这一点)。
2) 我最终得到的代码如下:
$usercred = "myemail@gmail.com"
$encryptedpw = Get-Content .\securepassword.txt
$password = ConvertTo-SecureString -String $encryptedpw
然后是
$smtp.Credentials = New-Object System.Net.NetworkCredential($usercred, $password);
在我的 .ps1 文件中,我想执行一个作业并通过电子邮件发送结果。我希望密码不可读,所以我事先通过这样做来加密它。
$password = read-host -prompt "Enter your Password"
write-host "$password is password"
$secure = ConvertTo-SecureString $password -force -asPlainText
$bytes = ConvertFrom-SecureString $secure
$bytes | out-file .\securepassword.txt
到目前为止工作正常。
然后我想在 .ps1 中使用安全密码,这是我遇到困难的地方。我尝试了很多变体,但我认为这是我迄今为止最可靠的尝试:
$usercred = "myemail@gmail.com"
$encryptedpw = Get-Content .\securepassword.txt
$pwcred = $ecryptedpw | ConvertTo-SecureString
当我尝试像这样在 $smtp.credentials
中使用它时:
$smtp.Credentials = New-Object System.Net.NetworkCredential($usercred, $pwcred);
它在 $smtp.send($msg)
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
而且我知道密码是问题所在(至少我很确定),因为如果我手动输入密码 - 它就可以正常工作。有什么想法吗?
_ 编辑 1 _
如果有人想知道,下面是整个发送邮件部分:
# Send Notification if alert $i is greater then 0
if ($i -gt 0)
{
foreach ($user in $users)
{
$smtpServer = "smtp.gmail.com"
$port = "587"
$smtp.EnableSSL = $true
$smtp = New-Object Net.Mail.SmtpClient($smtpServer, $port)
$smtp.EnableSsl = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($usercred, $pwcred);
$msg = New-Object Net.Mail.MailMessage
$msg.To.Add($user)
$msg.From = "myemail@gmail.com"
$msg.Subject = "Environment DiskSpace Report for $titledate"
$msg.IsBodyHTML = $true
$msg.Body = get-content $blablabla
$smtp.Send($msg)
$body = ""
}
}
根据这个 link 的答案,您可以尝试以下操作:
$cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $FileWhereYouIsStored | ConvertTo-SecureString)
$smtp.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
希望对您有所帮助
多亏了这里的一些输入,我才能够制定出解决方案。我认为还有一个因素在其中发挥了作用。
1) 我从另一台机器加密了密码,我显然不应该这样做。我注意到加密的结果根据进行加密的计算机而有所不同(有人可以随意解释这一点)。
2) 我最终得到的代码如下:
$usercred = "myemail@gmail.com"
$encryptedpw = Get-Content .\securepassword.txt
$password = ConvertTo-SecureString -String $encryptedpw
然后是
$smtp.Credentials = New-Object System.Net.NetworkCredential($usercred, $password);