![](/img/trans.png)
[英]MSBuild Running Slow Called from PowerShell Script (Visual Studio 2019)
[英]Exception running powershell script from MSBuild Exec Task
我遇到了MSBuild和Powershell的问题。 我想在MSBuild exec-Task中执行一个PS脚本。
问题:直接从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 "Scripts\SetSth.ps1" -PARAM "$(PathToSth)" -SVNID $(Revision) -SVNFOLDER "$(RepositoryPath)"" LogStandardErrorAsError="true" ContinueOnError="false"/>
</Target>
Command的执行方式与CMD完全相同,但是我从SVNFOLDER参数的Powershell脚本中获得了一个例外。
执行的命令如下所示:
powershell -file "Scripts\SetSth.ps1" -PARAM "C:\abc\cde" -SVNID 1234
-SVNFOLDER "https://domain/svn/rep/branches/xy%20(Build%2012)"
所以从CMD它起作用,从MSBuild内部不起作用。 我不知道为什么。 我希望你有个主意。
使用双引号和单引号的方法怎么样:
<Target Name="SetProductVersion" DependsOnTargets="SvnInfo">
<Exec Command="powershell -command "& {Scripts\SetSth.ps1 -PARAM '$(PathToSth)' -SVNID '$(Revision)' -SVNFOLDER '$(RepositoryPath)'}"" LogStandardErrorAsError="true" ContinueOnError="false"/>
</Target>
仔细检查你的路径。
请记住,以这种方式调用的powershell在任何正在执行构建的用户下运行为Msbuild.exe。 对于msbuild.exe,直接调用cmd.exe将在msbuild所在的工作目录中启动。
假设-file "Scripts\\SetSth.ps1"
引用C:\\ users \\ yourusername \\ Scripts \\ SetSth.ps1
所以对你来说,调用cmd.exe并运行它可以正常工作,b / c你的工作目录将匹配C:\\ users \\ yourusername
对于msbuild.exe,它可能无法找到该文件,因为它的起始类似* C:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0 *
所以它正在寻找C:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0 \\ Scripts \\ SetSth.ps1
我会尝试使该文件路径完全合格。 如果仍然无效,请让cmd.exe将其结果转储到属性中并让msbuild将其记录下来。 然后,您可以查看路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.