簡體   English   中英

從MSBuild Exec Task運行powershell腳本的異常

[英]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 &quot;Scripts\SetSth.ps1&quot; -PARAM &quot;$(PathToSth)&quot; -SVNID $(Revision) -SVNFOLDER &quot;$(RepositoryPath)&quot;" 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 &quot;&amp; {Scripts\SetSth.ps1 -PARAM '$(PathToSth)' -SVNID '$(Revision)' -SVNFOLDER '$(RepositoryPath)'}&quot;" 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM