首頁>Program>source

我在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變數,但是然後...?

最新回復
  • 2019-10-6
    1 #

    首先,我们儲存凭据

    "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
    

  • 2019-10-6
    2 #

    不是,以纯文字格式儲存根本不好,但是如果您不關心它,那就可以了.

    您還有其他選擇,包括安全/加密檔案和windows CredMan:

    快速安全地儲存您的凭据-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}
    

    將凭据安全地儲存在磁盤上

    允许多个使用者訪問使用export-clixml儲存的凭据

    如何针對具有不同凭据的多个Active Directory域執行PowerShell指令碼

    PowerShell凭證管理器 CredMan.ps1是一个PowerShell指令碼,它提供對win32 Credential Manager API的訪問,该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

  • java:將Spring MVC与AwS DocumentDB連線
  • xamarin:如何使用TwilioXamarinBindings TwilioVoiceiOS接听来電