-
Notifications
You must be signed in to change notification settings - Fork 1
/
Launch-VsDevShell.ps1
277 lines (226 loc) · 9.25 KB
/
Launch-VsDevShell.ps1
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<#
.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
$params.SkipAutomaticLocation = $true
# -Arch is only available from 17.1
if ($Arch -and $command.Parameters.ContainsKey("Arch"))
{
$params.Arch = $Arch
} else {
$params.Arch = "amd64"
}
# -HostArch is only available from 17.1
if ($HostArch -and $command.Parameters.ContainsKey("HostArch"))
{
$params.HostArch = $HostArch
} else {
$params.HostArch = "amd64"
}
# -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
}