【问题标题】:Escaping strings when using wmic使用 wmic 时转义字符串
【发布时间】:2014-05-23 10:46:52
【问题描述】:

我正在尝试使用 wmic 启动 VLC 实例 我这样做主要是因为我想捕获创建进程的pid

我知道下面的命令在命令行中可以正常工作:

C:\PROGRA~1\VideoLAN\VLC_117\vlc.exe rtsp://abcd --sout="#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show"

(对于本示例而言,rtsp://abcd 可以是任何输入文件)

尝试通过 wmic 运行它,使用各种不同的尝试作为转义序列(以下是其中之一):

wmic process create 'C:\PROGRA~1\VideoLAN\VLC_117\vlc.exe rtsp://abcd --sout="#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show" '

可靠地给了我同样的错误:

Invalid format.
Hint: <assignlist> = <propertyname>=<propertyvalue> [, <assignlist>].

但是如下:

wmic process create 'C:\PROGRA~1\VideoLAN\VLC_117\vlc.exe rtsp://abcd --sout="#duplicate{dst=display} --rtsp-caching=120 --no-video-title-show"'

工作正常 - 除了作为命令它对我没用。所以问题似乎出在我原始命令的嵌套大括号部分。

我尝试了各种不同的转义字符...到目前为止都没有成功。谁能建议我哪里出错了?

【问题讨论】:

  • 请更新您的问题以显示在没有 WMIC 的情况下工作的命令。我想我可以给你一个解决方案,但我不确定如何使用现有发布的代码来解释你的意图。
  • 嗨@dbenham - 感谢您的详细回复。发布的命令在命令行中运行良好。它正在加载 vlc,然后复制流。​​一个副本到屏幕,另一个到“视频。 mpg"."rtsp://abcd" 可以是任何测试源。按照建议将其传递给 wmic 时,仍然会出现上述错误。问题似乎与括号部分有关。修改它以删除第二组括号:C:\PROGRA~1\VideoLAN\VLC_117\vlc.exe rtsp://abcd --sout="#duplicate{dst=display} --rtsp-caching=120 --no-video-title-show" 有效。但是这样做我已经停止转储到文件
  • 我仍然不清楚 - 哪个命令有效?请编辑您的问题(标签下方有一个编辑链接)并发布给出您想要的结果的确切工作命令(没有 WMIC)。
  • 干杯@dbenham - 我已经更新了这个问题,希望能让事情更清楚
  • 坏消息 - 可能没有解决方案。请参阅我的更新答案。

标签: cmd wmic


【解决方案1】:

在 CMD 中引用和转义规则已经够复杂了,当你添加 WMIC 复杂性时,它们会变得更加疯狂。

WMIC 通常可以使用' 或" 作为引号字符。双引号内的双引号可以使用\" 进行转义。单引号中的单引号也可以使用\'进行转义,但是反斜杠似乎没有被消耗,所以看起来没什么用。

CMD 只使用",没有办法在带引号的字符串中转义"。 &amp;、|、&lt; 等不在引号内的毒字符必须像^&amp; 等一样转义。

尝试合并 WMIC 和 CMD 的引用和转义规则很棘手。请记住,CMD 引用和转义发生在 WMIC 看到命令行之前。

另外,您可以使用PROCESS CALL CREATE 代替PROCESS CREATE。我想PROCESS CREATE 是可能的,但我从未见过它是如何完成的。

我不确定我是否知道不使用 WMIC 的实际命令行。根据您的代码,我假设以下方法可行:

C:\PROGRA~1\VideoLAN\VLC_117\vlc.exe rtsp://abcd --sout="#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show"

如果是这样,那么我相信以下 WMIC 命令会起作用:

wmic process call create 'C:\PROGRA~1\VideoLAN\VLC_117\vlc.exe rtsp://abcd --sout="#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show"'

WMIC 必须在创建后将整个命令行视为一个连续的字符串。 'vlc_117/video.mpg' 中的内部单引号不会引起问题,因为内容中没有空格。添加空格会破坏这个解决方案,需要另一种策略。

如果您在 exe 路径周围使用双引号,您应该能够使用长路径而不是短路径:

wmic process call create '"C:\Program Files\VideoLAN\VLC_117\vlc.exe" rtsp://abcd --sout="#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show"'

您可能希望在批处理文件中的变量中捕获 PID,在这种情况下将使用 FOR /F 命令。这增加了更多的复杂性。像=、, 等不带引号的 CMD 标记分隔符必须被转义,因为 FOR /F 引入了额外的解析层。由于额外的解析层,未加引号的毒字符也必须被转义。

for /f "tokens=2 delims=;= " %%N in (
  'wmic process call create '"C:\Program Files\VideoLAN\VLC_117\vlc.exe" rtsp://abcd --sout^="#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show"' ^| find "ProcessId"'
) do set "pid=%%N"

FOR /F IN() 子句中的封闭单引号不会导致问题,因为它们在 WMIC 看到命令之前就被剥离了。但--sout=... 中未引​​用的= 必须转义。

我无法测试上述任何内容,因此可能没有一个可以按照书面说明进行。但是,我讨论的概念应该仍然有效。

由于引用和转义的层数和复杂性,对命令行的看似微小的更改可能会对解决方案产生重大影响。如果命令不是我解释的那样,请通过编辑您的问题让我知道正确的命令,我可以尝试调整答案。


2014 年 5 月 27 日更新

我的坏消息。 PROCESS CALL CREATE 选项期望完整的命令行作为第一个字符串参数,可选地后跟工作目录作为第二个参数。坏消息是参数由逗号分隔。您的命令行也有逗号,并且 PROCESS CALL CREATE 解析器(无论是什么)似乎都不支持命令行中的逗号:(

我尝试在单引号或双引号中引用逗号,但这没有帮助。我也试过用\ 转义逗号。再次,没有运气。我也用谷歌搜索了一个解决方案,结果一无所获。

恐怕没有解决方案,除非有可用的 VLC 语法可以消除逗号,或者如果有办法将复杂的 VLC 命令行参数放在某种类型的外部脚本文件中。或者也许其他一些聪明人发现了一种逃避逗号的方法?

这里有一些例子展示了我的尝试。我没有 VLC,所以我简单地将 CMD /C 替换为 ECHO 语句。每次尝试 ECHO 逗号都会失败。

@echo off
:: All of these commands without commas work just fine
wmic process call create 'cmd /c echo hello world^&pause'
wmic process call create 'cmd /c "echo hello world&pause"'
wmic process call create "cmd /c echo hello world&pause"

:: But none of these commands with commas work
wmic process call create 'cmd /c echo hello,goodbye^&pause'
wmic process call create 'cmd /c "echo hello,goodbye&pause"'
wmic process call create "cmd /c echo hello,goodbye&pause"
wmic process call create 'cmd /c echo hello\,goodbye^&pause'
wmic process call create 'cmd /c "echo hello\,goodbye&pause"'
wmic process call create "cmd /c echo hello\,goodbye&pause"

2014-12-23 更新:已找到解决方案!

在 DosTips,我们一群人开发了various methods for a batch script to determine its own PID。一旦知道这一点,就可以使用 WMIC 列出父会话的所有子进程。通过仔细的簿记,可以可靠地确定每个新创建的子进程的 PID。但它确实需要大量的代码。仅当您的命令行无法通过 WMIC PROCESS CALL CREATE(逗号问题)传递时才需要这样做。

@echo off
setlocal enableDelayedExpansion

:: Get the PID and UID for this batch process
call :getMyPID

:: Initialize an existing PIDs list
set "PIDs= "

:: Get a list of all existing child processes, except for the
:: child CMD.EXE process that was created by this FOR /F loop.
:: This is necessary because the console session that this script
:: is running in might have previously created a child process.
for /f %%A in (
  '2^>nul wmic process where "ParentProcessID=%myPID% and not CommandLine like '%%<%UID%>%%'" get ProcessID'
) do for %%B in (%%A) do set "PIDs=!PIDs!%%B "

:: Create your new process as you normally would.
:: For this demonstration, I will simply create a new CMD.EXE session 
start

:: Get the PID of the newly created child process by getting all child PIDs,
:: except for the child CMD.EXE process that was created by this FOR /F loop.
:: Ignore any PID that already exists in the %PIDs% list. The only PID left
:: is your new process.
for /f %%A in (
  '2^>nul wmic process where "ParentProcessID=%myPID% and not CommandLine like '%%<%UID%>%%'" get ProcessID'
) do for %%B in (%%A) do if "!PIDs: %%B =!" equ "!PIDs!" set "PID=%%B"

:: At this point you could append the new PID to the PIDs list using
::   set "PIDs=!PIDs!%PID% "
:: Then you can repeat the steps of creating a new proces and getting the new PID
::
:: But instead, I will simply show the result and exit
echo new PID=%PID%

exit /b


:getMyPID 
setlocal disableDelayedExpansion

:getLock

:: Establish a nearly unique temp file name using %time%
set "lock=%temp%\%~nx0.%time::=.%.lock"

:: Transform the full path into a UID that can be used in a WMIC query
set "uid=%lock:\=:b%"
set "uid=%uid:,=:c%"
set "uid=%uid:'=:q%"
set "uid=%uid:_=:u%"
setlocal enableDelayedExpansion
set "uid=!uid:%%=:p!"
endlocal & set "uid=%uid%"


:: Establish an exclusive lock on the temp file
:: If this fails, then loop back and try again until success
:: This guaranees no two process will ever have the same UID
2>nul ( 9>"%lock%" (

  %= The FOR /F loops creates a child CMD.EXE process which in turn invokes WMIC.         =%
  %= The child CMD.EXE process contains the WMIC query with the UID in its command line.  =%
  %= The WMIC query identifies the CMD.EXE process with the UID in the command line,      =%
  %= and returns the parent process ID, which happens to be the PID for this batch script =%
  for /f "skip=1" %%A in (
    'wmic process where "name='cmd.exe' and CommandLine like '%%<%uid%>%%'" get ParentProcessID'
  ) do for %%B in (%%A) do set "PID=%%B"
  (call ) %= Guarantee success so we don't accidently loop again =%

))||goto :getLock

:: Delete the temp file
del "%lock%" 2>nul

:: Return the result
( endlocal
  set "myPID=%PID%"
  set "UID=%uid%"
)
exit /b

【讨论】:

  • 哦,好吧 - 非常感谢您付出了这么多努力来尝试找到解决方案,即使最终找不到解决方案。听起来您可能已经尝试了许多与我相同的转义序列!是时候看看其他选项了...
  • 编辑 - @user1447903 - 我发现了逗号问题的解决方案。它不漂亮,但它有效!
  • 我发现我可以在类似的 wmic 情况下使用 ^ 转义逗号,所以,变成了 ^,
  • @morechilli - 我在原始答案第 10 段中将转义 , 称为 ^,。但是在尝试使用WMIC PROCESS CALL CREATE 时它不起作用。因此,如果^, 为您工作,那么我不会称其为类似情况。
  • @dbebham 我的目标是鼓励将其作为其他人的第一个尝试解决方案,以防他们有幸使用它。我在 for 循环内的 WMIC PROCESS CALL CREATE 上下文中使用它 - 所以我相信我的情况相似但不完全相同。在这些情况下,被忽视的微小差异可能会导致最终行为的重大差异。
【解决方案2】:

我想我可以在这里再发表一些意见。在使用 WMI 创建进程时,PowerShell 似乎更能抵抗标记和符号的混淆。只需反引号转义嵌套引号。示例:

$addr = "rtsp://abcd"
$cmd = "cmd /k echo C:\PROGRA~1\VideoLAN\VLC_117\vlc.exe $addr --sout=`"#duplicate{dst=display, dst={std{access=file, mux=ps, dst='vlc_117/video.mpg'}}} --rtsp-caching=120 --no-video-title-show`""
$proc = ([wmiclass]'win32_process').create($cmd)

$proc.ProcessId

Windows 脚本宿主(JScript 和 VBScript)同样可以抵抗逗号问题。这是一个 Batch + JScript 混合脚本(以 .bat 扩展名保存)来演示:

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "addr=rtsp://abcd"
for /f %%I in ('cscript /nologo /e:JScript "%~f0" "%addr%"') do set "PID=%%I"
echo Process: %PID%
goto :EOF

@end // end Batch / begin JScript hybrid code
var wmi = GetObject("winmgmts:root\\cimv2"),
    proc = wmi.Get("Win32_Process").Methods_("Create").InParameters.SpawnInstance_();

proc.CommandLine = 'cmd /k echo C:\\PROGRA~1\\VideoLAN\\VLC_117\\vlc.exe '
    + WSH.Arguments(0)
    + ' --sout="#duplicate{dst=display, dst={std{access=file, mux=ps, dst=\'vlc_117/video.mpg\'}}} --rtsp-caching=120 --no-video-title-show"';

var exec = wmi.ExecMethod('Win32_Process', 'Create', proc);
WSH.Echo(exec.ProcessId);

对于 PowerShell 和 JScript 解决方案,删除 cmd /k echo 以将您的 vlc 命令投入生产。


RE:纯批处理,对于问题中引用的异常复杂的vlc 命令,我无法提供太多帮助,但这可能对其他人有所帮助。对于需要嵌入引号但不必处理逗号的不太复杂的命令,您可以在 wmic 命令中嵌入引号,方法是使用反斜杠对其进行转义。

wmic process call create "powershell \"test-connection localhost -count 1\""

如果这在您的情况下不起作用,您还可以尝试将整个值放入通过延迟扩展检索的变量中。示例:

set "proc=powershell \"test-connection localhost -count 1\""
setlocal enabledelayedexpansion
for /f "tokens=2 delims==;" %%I in (
    '2^>NUL wmic process call create "!proc!" ^| find "ProcessId"'
) do endlocal & set /a PID = %%I
echo Proc: %PID%

或者为了方便复用,可以放到子程序中:

@echo off & setlocal

set "pc=localhost"
call :run PID "powershell -window normal \"test-connection %pc% -count 1\""
echo Proc: %PID%
goto :EOF

:run <varname_to_return_PID> <command>
setlocal
set proc=%*
call set proc=%%proc:%1 =%%
setlocal enabledelayedexpansion
for /f "tokens=2 delims==;" %%I in (
    '2^>NUL wmic process call create !proc! ^| find "ProcessId"'
) do (endlocal & endlocal & set /a %~1 = %%I && goto :EOF)

不幸的是,这些纯批处理解决方案都不能解决逗号问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多