xp_cmdshell 独立运行 7z,但找不到带参数的 EXE

xp_cmdshell runs 7z standalone, but cannot find EXE with parms

当我如下调用 xp_cmdshell 时,7z.exe 实际运行,并且它的输出是正确的,因为没有包含参数:

declare @Command VARCHAR(1000)
set @Command = '"C:\Program Files-Zipz.exe" a '
exec master..xp_cmdshell @command
select @Command

输出:

output
NULL
7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30
NULL
NULL
NULL
Command Line Error:
Cannot find archive name
NULL

添加文件后,出现完全不同的错误:7z.exe 未找到:

declare @Command VARCHAR(1000)
set @Command = '"C:\Program Files-Zipz.exe" a ' + '"' + 'C:\TO_ERASE\ToZip.txt' + '"'
exec master..xp_cmdshell @command
select @Command

输出:

output
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
NULL

可能是什么问题?

唯一的区别是我添加了文件名作为参数。如果 7z.exe 产生错误,我会理解,但是 Windows 没有找到 exe 文件。

有趣的问题。我四处寻找关于文件名中 space 的各种解决方案,并遇到了 THIS。我不确定为什么你的第一个例子有效,但他们的解决方案是在路径之前添加一些东西,这样路径就不是命令中的第一件事。

declare @Command VARCHAR(1000)
set @Command = 'cd.. && "C:\Program Files-Zipz.exe" a ' + '"' + 'C:\TO_ERASE\ToZip.txt' + '"'
exec master..xp_cmdshell @command
select @Command

另一种可能是使用cd作为第一个命令然后直接调用程序,就像这个例子:

DECLARE @p_cmd varchar(255)

SET @p_cmd = 'cd "C:\Program Files\edb\mtk\bin" && runMTK.bat -dataOnly -tables table_changes -sourcedbtype sqlserver -targetdbtype postgresql -targetSchema public dbo'
EXEC xp_cmdshell @p_cmd