通过 powershell 执行 cmd 命令失败,并出现错误“字符串缺少终止符:'。”

问题描述 投票:0回答:1

我最初有一个包含以下代码的 .bat 脚本:

cmd /V:ON /C "set unique=nets /ao&&FOR %%A IN (0 1 2 3 2 6 2 4 5 6 0 7 1337) DO set final=!final!!unique:~%%A,1!&& IF %%A==1337 CALL !final:~-12!"

这只是执行“netstat /ao”。它只是根据循环中的索引重建每个元素。

为了进一步实验,我尝试在 cmd 中但通过 powershell 运行以下命令。我使用的命令如下:

powershell -Command "Start-Process cmd.exe -ArgumentList '/c cmd /V:ON /K \"set unique=nets /ao&&FOR %A IN (0 1 2 3 2 6 2 4 5 6 0 7 1337) DO set final=!final!!unique:~%A,1!&& IF %A==1337 CALL !final:~-12!\"'"

但我收到以下错误:

The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

我尝试使用参数和执行命令的方式,但总是遇到相同的错误。知道我在这里做错了什么吗?

powershell batch-file cmd
1个回答
0
投票

为了达到你既定的目标,即在cmd中运行命令:

%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Start-Process \"$Env:SystemRoot\System32\cmd.exe\" -ArgumentList \"/V:On /D /C `\"@Set `\"unique=nets /ao`\" & For %G In (0 1 2 3 2 6 2 4 5 6 0 7 1337) Do @Set `\"final=!final!!unique:~%G,1!`\" & If %G Equ 1337 !final:~-12!`\"\""

请注意,如果您确实想验证它是否符合您的预期,则需要将

/C
更改为
/K

© www.soinside.com 2019 - 2024. All rights reserved.