powershell:将文件内容通过管道传输到命令中
powershell: pipe file content into a command
我对 PowerShell 不是很流利,而且我在一个有点简单的问题上苦苦挣扎:
我想用 DPAPI 加密一个(小的)随机文件并将结果写入另一个文件。
我可以加密立即字符串:
'cleartext' | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
--> the result is a bunch of digits: 1000000d08c9d[...]0e35854
同样的事情,输出到文件中:
'cleartext' | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Set-Content -Path output.txt
--> the file is created, and contains the right data
但我不明白如何从文件中获取数据:
# content of a file
Get-Content -Path input.txt
# --> 1st line
# blah
# last line
# pipe it into a command
Get-Content -Path input.txt | Measure-Object -line -word
# --> Lines Words Characters Property
# ----- ----- ---------- --------
# 3 5
# but can not pipe it into ConvertTo-SecureString
Get-Content -Path input.txt | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
# --> ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is an empty string.
# + ... -Path input.txt | ConvertTo-SecureString -AsPlainText -Force | Conve ...
# + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# + CategoryInfo : InvalidData : (:PSObject) [ConvertTo-SecureString], ParameterBindingValidationException
# + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
问题:如何让 ConvertTo-SecureString 读取文件内容?
感谢 @Santiago Squarzon
的回答:Get-Content
默认 return 一个字符串数组。
要获取“真实”数据,必须使用“-raw”参数。
Get-Content -Path input.txt -Raw | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
# --> 1000000d08c9d[...]0e35854
这里的问题是数据类型不匹配。 ConvertTo-SecureString 采用字符串输入而不是数组。如有疑问 运行 GetType() 对象上的方法:
'cleartext'.GetType().Name
String
而 return 来自 vanilla Get-Content 是对象数组:
(Get-Content .\input.txt).GetType().Name
Object[]
您不必使用 名称 属性,但它更干净。
因此,正如评论中所建议的那样,使用“-Raw”开关将为您提供您所需要的:
(Get-Content .\input.txt -Raw).GetType().Name
String
我对 PowerShell 不是很流利,而且我在一个有点简单的问题上苦苦挣扎:
我想用 DPAPI 加密一个(小的)随机文件并将结果写入另一个文件。
我可以加密立即字符串:
'cleartext' | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
--> the result is a bunch of digits: 1000000d08c9d[...]0e35854
同样的事情,输出到文件中:
'cleartext' | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Set-Content -Path output.txt
--> the file is created, and contains the right data
但我不明白如何从文件中获取数据:
# content of a file
Get-Content -Path input.txt
# --> 1st line
# blah
# last line
# pipe it into a command
Get-Content -Path input.txt | Measure-Object -line -word
# --> Lines Words Characters Property
# ----- ----- ---------- --------
# 3 5
# but can not pipe it into ConvertTo-SecureString
Get-Content -Path input.txt | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
# --> ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is an empty string.
# + ... -Path input.txt | ConvertTo-SecureString -AsPlainText -Force | Conve ...
# + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# + CategoryInfo : InvalidData : (:PSObject) [ConvertTo-SecureString], ParameterBindingValidationException
# + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
问题:如何让 ConvertTo-SecureString 读取文件内容?
感谢 @Santiago Squarzon
的回答:Get-Content
默认 return 一个字符串数组。
要获取“真实”数据,必须使用“-raw”参数。
Get-Content -Path input.txt -Raw | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
# --> 1000000d08c9d[...]0e35854
这里的问题是数据类型不匹配。 ConvertTo-SecureString 采用字符串输入而不是数组。如有疑问 运行 GetType() 对象上的方法:
'cleartext'.GetType().Name
String
而 return 来自 vanilla Get-Content 是对象数组:
(Get-Content .\input.txt).GetType().Name
Object[]
您不必使用 名称 属性,但它更干净。
因此,正如评论中所建议的那样,使用“-Raw”开关将为您提供您所需要的:
(Get-Content .\input.txt -Raw).GetType().Name
String