【发布时间】:2016-03-14 22:19:22
【问题描述】:
我使用 Powershell 已经有一段时间了,但我不明白加密是如何工作的,甚至不确定我在阅读帮助文件时使用了正确的语法。
#Get User Information
$User = Read-Host "Please enter your username"
$Password = Read-Host "Please enter your password. This will be encrypted" -AsSecureString | ConvertTo-SecureString -AsPlainText -Force
#Define a Key File
$KeyFile = "C:\Powershell\AES.key"
$Key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | out-file $KeyFile
#Encrypt using AES
$PasswordFile = "C:\Powershell\Password.txt"
$KeyFile = "C:\Powershell\AES.key"
$Key = Get-Content $KeyFile
$Password | ConvertFrom-SecureString | Out-File $PasswordFile
#Set credentials to be called.
$myCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key)
#Open text file.
Invoke-Command -ComputerName localhost -Credential $MyCredentials -ScriptBlock{
Invoke-Item C:\Powershell\Password.txt
}
我在运行这个时收到一个错误,我不确定为什么我不能通过管道传输这个:
ConvertFrom-SecureString : 输入对象不能绑定到任何 该命令的参数要么是因为该命令不采用 管道输入或输入及其属性与任何 接受管道输入的参数。在 C:\Powershell\Password.ps1:15 字符:13 + $密码 | ConvertFrom-SecureString -Key $Key |输出文件 $PasswordFile + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (System.Security.SecureString:String) [ConvertFrom-SecureString], 参数绑定异常 + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand
是否有任何加密专家可以提供帮助?我试图简单地将密码保存到文本文件(加密),然后我想使用另一个脚本使用加密密码使用新凭据调用各种程序,但我什至无法让加密密码正常工作。提前致谢。
【问题讨论】:
标签: powershell password-encryption