从 MSBuild Exec 任务运行 powershell 脚本时出现异常

发布于 2024-10-16 02:57:45 字数 1717 浏览 0 评论 0原文

我在使用 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 &quot;Scripts\SetSth.ps1&quot; -PARAM &quot;$(PathToSth)&quot; -SVNID $(Revision) -SVNFOLDER &quot;$(RepositoryPath)&quot;" LogStandardErrorAsError="true" ContinueOnError="false"/>
</Target>

该命令的执行方式与 CMD 上的执行方式完全相同,但我从 Powershell 脚本中得到了 SVNFOLDER 参数的异常。

执行的命令如下所示:

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

因此,从 CMD 中它可以工作,从 MSBuild 中则不行。我不知道为什么。我希望你有主意。

i´m having a problem with MSBuild and Powershell. There is a PS-script that i want to execute within the MSBuild exec-Task.

The Problem: Running the Script direct from CMD works, but running the script within MSBuild I get an error.

Here the MSBuild script:

<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>

The Command is executed exactly the same way as on CMD, but i get an exception from the Powershell Script for the SVNFOLDER param.

The Command that is executed looks like this:

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

So from CMD it works, from within MSBuild not. I have no idea why. I hope you got an idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-10-23 02:57:45

这种使用双引号和单引号的方法怎么样:

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

What about this approach playing with double and singles quotes:

<Target Name="SetProductVersion" DependsOnTargets="SvnInfo">
    <Exec Command="powershell -command "& {Scripts\SetSth.ps1 -PARAM '$(PathToSth)' -SVNID '$(Revision)' -SVNFOLDER '$(RepositoryPath)'}"" LogStandardErrorAsError="true" ContinueOnError="false"/>
</Target>
川水往事 2024-10-23 02:57:45

仔细检查你的路径。

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

假设 -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 记录它。然后您可以查看路径。

Double check your paths.

Remember, powershell invoked in this way runs as Msbuild.exe under whatever user is executing the build. To msbuild.exe, a straight call to cmd.exe is going to start in the working directory where msbuild lives.

Assume -file "Scripts\SetSth.ps1" references C:\users\yourusername\Scripts\SetSth.ps1

So for you, calling cmd.exe and running that may work just fine, b/c your working directory is going to match C:\users\yourusername

For msbuild.exe, its likely unable to find that file, as its starting in something like *C:\Windows\Microsoft.NET\Framework\v4.0*

So it's looking for C:\Windows\Microsoft.NET\Framework\v4.0\Scripts\SetSth.ps1

I would try making that file path fully qualified. If that still doesn't work, have cmd.exe dump its results into a property and have msbuild log it. Then you can review the paths.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文