使用 -EncodedCommand 时无法重定向 PowerShell 输出
对于我的测试,我使用“开始>” “运行”对话框(不是 cmd.exe)。
这工作正常,我在 log.txt 中得到 9
powershell -Command 4+5 > c:\log.txt
但这不起作用:
powershell -EncodedCommand IAA1ACsANwAgAA== > c:\log.txt
那么在这种情况下如何重定向输出?
实验代码:
function Test
{
$cmd = { 5+7 }
$encodedCommand = EncodeCommand $cmd
StartProcess "powershell -Command $cmd > c:\log.txt"
StartProcess "powershell -EncodedCommand $encodedCommand > c:\log2.txt"
}
function StartProcess($commandLine)
{
Invoke-WMIMethod -path win32_process -name create -argumentList $commandLine
}
function EncodeCommand($expression)
{
$commandBytes = [System.Text.Encoding]::Unicode.GetBytes($expression)
[Convert]::ToBase64String($commandBytes)
}
For my tests I am using 'Start > Run' dialog (not the cmd.exe).
This works fine, and I get 9 in log.txt
powershell -Command 4+5 > c:\log.txt
But this does not work:
powershell -EncodedCommand IAA1ACsANwAgAA== > c:\log.txt
So how can I redirect output in this case?
Experimental code:
function Test
{
$cmd = { 5+7 }
$encodedCommand = EncodeCommand $cmd
StartProcess "powershell -Command $cmd > c:\log.txt"
StartProcess "powershell -EncodedCommand $encodedCommand > c:\log2.txt"
}
function StartProcess($commandLine)
{
Invoke-WMIMethod -path win32_process -name create -argumentList $commandLine
}
function EncodeCommand($expression)
{
$commandBytes = [System.Text.Encoding]::Unicode.GetBytes($expression)
[Convert]::ToBase64String($commandBytes)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“运行”对话框似乎根本不提供重定向。 根据此 usenet 帖子,您仅当您有控制台时才获得重定向。 我想知道 powershell.exe 是否正在解析重定向参数,如果它没有接收编码输入,它会选择重定向? 听起来像是 Raymond Chen 的问题。
无论如何,这是可行的,但代价是生成一个无用的控制台:
The "Run" dialog doesn't seem like it provides redirection at all. According to this usenet post you only get redirection if you have a console. I wonder if the redirection parameter is being parsed by powershell.exe, which is choosing to redirect if it's not receiving encoded input? Sounds like a question for Raymond Chen.
Anyway, this works, at the expense of spawning an otherwise useless console:
这两个命令之间的区别在于 -Command 参数是贪婪的。 它会获取后面命令行上的所有内容,而 -EncodedCommand 并不贪婪。 第一个命令真正做的是:
新的 PowerShell 实例正在处理重定向。 但是,如果您使用 -EncodedCommand 参数,新的 PowerShell 实例不会看到重定向,因为您没有将其包含在编码命令中。 如果调用 PowerShell 的环境没有重定向(例如在计划任务中),这可能是一件坏事。
因此,正如“crb”所示,您需要将重定向编码到命令中,或者从可以处理重定向的环境(例如 cmd 或另一个 PowerShell 实例)调用 PowerShell。
The difference between those two commands is that the -Command parameter is greedy. It takes everything on the command line after it, while -EncodedCommand is not greedy. What the first command is really doing is:
So the new PowerShell instance is handling the redirection. However, if you use the -EncodedCommand paramter, the new PowerShell instance does not see the redirection because you did not include it in the encoded command. This can be a bad thing if the environment calling PowerShell does not have redirection (like in a scheduled task).
So, as "crb" showed, you need to either encode the redirection into your command, or call PowerShell from an environment that can handle the redirection (like cmd, or another PowerShell instance).
我必须将命令与重定向一起编码。
所以这会将 5+7 之和写入 c:\
log2.log
crb 建议使用“cmd /c”。 但在这种情况下,编码的脚本长度将受到 命令的限制线路限制
I had to encode command together with redirection.
So this will write a sum 5+7 into c:\log2.log
P.S.
crb suggested to use "cmd /c". But in this case the encoded script length will be constrained by the command line limitations