我在PS中使用下一个命令:
"Password" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
这将生成一个我保存为“Key.txt”文件的密钥
现在我想用这个解密密码:
$password = Get-Content password.txt (or just copy-pasting the key)
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $username,($password | ConvertTo-SecureString)
但...
我应该如何添加到这...
$EmailFrom = "[email protected]"
$EmailTo = "[email protected]"
$Subject = "Test"
$Body = "this is a Test"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("My_USer", "My_Password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
我想将它添加为My_Password,当然我应该添加一个来自Key.txt文件的变量$ password,但是然后......?
首先,我们保存凭据
"Password123" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File C:\key.txt -NoNewline
然后我们可以像这样使用它:
$SMTPClient = New-Object Net.Mail.SmtpClient("SomeServer", 587)
$SMTPClient.Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist ThisIsAUserName ,($(Get-Content C:\key.txt) | ConvertTo-SecureString)
我们可以检查以确保它正确加载如下:
$SMTPClient.Credentials | select username, password
输出看起来像这样
UserName Password
-------- --------
ThisIsAUserName Password123
不存在,用纯文本存储并不是很好,但是如果你不关心它那么它就在那里。
您还有其他选择,包括安全/加密文件和Windows CredMan:
Quickly and securely storing your credentials – PowerShell
要获取凭据对象,我们可以手动创建凭证对象,也可以使用Get-Credential cmdlet提示帐户详细信息:
$Credential = Get-Credential
要将凭据存储到.cred文件中:
$Credential | Export-CliXml -Path "${env:\userprofile}\Jaap.Cred"
并从文件加载凭据并返回到变量:
$Credential = Import-CliXml -Path "${env:\userprofile}\Jaap.Cred"
Invoke-Command -Computername 'Server01' -Credential $Credential {whoami}
Securely Store Credentials on Disk
Allow multiple users to access credentials stored using export-clixml
How to run a PowerShell script against multiple Active Directory domains with different credentials
PowerShell凭据管理器CredMan.ps1是一个PowerShell脚本,提供对用于管理存储凭据的Win32凭据管理器API的访问。 https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Credentials-d44c3cde
和模块使用
https://powershellgallery.com/packages/BetterCredentials https://powershellgallery.com/packages/CredentialManager https://powershellgallery.com/packages/IntelliTect.CredentialManager