I have a .NET Core 1.1 console project created with Visual Studio 2017.
After building the project, I need to run a powershell script so I added the following to the MyProject.csproj
file:
<Target Name="PostcompileScript" AfterTargets="Build">
<Exec Command="execute-tasks.ps1" />
</Target>
Now I need to build the project in a Linux environment, I need to instruct MSBuild to run execute-tasks.sh
instead of execute-tasks.ps1
when it is running in Linux.
I believe this is achievable via the Condition
attribute, but is there an MSBuild variable that holds the operating system name?
To install MSBuild on a system that doesn't have Visual Studio, go to Build Tools for Visual Studio 2022 on the downloads page. Another way of getting MSBuild is to install the . NET SDK. You want to use the 64-bit version of MSBuild, and you're using Visual Studio 2019 or earlier.
Builds the targets in the project file that you specify. If you don't specify a project file, MSBuild searches the current working directory for a file name extension that ends in proj and uses that file. You can also specify a Visual Studio solution file for this argument.
To build a specific target of a specific project in a solution. At the command line, type MSBuild.exe <SolutionName>. sln , where <SolutionName> corresponds to the file name of the solution that contains the target that you want to execute.
Using $(OS)
, as documented in other answers, can distinguish between Windows_NT
and Unix
(which includes both Linux and macOS), but not between different Unix-like systems. If you are using MSBuild 15.3 or later (which is very likely), you may want to consider using [MSBuild]::IsOsPlatform()
:
<Exec Command="./foo.sh" Condition="$([MSBuild]::IsOSPlatform('Linux'))" />
The argument can be the name of any OsPlatform
member.
The variable is $(OS)
, usually checked against being or not being Windows_NT
:
<Exec Command="./foo.sh" Condition=" '$(OS)' != 'Windows_NT' " />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With