10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

各バージョンのMSBuildのパスを解決するPower Shellスクリプト

Last updated at Posted at 2019-01-25

TL;DR

  • 複数のバージョンのVisual Studioをインストールしている環境下において、使用したいバージョンのコマンドラインビルドに使用するMSBuild.exeを探すスクリプトを作成した

※ 本記事は、2019/01/25に弊社スタッフブログに投稿した記事をQiitaに転載しています。

(2019.01.26追記)
nukie_53さんから編集リクエストいただき、PowerShellスクリプトにシンタックスハイライトが適用されるように修正しました!

(2019.03.10追記)
下記バージョンのMSBuild.exeのパスを追加

  • Visual Studio 2013
  • Visual Studio 2017 Professional

参照URL

概要

かなりニッチな状況ですが、

  • Visual Studioで作成したツール類をコマンドラインからビルドしたい
  • Visual Studioのバージョンは特に指定しないけど、特定バージョン以降じゃないとビルドが通らない
  • ビルドする環境が32bit/64bit混在
    という状況になった際に、Power Shellスクリプトで解決したので、まとめておきます。

解説

スクリプト

スクリプトは、下記よりダウンロードできます。

https://github.com/rot-z/MSBuildSearch

スクリプト起動用のバッチ

Power Shellはデフォルト設定だと実行が禁止されていますので、権限指定してバッチファイルから指定します。

@echo off

cd /d %~dp0

powershell -NoProfile -ExecutionPolicy Unrestricted .\MSBuilsSearch_impl.ps1

pause

MSBuild.exeを探すPower Shellスクリプト

単純に、あらかじめリストで持っておいた"Program Files"フォルダ配下のパスにMSBuild.exeを探しています。
"Program Files"フォルダはOSのアーキテクチャによって"Program Files"(32bit), "Program Files (x86)"(64bit)と名前が異なりますので、そのあたりは勝手に解決しています。

# =============================================================================
#     search MSBuild.exe 
# =============================================================================

# folder paths of Visual Studio
$MSBUILD_12_PATH = "MSBuild`\12.0`\Bin"                                                             # Visual Studio 2013
$MSBUILD_14_PATH = "MSBuild`\14.0`\Bin"                                                             # Visual Studio 2015
$MSBUILD_15_COMMUNITY_PATH = "Microsoft Visual Studio`\2017`\Community`\MSBuild`\15.0`\Bin"         # Visual Studio 2017 Community
$MSBUILD_15_PROFESSIONAL_PATH = "Microsoft Visual Studio`\2017`\Professional`\MSBuild`\15.0`\Bin"   # Visual Studio 2017 Professional

# target paths for MSBuild
# sort by priority
[array]$SEARCH_PATHS = @(
        $MSBUILD_14_PATH, 
        $MSBUILD_15_COMMUNITY_PATH,
        $MSBUILD_15_PROFESSIONAL_PATH,
        $MSBUILD_12_PATH
    )

# get full path of "Program Files" folder from OS archtechture
$arch = (Get-WmiObject win32_operatingsystem | Select-Object osarchitecture)
$archName = $arch.osarchitecture
$programFilesDir = ""
if ($archName.StartsWith("64"))
{
    $programFilesDir = ${env:ProgramFiles(x86)}
}
else
{
    $programFilesDir = ${env:ProgramFiles}
}

# search MSBuild.exe
$msbuildPath = ""
foreach($p in $SEARCH_PATHS)
{
    # is folder exists?
    $targetPath = Join-Path $programFilesDir $p
    if (!(Test-Path $targetPath)) 
    {
        continue
    }

    # select the most shortest (shallowest) path
    $results = (Get-ChildItem $targetPath -Include MSBuild.exe -Recurse).FullName | Sort-Object -Property Length
    if ($results.Length -gt 0)
    {
        $msbuildPath = $results[0]
        Write-host $msbuildPath
    }
}

無償版のパスしか埋めてないのは、手元の環境で調査したからです。
適宜自分の環境に合わせてパスの追加を行ってください。

10
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?