使用PowerShell進行安全密碼加密
很多時候我們需要在PowerShell中使用密碼,並將其傳遞給憑據引數,而密碼應該始終是安全字串,而不是純文字。下面介紹幾種加密密碼的方法。
a) Get-Credential 格式
我們有一種方法可以使用cmdlet **Get-Credential**儲存使用者名稱和密碼。它將提供一個GUI提示。您可以將此密碼儲存到變數中,並在以後的命令中使用它。
$cred = Get-Credential
憑據儲存在**$cred**變數中。以下是變數的值。輸出如下。
PS C:\WINDOWS\system32> $cred UserName Password -------- -------- test System.Security.SecureString
您可以看到密碼儲存在安全字串中。您可以將上述變數與cmdlet支援的憑據引數一起使用。
例如:
Invoke-Command -ComputerName Test-PC -ScriptBlock {Get-Service} - Credential $cred
您可以看到這個密碼的加密形式,為此,您需要使用**ConvertFrom-SecureString**命令。
PS C:\WINDOWS\system32> $cred.Password | ConvertFrom-SecureString 01000000d08c9ddf0115d1118c7a00c04fc297eb01000000fe83583138f0ce4bb0e3654f0529948100000000020000000000 106600000001000020000000cf8760de4e2ca7a28c8efd055c578dc7779f3984f25c1f9bc8c7a8ec50d97aa1000000000e80 00000002000020000000fb33efe90790e302ee738ad245b0b256d34e49728a08dfd2ef524da22f9bccaf100000003cf7a899 ea4c75edd7b4e418351f686040000000b21de677725588fb8f8bd589de058247171c103b48a50d06f152f25c8196935ca78f f7b1909bc5e76ad5c9cf1e2df497769aed3c1bb2f4035c26e6bdd7aa3875
b) 安全字串格式
另一種獲取安全字串中密碼的方法是使用**Read-Host**命令和–**AsSecureString**引數。
PS C:\WINDOWS\system32> $passwd = Read-Host "Enter Password" -AsSecureString Enter Password: *******
您可以透過建立新的**PSCredential**物件,直接在支援**Credential**引數的cmdlet中使用此密碼,如下例所示。
$username = Read-Host "Enter UserName" $passwd = Read-Host "Enter Password" -AsSecureString $creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$passwd Connect-VIServer -Server TestvCenter.lab -Credential $creds
當您檢查此密碼變數時,它也處於**Secure.String**格式,並且您可以再次使用**ConvertFrom-SecureString**管道命令檢索加密的密碼。
PS C:\WINDOWS\system32> $passwd System.Security.SecureString PS C:\WINDOWS\system32> $passwd | ConvertFrom-SecureString 01000000d08c9ddf0115d1118c7a00c04fc297eb01000000fe83583138f0ce4bb0e3654f05299481000000000200000000001 06600000001000020000000d5439c95404cc4e81dbb83b557e58cec12b5047e441b22bd3dabca96bc231d39000000000e8000 000002000020000000a9c2b95f3363ecbd46beaf927a1779ff2f43e17fb0e3aadf0d2c9c3dedc5fb1e10000000439a72d5bd9 97aacd529f49425909151400000008ad5e497b74e054e21ea1232c6ae45a64af302d91df09c0768eacd57b08f6b86f9bc4244 75c2e14173edd287e8a9304c570104475d09ebd9ab4c419167222260
一旦您的密碼字串安全,您可以直接將其用作密碼。您不需要使用ConvertFrom-SecureString獲取加密的密碼。這只是為了檢視密碼安全字串。
c) 明文格式
如果**密碼**是**明文**格式,您可以直接在支援**Password**引數的命令中使用明文密碼,但不推薦以下方法,因為它採用明文格式,可能造成嚴重的安全漏洞。請參見以下示例。
Connect-VIServer -Server TestvCenter.lab -User "Testadmin" - Password "PowerShell"
您可以將明文密碼轉換為安全字串格式。當您在安全位置放置了一個包含密碼的文字檔案,而PowerShell需要在不使用明文的情況下使用該密碼時,這很有用。過程如下所示。
$passwd = "Test@123" | ConvertTo-SecureString -AsPlainText -Force
PS C:\WINDOWS\system32> $passwd System.Security.SecureString
現在我們的密碼是安全的,我們可以將其用作憑據中的密碼。在這裡,我們使用**$cred**引數連線名為**TestvCenter.lab**的vCenter伺服器。
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList “test”,$passwd Connect-VIServer -Server TestvCenter.lab -Credential $creds
您可以使用以下方法檢視加密的密碼。它是文字編碼格式,而不是原始密碼。
PS C:\WINDOWS\system32> $passwd | ConvertFrom-SecureString 01000000d08c9ddf0115d1118c7a00c04fc297eb01000000fe83583138f0ce4bb0e3654f052994810000000002 0000000000106600000001000020000000358daf9fec158cf68ccf5e06bf0eb829953553f5e703df2ce0d97e8a 95f5761f000000000e8000000002000020000000cf65dea31f8f4082392f958cd836b22cff2b6d86c3ab29d235 2f56aa757300b520000000f9626238a73c899ebf61bd935651a8deb391e2615164779d461c679ad04bc1264000 00007050962ea9d8c67f051b9fcbdbee953dffa2cc2377905911814a8d87d3e7678914a3e0292cb24977767881 6391919c5518d6251a78f87b6efd990cb3eabe96e4
如果您需要將此密碼儲存在檔案中,則可以使用上述命令。
PS C:\WINDOWS\system32> $passwd | ConvertFrom-SecureString | Out-File C:\Passwd.txt
但是,當您取回密碼時,需要再次將其轉換為安全字串格式,因為**credential**引數只接受安全字串。
PS C:\WINDOWS\system32> $passwd = (Get-Content C:\Passwd.txt) | ConvertTo-SecureString
PS C:\WINDOWS\system32> $passwd System.Security.SecureString
您可以在支援的cmdlet的credential引數中使用此密碼。