【问题标题】:Problems Executing Powershell from .cmd file从 .cmd 文件执行 Powershell 时出现问题
【发布时间】:2015-12-23 17:15:55
【问题描述】:

我正在尝试从 Powershell 提取 zip 文件的 .cmd 文件运行构建任务,这有​​助于绕过 Visual Studio 对目录字符数的限制问题。但是,我无法正确执行 Powershell 命令。我用引号尝试了许多变体,我得到一个终止错误,或者 Powershell 命令输出为一个字符串,其中未提取 zip 文件。以下是我当前的 .cmd 文件的示例:

//%1% is a passed in command line argument for the absolute path, e.g. C:\path\to\dir
set Source=%1%directory.zip
set Destination=%1%directory
powershell -Command $pscmd = '{Add-Type -assembly "system.io.compression.filesystem";[io.compression.zipfile]::ExtractToDirectory("%Source%", "%Destination%");}'; Write-Host $pscmd;

我非常愿意接受许多可以使其工作的变体,只要此任务在命令行上运行,使用 Powershell,并且可以从 .cmd 文件执行,该文件由我们的应用程序构建触发过程。如果需要,我很乐意提供更多信息。谢谢!

【问题讨论】:

    标签: windows powershell cmd unzip


    【解决方案1】:

    这很奇怪。您上面的代码中有某种隐藏字符。我拿了代码并在记事本中打开它,保存为 ANSI,当你在命令行中键入它或在记事本的新实例中再次打开它时,你会看到错误。

    add-type 和 ExtractToDirectory 都没有输出,所以我删除了你的 pscmd var。

    我会打开您现有的脚本,另存为 ansi 作为新文件名,删除原始文件,将新文件重命名为原始名称。

    这是我想出的用于解决您的脚本问题的方法,它可以在我的机器上运行。

    我将我的脚本命名为 L:\util\unzip.cmd

    setlocal
    //%1% is a passed in command line argument for the absolute path, e.g. C:\path\to\dir
    set _Source='%1\directory.zip'
    set _Destination='%1\directory'
    echo _Source=%_Source%
    echo _Destination=%_Destination%
    set _c1=Add-Type -assembly system.io.compression.filesystem;
    set _c2=[io.compression.zipfile]::ExtractToDirectory(%_Source%, %_Destination%)
    echo _c1=%_c1%
    echo _c2=%_c2%
    set _Command=^& {%_c1% %_c2%};
    : to echo, use double quotes or cmd thinks text after & is another command
    echo _Command="%_Command%"
    pause
    
    powershell -Command "%_Command%"
    
    endlocal
    

    我是这样运行的,它成功了:unzip.cmd L:\util

    我敢打赌这个信息,你很高兴。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      相关资源
      最近更新 更多