我正在尝试批量从securestring中提取密码。
这是在PS:
Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\Temp\Password.txt"
$pass = Get-Content "C:\Temp\Password.txt" | ConvertTo-SecureString
$stringValue = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass));
它按预期工作。
我正在批量转换上面的代码:
powershell -Command "& {Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\Temp\Password.txt"}"
powershell -command "& {$pass = Get-Content "C:\Temp\Password.txt" | ConvertTo-SecureString; ^
$stringValue = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))}"
FOR /F "usebackq delims=" %v IN (`stringValue`) DO set "d=%v"
echo %d%
我不确定是否有什么问题,我只能在 echo%d%上获得“ powershell.exe -encodedCommand $ encoded命令”,而不是明文密码。< / p>
答案 0 :(得分:0)
在能够在cmd中使用它之前,您必须回显$stringValue
。
您可以使用转化结果的回应将作业替换为$stringValue
,并将其嵌入到for&#34;循环&#34;。
修改强>
让我们写一些代码。
:: Write the password to File (unchanged)
powershell -Command "& {Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\Temp\Password.txt"}"
::Read in the file, echo it out. (changed at the end)
powershell -command "& {$pass = Get-Content "C:\Temp\Password.txt" | ConvertTo-SecureString; $stringValue = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass));echo $stringValue}"
上面代码中的最后一行以纯文本形式显示密码。 现在,您只需将最后一行嵌入到for循环中,而不是只显示密码:
FOR /F "usebackq delims=" %v IN (`powershell -command "& {$pass = Get-Content "C:\Temp\Password.txt" | ConvertTo-SecureString; $stringValue = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass));echo $stringValue}"`) DO set "d=%v"
现在,echo会给我密码:
C:\Temp> FOR /F "usebackq delims=" %v IN (`powershell -command "& {$pass = Get-Content "C:\Temp\Password.txt" | ConvertTo-SecureString; $stringValue = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass));echo $stringValue}"`) DO set "d=%v"
C:\Temp> set "d=asdf"
C:\Temp> echo %d%
asdf
C:\Temp>