【问题标题】:xp_cmdshell runs 7z standalone, but cannot find EXE with parmsxp_cmdshell 独立运行 7z,但找不到带参数的 EXE
【发布时间】:2018-10-31 07:05:57
【问题描述】:

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

declare @Command VARCHAR(1000)
set @Command = '"C:\Program Files\7-Zip\7z.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 is not found:

declare @Command VARCHAR(1000)
set @Command = '"C:\Program Files\7-Zip\7z.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 文件。

【问题讨论】:

    标签: sql-server-2008 tsql windows-server-2008


    【解决方案1】:

    另一种可能是使用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
    

    【讨论】:

      【解决方案2】:

      有趣的问题。我四处寻找有关文件名中空格的各种解决方案,并遇到了 THIS。我不确定为什么您的第一个示例有效,但他们的解决方案是在路径之前添加一些内容,以便路径不是命令中的第一件事。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-23
        • 2020-10-06
        • 2017-02-16
        • 2013-05-19
        • 1970-01-01
        • 2017-03-07
        • 2018-04-10
        • 1970-01-01
        相关资源
        最近更新 更多