我试图在不提升用户和密码的情况下从PowerShell发送邮件(我希望它是自动的),我使用了下面的代码,但我得到了错误的信息。The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.
当我刚刚进入 Get-Credentials
它的工作正常这是我的代码
$pass = ConvertTo-SecureString “mypassword” -AsPlainText -Force
$From = "[email protected]"
$To = "[email protected]"
$Attachment = "C:\hosts.txt"
$Subject = "files from script"
$Body = "none"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Cred = New-Object System.Management.Automation.PSCredential (“[email protected]”, $pass)
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -BodyAsHtml -SmtpServer $SMTPServer -Port $SMTPPort -Credential $cred -Attachments $Attachment -UseSsl
将你的密码存储在一个安全的文件中,注册表或Windows密码管理器,并根据需要从那里调用。 大量的例子在网络上。
使用Powershell操纵Windows 82012 PasswordVault中的凭证。 PasswordVault.psm1
最后一个链接中的样本。
<# Set and encrypt credentials to file using default method #>
$credential = Get-Credential
$credential.Password |
ConvertFrom-SecureString |
Set-Content c:scriptsencrypted_password1.txt
<#
Set some variables
...
#>
$emailusername = "myemail"
$encrypted = Get-Content c:scriptsencrypted_password.txt |
ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($emailusername, $encrypted)
if($something = $somethingElse)
{
<#
Do some stuff
...
#>
$EmailFrom = "[email protected]"
$EmailTo = "[email protected]"
$Subject = "I did some stuff!"
$Body = "This is a notification from Powershell."
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = $credential;
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}
这是一个非常常见的事情,以限制需要手动输入credsmultiple times,并且非常好的记录,在MS powershellgallery.com中的模块可以用于这种类型的事情。
Find-Module -Name '*credential*' |
Format-Table -AutoSize
<#
# Results
Version Name Repository Description
------- ---- ---------- -----------
2.0 CredentialManager PSGallery Provides access to credentials ...
1.0.4 WindowsCredential PSGallery Management module for Windows Credential Store.
...
1.1.0 CredentialSpec PSGallery Tools to create and find Credential Spe ...
4.5 BetterCredentials PSGallery A (compatible) major upgrade for Get-Credential, ...
1.0.11 pscredentialmanager PSGallery This module allows management and autom ...
...
1.0.0 CredentialLocker PSGallery CredentialLocker is a module that provide ...
1.1 CredentialsManager PSGallery The module Credentials Manager provides y ...
1.2.2.20190715 SimplyCredential PSGallery Simply Module for windows credentials.
1.0.2 CredentialManagement PSGallery Manage Credentials stored in the Windows Credential Manager
...
1.1.0 PSCredentialTools PSGallery PSCredentialTools provides various methods for ...
1.0.0.0 SelectCredential PSGallery A module for selecting the credential stor ...
2.1 SecuredCredential PSGallery SecuredCredential Routines for modules suppor ....
1.0.477 PSCredentialStore PSGallery A simple credential manager to store and ...
...
3.0 CredentialUtility PSGallery This is a credential manager tool which co ...
1.3 MiCredentialModule PSGallery Saves/Retrieves credentials to/from a file (w ...
1.3 vaultcredential PSGallery Manages credentials in the credential vault
0.0.1 SecureCredentials PSGallery This module allow to secure store encrypted ...
#>