使用 -EncodedCommand 时无法重定向 PowerShell 输出

发布于 2024-07-16 12:52:34 字数 816 浏览 7 评论 0原文

对于我的测试,我使用“开始>” “运行”对话框(不是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃素的狼 2024-07-23 12:52:34

“运行”对话框似乎根本不提供重定向。 根据此 usenet 帖子,您仅当您有控制台时才获得重定向。 我想知道 powershell.exe 是否正在解析重定向参数,如果它没有接收编码输入,它会选择重定向? 听起来像是 Raymond Chen 的问题。

无论如何,这是可行的,但代价是生成一个无用的控制台:

cmd /c powershell -EncodedCommand IAA1ACsANwAgAA== > c:\ps.txt

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:

cmd /c powershell -EncodedCommand IAA1ACsANwAgAA== > c:\ps.txt
飘然心甜 2024-07-23 12:52:34

这两个命令之间的区别在于 -Command 参数是贪婪的。 它会获取后面命令行上的所有内容,而 -EncodedCommand 并不贪婪。 第一个命令真正做的是:

powershell -Command "4+5 > c:\log.txt"

新的 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:

powershell -Command "4+5 > c:\log.txt"

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).

世界如花海般美丽 2024-07-23 12:52:34

我必须将命令与重定向一起编码。

function Test
{
    $cmd = { 5+7 }
    $encodedCommand = EncodeCommand "$cmd > 'c:\log2.log'"

    StartProcess "powershell -Command $cmd > c:\log.txt"
    StartProcess "powershell -EncodedCommand $encodedCommand"
}

所以这会将 5+7 之和写入 c:\

powershell -EncodedCommand IAA1ACsANwAgACAAPgAgACcAYwA6AFwAbABvAGcAMgAuAGwAbwBnACcA

log2.log

crb 建议使用“cmd /c”。 但在这种情况下,编码的脚本长度将受到 命令的限制线路限制

在运行 Microsoft Windows 的计算机上
XP 或更高版本,最大长度
您可以在命令中使用的字符串
提示符为 8191 个字符。 在
运行 Microsoft Windows 的计算机
2000或Windows NT 4.0,最大
您可以使用的字符串的长度
在命令提示符下是 2047
字符。

I had to encode command together with redirection.

function Test
{
    $cmd = { 5+7 }
    $encodedCommand = EncodeCommand "$cmd > 'c:\log2.log'"

    StartProcess "powershell -Command $cmd > c:\log.txt"
    StartProcess "powershell -EncodedCommand $encodedCommand"
}

So this will write a sum 5+7 into c:\log2.log

powershell -EncodedCommand IAA1ACsANwAgACAAPgAgACcAYwA6AFwAbABvAGcAMgAuAGwAbwBnACcA

P.S.

crb suggested to use "cmd /c". But in this case the encoded script length will be constrained by the command line limitations

On computers running Microsoft Windows
XP or later, the maximum length of the
string that you can use at the command
prompt is 8191 characters. On
computers running Microsoft Windows
2000 or Windows NT 4.0, the maximum
length of the string that you can use
at the command prompt is 2047
characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文