我想解密從System.Security.SecureString到可讀密碼的密碼。PowerShell - 解碼System.Security.SecureString爲可讀密碼
$password = convertto-securestring "TestPassword" -asplaintext -force
$credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain")
此代碼部分工作正常,我可以使用$ credentials對象。 但後來在我的代碼中,我需要可讀格式的密碼。因爲方法需要可讀字符串中的密碼。所以我必須將密碼解碼回來。
如何從$ credentials對象解碼密碼?
更新
不工作:
$password = convertto-securestring "TestPassword" -asplaintext -force
$credentials = New-Object System.Net.NetworkCredential("TestUsername", $password, "TestDomain")
$Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($credentials.password)
$result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
$result
一旦你擁有了'PSCredentials'對象,你就可以: '''$ credentials.GetNetworkCredential()。Password''' – CubanX