microsoft / vssetup.powershell

PowerShell module to interact with Visual Studio Setup
MIT License
233 stars 41 forks source link

Get-VsSetupInstance is unable to find the newly installed instance of Visual Studio 2019 without a reboot #71

Closed tristanbarcelon closed 2 years ago

tristanbarcelon commented 2 years ago

We have a script which uses winget and custom powershell modules to install tools and visual studio 2019 for a self-hosted Azure Devops build agent. More often than not, Get-VSSetupInstance is unable to detect the newly installed instance of VS2019 if I execute it from the same powershell session used to install VS 2019. If I reboot the machine and re-execute Get-VSSetupInstance, it works. Is there something I am doing wrong? When installing visual studio, I am passing the --norestart parameter as specified here: https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio?view=vs-2019. The variable $VS2019 is used to calculate path for vsixinstaller.exe which we then use to programmatically download and install VSIX from the marketplace.

This is the powershell snippet we use and without a reboot, $VS2019 variable is $null.

switch ($Edition) {
    'Enterprise' { $DisplayName = 'Visual Studio Enterprise 2019'}
    'Professional' { $DisplayName = 'Visual Studio Professional 2019'}
    'Community' { $DisplayName = 'Visual Studio Community 2019'}
    'BuildTools' { $DisplayName = 'Visual Studio Build Tools 2019'}
}

$VsSetup = Get-InstalledModule -Name VsSetup -ErrorAction SilentlyContinue -Verbose:$false

if ($null -eq $VsSetup) {
    Install-Module -Name VsSetup -Repository PSGallery -Verbose:$false
}

Import-Module -Name VsSetup -Verbose:$false

$VS2019 = Get-VsSetupInstance -ErrorAction SilentlyContinue -Verbose:$false |  `
    Where-Object { ($_.DisplayName -ieq $DisplayName) -and ($_.Product.Id -ieq "Microsoft.VisualStudio.Product.$($Edition)") } | `
    Select-VSSetupInstance -Latest -Verbose:$false
heaths commented 2 years ago

If VS required a reboot it won't show up by default by design. The same is true for vswhere. You can pass -All to see all instances whether in a launchable state or not, but if it showed up after a reboot then it seems very likely that VS itself needed a reboot and wouldn't have launched until that happens.

If you can reproduce this, I recommend passing Get-VSSetupInstance -All -Prerelease | select InstanceId, DisplayName, State and see what the State says. It should say "Complete". If it does and still doesn't show up, please reopen this issue and provide the output of Get-VSSetupInstance -All -Prerelease | select *.