【问题标题】:Escape powershell characters inside cmd在 cmd 中转义 powershell 字符
【发布时间】:2021-06-20 07:37:45
【问题描述】:

我想执行一个 ping 扫描,就是这个 powershell 命令:

$NET="192.168.111";for($i=1;$i -lt 255;$i++){$command="ping -n 1 -w 100 $NET.$i > nul 2>&1 && echo $NET.$i";start-process -nonewwindow "cmd" -argumentlist "/c $command" -redirectstandardoutput "tmp$i.txt"};cat tmp*.txt > sweep.txt

我需要在cmd里面做这个,我已经试过了:

没有引号

powershell -c $NET="192.168.111";for($i=1;$i -lt 255;$i++){$command="ping -n 1 -w 100 $NET.$i > nul 2>&1 && echo $NET.$i";start-process -nonewwindow "cmd" -argumentlist "/c $command" -redirectstandardoutput "tmp$i.txt"};cat tmp*.txt > sweep.txt

带有 1 个双引号

powershell -c "$NET="192.168.111";for($i=1;$i -lt 255;$i++){$command="ping -n 1 -w 100 $NET.$i > nul 2>&1 && echo $NET.$i";start-process -nonewwindow "cmd" -argumentlist "/c $command" -redirectstandardoutput "tmp$i.txt"};cat tmp*.txt > sweep.txt"

有 3 个双引号

powershell -c """$NET="192.168.111";for($i=1;$i -lt 255;$i++){$command="ping -n 1 -w 100 $NET.$i > nul 2>&1 && echo $NET.$i";start-process -nonewwindow "cmd" -argumentlist "/c $command" -redirectstandardoutput "tmp$i.txt"};cat tmp*.txt > sweep.txt"""

转义 &&

powershell -c "$NET="192.168.111";for($i=1;$i -lt 255;$i++){$command="ping -n 1 -w 100 $NET.$i > nul 2>&1 \&\& echo $NET.$i";start-process -nonewwindow "cmd" -argumentlist "/c $command" -redirectstandardoutput "tmp$i.txt"};cat tmp*.txt > sweep.txt"

还没有成功。

谢谢!

【问题讨论】:

  • 您应该将整个 PowerShell 命令用双引号括起来,以将其作为单个参数传递给 powershell.exe。然后你需要从 cmd.exe 解析中转义嵌套的双引号字符。为此,转义字符是反斜杠。例如powershell "$NET=\"192.168.111\";for($i=1;$i -lt 255;$i++){$command=\"ping -n 1 -w 100 $NET.$i > nul 2>&1 && echo $NET.$i\";start-process -nonewwindow \"cmd\" -argumentlist \"/c $command\" -redirectstandardoutput \"tmp$i.txt\"};cat tmp*.txt > sweep.txt"这假设您提供的一个衬垫直接在 PowerShell 中工作
  • 它不起作用,缺少 } 它说。

标签: powershell cmd escaping


【解决方案1】:

这对我有用,我在引号前使用了反引号来逃避。即使这可以通过本机 Powershell 命令来完成,但由于您要求特定的东西,这里就是。

$NET="192.168.111"

for($i=1;$i -lt 255;$i++)
{
    $command="echo ########### && echo Pinging $NET.$i && ping -n 1 -w 100 $NET.$i 2>&1"
    start-process "cmd" -argumentlist "/c `"$command`"" -NoNewWindow -RedirectStandardOutput "tmp$i.txt"
}

while($true)
{
    if(-not (Get-Process cmd -EA SilentlyContinue))
    {
        cat tmp*.txt > sweep.txt
        break
    }
    sleep 5
}

请注意,while 循环将等待所有 cmd.exe 进程停止,然后再将这些文件分解并统一为 sweep.txt。请记住在运行此脚本之前关闭您打开的任何 cmd.exe 实例,否则此循环将永远继续:D

【讨论】:

  • 感谢您的回答!我忘了说,在oneliner ...
  • 当然,如果你想要一个单行,你可以用分号把所有的东西都包起来。
  • powershell -c $NET="192.168.111";for($i=1;$i -lt 255;$i++){$command="echo ######## ### && echo Pinging $NET.$i && ping -n 1 -w 100 $NET.$i 2>&1";start-process "cmd" -argumentlist "/c "$command"" -NoNewWindow -RedirectStandardOutput " tmp$i.txt"};while($true){if(-not (Get-Process cmd -EA SilentlyContinue)){cat tmp*.txt > sweep.txt;break}sleep 5} 我缺少分号关闭
  • 不知道这是否解决了您的问题,或者您仍然找不到解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多