<?xml version="1.0" encoding="utf-8"?> | |
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<!-- #1 Place this line at the top of any msbuild script (ie, csproj, etc) --> | |
<PropertyGroup><PowerShell># 2>nul || type %~df0|find /v "setlocal"|find /v "errorlevel"|powershell.exe -noninteractive -& exit %errorlevel% || #</PowerShell></PropertyGroup> | |
<!-- #2 in any target you want to run a script --> | |
<Target Name="default" > | |
<PropertyGroup> <!-- #3 prefix your powershell script with the $(PowerShell) variable, then code as normal! --> | |
<myscript>$(PowerShell) | |
# | |
# powershell script can do whatever you need. | |
# | |
dir ".\*.cs" -recurse |% { | |
write-host Examining file named: $_.FullName | |
# do other stuff here... | |
} | |
$answer = 2+5 | |
write-host Answer is $answer ! | |
</myscript> | |
</PropertyGroup> | |
<!-- #4 and execute the script like this --> | |
<Exec Command="$(myscript)" EchoOff="true" /> | |
<!-- | |
Notes: | |
====== | |
- You can still use the standard Exec Task features! (see: https://msdn.microsoft.com/en-us/library/x8zx72cd.aspx) | |
- if your powershell script needs to use < > or & characters, just place the contents in a CDATA wrapper: | |
<script2><![CDATA[ $(PowerShell) | |
# your powershell code goes here! | |
write-host "<<Hi mom!>>" | |
]]></script2> | |
- if you want return items to the msbuild script you can get them: | |
<script3>$(PowerShell) | |
# your powershell code goes here! | |
(dir "*.cs" -recurse).FullName | |
</script3> | |
<Exec Command="$(script3)" EchoOff="true" ConsoleToMSBuild="true"> | |
<Output TaskParameter="ConsoleOutput" PropertyName="items" /> | |
</Exec> | |
<Touch Files="$(items)" /> <- see! then you can use those items with another msbuild Task | |
--> | |
</Target> | |
</Project> |
This comment has been minimized.
This comment has been minimized.
This was a massive help! Whenever i tried running powershell scripts another way notepad just opened with the contents of the script!?!? |
This comment has been minimized.
This comment has been minimized.
I must say that if this approach works (which I assume it does) then I owe you a big thank you. Being able to define the script inside the csproj itself is just pure awesomeness. |
This comment has been minimized.
This comment has been minimized.
This is great - do you know how to reference other variables from within the script? For example, accessing the
|
This comment has been minimized.
This comment has been minimized.
did you try |
This comment has been minimized.
This comment has been minimized.
I found this to be extremely helpful ! However, I ran through some limitations with the PowerShell command. Those limitations are:
Now, that being said, I used your scheme and changed the
With this improved PowerShell command, I was able to fix all the limitations previously mentioned, I thought I'd share it with you since the original command found here really helped be grasp how MsBuild was actually calling PowerShell. |
This comment has been minimized.
This comment has been minimized.
I use it this way. |
This comment has been minimized.
This is amazing. Can you explain the value of the PowerShell property? What does the leading # mean to CMD? And how might one get something like this to work using dotnet core on a non-windows platform (to invoke PowerShell Core from bash platforms?) I have come up with the following which seems close but is giving me strange, erratic errors from PowerShell: