Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current operating system in MSBuild?

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?

like image 591
Chedy2149 Avatar asked Apr 19 '17 14:04

Chedy2149


People also ask

How do I find MSBuild version?

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.

What is MSBuild command line?

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.

How do I create a project using MSBuild command line?

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.


2 Answers

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.

like image 159
Kevinoid Avatar answered Sep 22 '22 00:09

Kevinoid


The variable is $(OS), usually checked against being or not being Windows_NT:

<Exec Command="./foo.sh" Condition=" '$(OS)' != 'Windows_NT' " />
like image 21
Martin Ullrich Avatar answered Sep 21 '22 00:09

Martin Ullrich