2011-02-06 56 views
3

我有MSBuild和Powershell的问题。有一个PS脚本,我想要在MSBuild exec-Task中执行。从MSBuild Exec运行powershell脚本的异常任务

问题:直接从CMD运行脚本,但在MSBuild内运行脚本时出现错误。

这里的MSBuild脚本:

<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/> 
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/> 

<PropertyGroup> 
    <PathToSvnClient>C:\Program Files (x86)\CollabNet\Subversion Client</PathToSvnClient> 
</PropertyGroup> 

<ItemGroup> 
    <!-- set Folder to Svn Repository for svn info command--> 
    <SvnFolder Include="$(MSBuildProjectDirectory)\.."/> 
</ItemGroup> 

<Target Name="SvnInfo"> 
    <!-- get SVN Revision and Repository Path --> 
    <SvnInfo LocalPath="%(SvnFolder.FullPath)" ToolPath="$(PathToSvnClient)"> 
     <Output TaskParameter="Revision" PropertyName="Revision" /> 
     <Output TaskParameter="RepositoryPath" PropertyName="RepositoryPath" /> 
    </SvnInfo> 
</Target> 

<Target Name="SetProductVersion" DependsOnTargets="SvnInfo"> 
    <Exec Command="powershell -file &quot;Scripts\SetSth.ps1&quot; -PARAM &quot;$(PathToSth)&quot; -SVNID $(Revision) -SVNFOLDER &quot;$(RepositoryPath)&quot;" LogStandardErrorAsError="true" ContinueOnError="false"/> 
</Target> 

命令正好执行相同的方式,在CMD,但我从PowerShell脚本异常的SVNFOLDER PARAM 。

所执行的命令如下:

powershell -file "Scripts\SetSth.ps1" -PARAM "C:\abc\cde" -SVNID 1234 
-SVNFOLDER "https://domain/svn/rep/branches/xy%20(Build%2012)" 

因此,从CMD它的工作原理,从内部的MSBuild没有。我不知道为什么。我希望你有个想法。

回答

1

仔细检查你的路径。

请记住,以这种方式调用的powershell作为Msbuild.exe在任何用户执行构建下运行。对于msbuild.exe,直接调用cmd.exe将在msbuild所在的工作目录中开始。

假设-file "Scripts\SetSth.ps1"引用C:\用户\ yourusername \脚本\ SetSth.ps1

那么对你来说,调用CMD.EXE并运行,可能工作得很好,B/C你的工作目录是要比赛C:\用户\ yourusername

对于msbuild.exe,它可能无法找到该文件,因为它在类似* 初始的C:\ WINDOWS \ Microsoft.NET \框架\ V4。 0 *

所以它寻找C:\ WINDOWS \ Microsoft.NET \框架\ V4.0 \脚本\ SetSth.ps1

我会尝试做的是文件路径完全合格。如果仍然不起作用,请将cmd.exe的结果转储到属性中,并让msbuild将其记录下来。然后你可以查看路径。

+0

谢谢您的回答。但脚本开始,然后引发异常。所以它是。其他。目前我正在查看SVNFOLDER路径的“%20”。我假设MSBuild将其替换为一个项目?! – BoBBuilder 2011-02-06 13:51:42

3

什么对这种做法有双重单打引号玩:

<Target Name="SetProductVersion" DependsOnTargets="SvnInfo"> 
    <Exec Command="powershell -command &quot;&amp; {Scripts\SetSth.ps1 -PARAM '$(PathToSth)' -SVNID '$(Revision)' -SVNFOLDER '$(RepositoryPath)'}&quot;" LogStandardErrorAsError="true" ContinueOnError="false"/> 
</Target>