Code/Import-VisualStudioEnvironment.ps1
#requires -version 2.0 ############################################################################### # WintellectPowerShell Module # Copyright (c) 2010-2014 - John Robbins/Wintellect # # Do whatever you want with this module, but please do give credit. ############################################################################### # Always make sure all variables are defined and all best practices are # followed. Set-StrictMode -version Latest ############################################################################### # Public Cmdlets ############################################################################### function Import-VisualStudioEnvironment { <# .SYNOPSIS Sets up the current PowerShell instance with the Visual Studio environment variables so you can use those tools at the command line. .DESCRIPTION Command line usage is the way to go, but Visual Studio requires numerous environment variables set in order to properly work. Since those are controlled by the vcvarsall.bat cmd script, it's a pain to get working. This script does the work of calling the specific vcvarsall.bat file for the specific version of Visual Studio you want to use. If the vcvarsall.bat file does not exist, which happens if you do not install the C++ tools, this cmdlet will use vsdevcmd.bat instead. This implementation uses the registry to look up the installed Visual Studio versions and does not rely on any preset environment variables such as VS110COMNTOOLS. .PARAMETER VSVersion The version of Visual Studio you want to use. If left to the default, Latest, the script will look for the latest version of Visual Studio installed on the computer as the tools to use. Specify 2008, 2010, 2012, 2013, or 2015 for a specific version. .PARAMETER Architecture The tools architecture to use. This defaults to the $env:PROCESSOR_ARCHITECTURE environment variable so x86 and x64 are automatically handled. The valid architecture values are x86, amd64, x64, arm, x86_arm, and x86_amd64. If the C++ tools were not installed, this parameter is ignored because vsdevcmd.bat only sets the environment for 32-bit tools. .LINK http://www.wintellect.com/blogs/jrobbins https://github.com/Wintellect/WintellectPowerShell #> param ( [Parameter(Position=0)] [ValidateSet("Latest", "2008", "2010", "2012", "2013", "2015")] [string] $VSVersion = "Latest", [Parameter(Position=1)] [ValidateSet("x86", "amd64", "x64", "arm", "x86_arm", "x86_amd64")] [string] $Architecture = ($Env:PROCESSOR_ARCHITECTURE) ) $versionSearchKey = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7" if ([IntPtr]::size -ne 8) { $versionSearchKey = "HKLM:\SOFTWARE\Microsoft\VisualStudio\SxS\VS7" } $vsDirectory = "" if ($VSVersion -eq 'Latest') { # Find the largest number in the install lookup directory and that will # be the latest version. $biggest = 0.0 Get-RegistryKeyPropertiesAndValues $versionSearchKey | ForEach-Object { if ([System.Convert]::ToDecimal($_.Property) -gt [System.Convert]::ToDecimal($biggest)) { $biggest = $_.Property $vsDirectory = $_.Value } } } else { $propVal = switch($VSVersion) { "2008" { "9.0" } "2010" { "10.0" } "2012" { "11.0" } "2013" { "12.0" } "2015" { "14.0" } default { throw "Unknown version of Visual Studio!" } } if (Test-PathReg -Path $versionSearchKey -Property $propVal) { $vsDirectory = (Get-ItemProperty -Path $versionSearchKey -WarningAction SilentlyContinue).$propVal } else { $vsDirectory = $null } } if ([String]::IsNullOrEmpty($vsDirectory)) { throw "The requested Visual Studio version is not installed" } # Turns out some people install VS without the C++ tools. Crazy, I know! if (Test-Path -Path "$vsDirectory\vc\vcvarsall.bat") { # Got the VS directory, now setup to make the call. Invoke-CmdScript -script "$vsDirectory\vc\vcvarsall.bat" -parameters "$Architecture" } else { Write-Verbose -Message "Falling back to VSDEVCMD.BAT, which only supports the 32-bit tools." # Use VsDevCmd.bat instead. Invoke-CmdScript -script "$vsDirectory\Common7\Tools\VsDevCmd.bat" } } ############################################################################### # SIG # Begin signature block # MIIYTQYJKoZIhvcNAQcCoIIYPjCCGDoCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU2oW7nkMa6jumrS+MWm8oSGIB # 5YmgghM9MIIEhDCCA2ygAwIBAgIQQhrylAmEGR9SCkvGJCanSzANBgkqhkiG9w0B # AQUFADBvMQswCQYDVQQGEwJTRTEUMBIGA1UEChMLQWRkVHJ1c3QgQUIxJjAkBgNV # BAsTHUFkZFRydXN0IEV4dGVybmFsIFRUUCBOZXR3b3JrMSIwIAYDVQQDExlBZGRU # cnVzdCBFeHRlcm5hbCBDQSBSb290MB4XD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