2015-12-22 94 views
0

我试图从.cmd文件中运行一个生成任务,其中Powershell提取一个zip文件,这有助于绕过Visual Studio对目录字符数限制的问题。但是,我在使Powershell命令正确执行时遇到问题。我已经尝试了一些带有引号的变体,并且我得到一个终止错误,或者Powershell命令输出为一个字符串,而zip文件没有被提取。下面是我目前的.cmd文件的一个例子:从.cmd文件执行PowerShell的问题

//%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文件执行,这是由我们的应用程序构建过程触发的。如果需要,我很乐意提供更多信息。谢谢!

回答

0

这是一个奇怪的。上面的代码中有一些隐藏的字符。我拿了代码,并在记事本中打开它,保存为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

我敢打赌,这个这个信息,你是好去。