我需要一个通过PowerShell执行CLI命令的简单测试用例,该命令已经在Base64中编码。
假设Get-ChildItem
已提前转换为Base64字符串R2V0LUNoaWxkSXRlbQ==
。
此外,假设我打开了一个DOS CLI实例,并希望测试在powershell中执行此字符串:
C:>\ powershell.exe -enc R2V0LUNoaWxkSXRlbQ==
但是,我收到以下错误:
The term '???????' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:8
*??????? <<<<
+ CategoryInfo :ObjectNotFound: (???????:String) [], CommandNotFoundException
+ FullyQualifiedErrorID: CommandNotFoundException
我知道有一种方法可以引入变量的使用,甚至包括编码过程本身。但我想知道的是,这个方法可行吗?我可能做错什么了?我正在Win7中运行PS2.0版本。
发布于 2016-05-21 03:41:14
如果必须对其进行编码,请使用以下方法获取Base64字符串:
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes('Get-ChildItem'))
这对我来说很管用:
powershell.exe -encodedCommand RwBlAHQALQBDAGgAaQBsAGQASQB0AGUAbQA=
发布于 2016-12-02 20:55:30
要添加到@SomethingElse的答案中,不工作的Base64字符串与其工作的Base64字符串之间的区别是,在将原始字符串转换为字节值时使用的字符编码。
它需要被编码为UTF-16-LE,然后转换为Base64以便PowerShell喜欢它,而您的编码为UTF 8/平原ASCII。
# Your version, with 1-byte-per-character encoding:
PS> [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes('Get-ChildItem'))
R2V0LUNoaWxkSXRlbQ==
# Working version, with 2-bytes-per-character encoding:
PS> [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes('Get-ChildItem'))
RwBlAHQALQBDAGgAaQBsAGQASQB0AGUAbQA=
https://serverfault.com/questions/778123
复制相似问题