4

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.

0

2 Answers 2

4

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

Thanks for your answer. But the script gets started and then raises an exception. So it has be sth. else. Currently i´m looking at the "%20" of the SVNFOLDER Path. I assume that MSBuild trys to substitute it with an item?!
It shouldn't, msbuild uses %% syntax just like any xml-based system to parse special characters. What you may wanna try is building your command string in a Property, then outputing that property in the log and see if it matches what you expect
It´s strange. If i run the MSBuild script it´s writing the target name and the command "powershell -file "Scripts\SetSth.ps1" -PARAM "C:\abc\cde" -SVNID 1234 -SVNFOLDER "domain/svn/rep/branches/xy%20(Build%2012)"" but powershell gets "powershell -file "Scripts\SetSth.ps1" -PARAM "C:\abc\cde" -SVNID 1234 -SVNFOLDER "domain/svn/rep/branches/xy0(Build012)"" so the "%20" is substituted with "0" and that is not what i expect in the script. (i do a match and replace in there). The question now is, why tells me MSBuild that it uses the "%20" and not "0"??
What are you using the "0" for? If its just to trigger a find replace, can you use something that isn't an ASCII short code? MSBUILD is XML so %20 is a valid code, vs just some string. You'd prob need to use [[CDATA to pass it untouched ... but if you're just looking to trigger a "hey, substitute here", can you use something else like --**-- ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.