Last active
September 22, 2023 20:36
-
-
Save sharadhr/7e7e52d4e1c8120a5fcde183db127aa2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS Launch Developer PowerShell | |
.DESCRIPTION | |
Locates and imports a Developer PowerShell module and calls the Enter-VsDevShell cmdlet. The Developer PowerShell module | |
is located in one of several ways: | |
1) From a path in a Visual Studio installation | |
2) From the latest installation of Visual Studio (higher versions first) | |
3) From the instance ID of a Visual Studio installation | |
4) By selecting a Visual Studio installation from a list | |
By default, with no parameters, the path to this script is used to locate the Developer PowerShell module. If that fails, | |
then the latest Visual Studio installation is used. If both methods fail, then the user can select a Visual Studio installation | |
from a list. | |
.PARAMETER VsInstallationPath | |
A path in a Visual Studio installation. The path is used to locate the Developer PowerShell module. | |
By default, this is the path to this script. | |
.PARAMETER Latest | |
Use the latest Visual Studio installation to locate the Developer PowerShell module. | |
.PARAMETER List | |
Display a list of Visual Studio installations to choose from. The choosen installation is used to locate the Developer PowerShell module. | |
.PARAMETER VsInstanceId | |
A Visual Studio installation instance ID. The matching installation is used to locate the Developer PowerShell module. | |
.PARAMETER ExcludePrerelease | |
Excludes Prerelease versions of Visual Studio from consideration. Applies only to Latest and List. | |
.PARAMETER VsWherePath | |
Path to the vswhere utility used to located and identify Visual Studio installations. | |
By default, the path is the well-known location shared by Visual Studio installations. | |
#> | |
[CmdletBinding(DefaultParameterSetName = "Default")] | |
param ( | |
[ValidateScript({Test-Path $_})] | |
[Parameter(ParameterSetName = "VsInstallationPath")] | |
[string] | |
$VsInstallationPath = "$($MyInvocation.MyCommand.Definition)", | |
[Parameter(ParameterSetName = "Latest")] | |
[switch] | |
$Latest, | |
[Parameter(ParameterSetName = "List")] | |
[switch] | |
$List, | |
[Parameter(ParameterSetName = "List")] | |
[object[]] | |
$DisplayProperties = @("displayName", "instanceId", "installationVersion", "isPrerelease", "installationName", "installDate"), | |
[Parameter(ParameterSetName = "VsInstanceId", Mandatory = $true)] | |
[string] | |
$VsInstanceId, | |
[Parameter(ParameterSetName = "Latest")] | |
[Parameter(ParameterSetName = "List")] | |
[switch] | |
$ExcludePrerelease, | |
[Parameter(ParameterSetName = "Default")] | |
[Parameter(ParameterSetName = "VsInstallationPath")] | |
[Parameter(ParameterSetName = "Latest")] | |
[Parameter(ParameterSetName = "List")] | |
[Parameter(ParameterSetName = "VsInstanceId")] | |
[ValidateSet('x86','amd64','arm','arm64')] | |
[string] | |
$Arch, | |
[Parameter(ParameterSetName = "Default")] | |
[Parameter(ParameterSetName = "VsInstallationPath")] | |
[Parameter(ParameterSetName = "Latest")] | |
[Parameter(ParameterSetName = "List")] | |
[Parameter(ParameterSetName = "VsInstanceId")] | |
[ValidateSet('x86','amd64')] | |
[string] | |
$HostArch, | |
[Parameter(ParameterSetName = "Default")] | |
[Parameter(ParameterSetName = "VsInstallationPath")] | |
[Parameter(ParameterSetName = "Latest")] | |
[Parameter(ParameterSetName = "List")] | |
[Parameter(ParameterSetName = "VsInstanceId")] | |
[switch] | |
$SkipAutomaticLocation, | |
[ValidateScript({Test-Path $_ -PathType 'Leaf'})] | |
[Parameter(ParameterSetName = "Default")] | |
[Parameter(ParameterSetName = "VsInstallationPath")] | |
[Parameter(ParameterSetName = "Latest")] | |
[Parameter(ParameterSetName = "List")] | |
[Parameter(ParameterSetName = "VsInstanceId")] | |
[string] | |
$VsWherePath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" | |
) | |
function GetSetupConfigurations { | |
param ( | |
$whereArgs | |
) | |
$expression = "& `"$VsWherePath`" $whereArgs -format json" | |
Invoke-Expression $expression | ConvertFrom-Json | |
} | |
function LaunchDevShell { | |
param ( | |
$config | |
) | |
$basePath = $config.installationPath | |
$instanceId = $config.instanceId | |
$currModulePath = "$basePath\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" | |
# Prior to 16.3 the DevShell module was in a different location | |
$prevModulePath = "$basePath\Common7\Tools\vsdevshell\Microsoft.VisualStudio.DevShell.dll" | |
$modulePath = if (Test-Path $prevModulePath) { $prevModulePath } else { $currModulePath } | |
if (Test-Path $modulePath) { | |
Write-Verbose "Found at $modulePath." | |
try { | |
Import-Module $modulePath | |
} | |
catch [System.IO.FileLoadException] { | |
Write-Verbose "The module has already been imported from a different installation of Visual Studio:" | |
(Get-Module Microsoft.VisualStudio.DevShell).Path | Write-Verbose | |
} | |
$params = @{ | |
VsInstanceId = $instanceId | |
} | |
$command = Get-Command Enter-VsDevShell | |
if ($SkipAutomaticLocation) | |
{ | |
$params.SkipAutomaticLocation = $true | |
} | |
# -Arch is only available from 17.1 | |
if ($Arch -and $command.Parameters.ContainsKey("Arch")) | |
{ | |
$params.Arch = $Arch | |
} | |
# -HostArch is only available from 17.1 | |
if ($HostArch -and $command.Parameters.ContainsKey("HostArch")) | |
{ | |
$params.HostArch = $HostArch | |
} | |
# -ReportNewInstanceType is only available from 16.5 | |
if ($command.Parameters.ContainsKey("ReportNewInstanceType")) { | |
$params.ReportNewInstanceType = "LaunchScript" | |
} | |
$boundParams = $PSCmdlet.MyInvocation.BoundParameters | |
if ($boundParams.ContainsKey("Verbose") -and | |
$boundParams["Verbose"].IsPresent) | |
{ | |
Write-Verbose "Enter-VsDevShell Parameters:" | |
$params.GetEnumerator() | ForEach-Object{ | |
$message = '{0} = {1}' -f $_.key, $_.value | |
Write-Verbose $message | |
} | |
} | |
Enter-VsDevShell @params | |
exit | |
} | |
throw [System.Management.Automation.ErrorRecord]::new( | |
[System.Exception]::new("Required assembly could not be located. This most likely indicates an installation error. Try repairing your Visual Studio installation. Expected location: $modulePath"), | |
"DevShellModuleLoad", | |
[System.Management.Automation.ErrorCategory]::NotInstalled, | |
$config) | |
} | |
function VsInstallationPath { | |
$setupargs = "-path `"$VsInstallationPath`"" | |
Write-Verbose "Using path: $VsInstallationPath" | |
$config = GetSetupConfigurations($setupargs) | |
LaunchDevShell($config) | |
} | |
function Latest { | |
$setupargs = "-latest" | |
if (-not $ExcludePrerelease) { | |
$setupargs += " -prerelease" | |
} | |
$config = GetSetupConfigurations($setupargs) | |
LaunchDevShell($config) | |
} | |
function VsInstanceId { | |
$configs = GetSetupConfigurations("-prerelease -all") | |
$config = $configs | Where-Object { $_.instanceId -eq $VsInstanceId } | |
if ($config) { | |
Write-Verbose "Found Visual Studio installation with InstanceId of '$($config.instanceId)' and InstallationPath '$($config.installationPath)'" | |
LaunchDevShell($config) | |
exit | |
} | |
throw [System.Management.Automation.ErrorRecord]::new( | |
[System.Exception]::new("Could not find an installation of Visual Studio with InstanceId '$VsInstanceId'."), | |
"VsSetupInstance", | |
[System.Management.Automation.ErrorCategory]::InvalidArgument, | |
$config) | |
} | |
function List { | |
$setupargs = "-sort" | |
if (-not $ExcludePrerelease) { | |
$setupargs = " -prerelease" | |
} | |
$configs = GetSetupConfigurations($setupargs) | |
$DisplayProperties = @("#") + $DisplayProperties | |
# Add an incrementing select column | |
$configs = $configs | | |
Sort-Object displayName, installationDate | | |
ForEach-Object {$i = 0}{ $i++; $_ | Add-Member -NotePropertyName "#" -NotePropertyValue $i -PassThru } | |
Write-Host "The following Visual Studio installations were found:" | |
$configs | Format-Table -Property $DisplayProperties | |
$selected = Read-Host "Enter '#' of the Visual Studio installation to launch DevShell. <Enter> to quit: " | |
if (-not $selected) { exit } | |
$config = $configs | Where-Object { $_."#" -eq $selected } | |
if ($config) { | |
LaunchDevShell($config) | |
} | |
else { | |
"Invalid selection: $selected" | |
} | |
} | |
function Default{ | |
Write-Verbose "No parameters passed to script. Trying VsInstallationPath." | |
try { | |
VsInstallationPath | |
exit | |
} | |
catch { | |
Write-Verbose "VsInstallationPath failed. Trying Latest." | |
} | |
Write-Host "Could not start Developer PowerShell using the script path." | |
Write-Host "Attempting to launch from the latest Visual Studio installation." | |
try { | |
Latest | |
exit | |
} | |
catch { | |
Write-Verbose "Latest failed. Defaulting to List." | |
} | |
Write-Host "Could not start Developer PowerShell from the latest Visual Studio installation." | |
Write-Host | |
List | |
} | |
if ($PSCmdlet.ParameterSetName) { | |
& (Get-ChildItem "Function:$($PSCmdlet.ParameterSetName)") | |
exit | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment