Introduction

ビルドスクリプトを作成するとき、面倒なのが devenv.exemsbuild.exe のパスを調べること。
Enterpise とか Community とか場合分けしてたらキリがない。

Resolution

vswhere を使います。
Microsoft 公式ツールで、 Visual Studio 2017 以降は標準でついているのは知っていたけど、OSS になっていたのは知りませんでした。

https://github.com/microsoft/vswhere

おもむろにダウンロードして実行すると

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
> vswhere.exe
Visual Studio Locator version 2.9.3+21bcdb639c [query version 2.7.3111.17308]
Copyright (C) Microsoft Corporation. All rights reserved.

instanceId: 5d4ea00a
installDate: 2019/04/06 3:54:16
installationName: VisualStudio/16.11.3+31702.278
installationPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
installationVersion: 16.11.31702.278
productId: Microsoft.VisualStudio.Product.Community
productPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe
state: 4294967295
isComplete: 1
isLaunchable: 1
isPrerelease: 0
isRebootRequired: 0
displayName: Visual Studio Community 2019
description: 学生、オープンソースの共同作成者、個人用の無料で強力な IDE
channelId: VisualStudio.16.Release
channelUri: https://aka.ms/vs/16/release/channel
enginePath: C:\Program Files (x86)\Microsoft Visual Studio\Installer\resources\app\ServiceHub\Services\Microsoft.VisualStudio.Setup.Service
releaseNotes: https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes-v16.11#16.11.3
thirdPartyNotices: https://go.microsoft.com/fwlink/?LinkId=660909
updateDate: 2021-09-15T04:19:12.2565159Z
catalog_buildBranch: d16.11
catalog_buildVersion: 16.11.31702.278
catalog_id: VisualStudio/16.11.3+31702.278
catalog_localBuild: build-lab
catalog_manifestName: VisualStudio
catalog_manifestType: installer
catalog_productDisplayVersion: 16.11.3
catalog_productLine: Dev16
catalog_productLineVersion: 2019
catalog_productMilestone: RTW
catalog_productMilestoneIsPreRelease: False
catalog_productName: Visual Studio
catalog_productPatchVersion: 3
catalog_productPreReleaseMilestoneSuffix: 1.0
catalog_productSemanticVersion: 16.11.3+31702.278
catalog_requiredEngineVersion: 2.11.40.25675
properties_campaignId: 1374116220.1523469529
properties_channelManifestId: VisualStudio.16.Release/16.11.3+31702.278
properties_nickname:
properties_setupEngineFilePath: C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installershell.exe

instanceId: d8cf53b0
installDate: 2018/04/12 16:54:05
installationName: VisualStudio/15.9.39+28307.1684
installationPath: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community
installationVersion: 15.9.28307.1684
productId: Microsoft.VisualStudio.Product.Community
productPath: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\devenv.exe
state: 4294967295
isComplete: 1
isLaunchable: 1
isPrerelease: 0
isRebootRequired: 0
displayName: Visual Studio Community 2017
description: 学生、オープン ソース、および個々の開発者のための無料で完全な機能を備えた IDE
channelId: VisualStudio.15.Release
channelPath: C:\Users\TAKUYA\AppData\Local\Microsoft\VisualStudio\Packages\_Channels\4CB340F5\catalog.json
channelUri: https://aka.ms/vs/15/release/channel
enginePath: C:\Program Files (x86)\Microsoft Visual Studio\Installer\resources\app\ServiceHub\Services\Microsoft.VisualStudio.Setup.Service
releaseNotes: https://go.microsoft.com/fwlink/?LinkId=660692#15.9.39
thirdPartyNotices: https://go.microsoft.com/fwlink/?LinkId=660708
updateDate: 2021-09-15T04:14:47.1648249Z
catalog_buildBranch: d15.9
catalog_buildVersion: 15.9.28307.1684
catalog_id: VisualStudio/15.9.39+28307.1684
catalog_localBuild: build-lab
catalog_manifestName: VisualStudio
catalog_manifestType: installer
catalog_productDisplayVersion: 15.9.39
catalog_productLine: Dev15
catalog_productLineVersion: 2017
catalog_productMilestone: RTW
catalog_productMilestoneIsPreRelease: False
catalog_productName: Visual Studio
catalog_productPatchVersion: 39
catalog_productPreReleaseMilestoneSuffix: 1.0
catalog_productRelease: RTW
catalog_productSemanticVersion: 15.9.39+28307.1684
catalog_requiredEngineVersion: 1.18.1063.29791
properties_campaignId: 1768719327.1523469536
properties_canceled: 0
properties_channelManifestId: VisualStudio.15.Release/15.9.39+28307.1684
properties_nickname:
properties_setupEngineFilePath: C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installershell.exe

たくさん出てきます。

MSBuild.exe は?

本題。
ここは Powershell を使いましょう。
ダウンロードしてきた vswhere.exe がある場所で実行します。

1
2
3
4
$vswhere = Resolve-Path("vswhere.exe")
$msbuild = & "${vswhere}" -latest -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe | select-object -first 1
Write-Host "[Info] ${vswhere} is found" -ForegroundColor Green
Write-Host "[Info] ${msbuild} is found" -ForegroundColor Green

これを叩くと

1
2
[Info] D:\tools\vswhere.exe is found
[Info] C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe is found

こうなります。