简体   繁体   中英

Exception running powershell script from MSBuild Exec Task

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 &quot;Scripts\SetSth.ps1&quot; -PARAM &quot;$(PathToSth)&quot; -SVNID $(Revision) -SVNFOLDER &quot;$(RepositoryPath)&quot;" 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.

What about this approach playing with double and singles quotes:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM