-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chocolatey-visualstudio.extension: refactor waiting for orphaned VS u…
…ninstaller processes GitHub-Issue: GH-13
- Loading branch information
1 parent
14bbf82
commit 4a92f7e
Showing
2 changed files
with
67 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
64 changes: 64 additions & 0 deletions
64
chocolatey-visualstudio.extension/extensions/Wait-VSInstallerProcesses.ps1
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
function Wait-VSInstallerProcesses | ||
{ | ||
[CmdletBinding()] | ||
Param | ||
( | ||
[Parameter(Mandatory = $true)] [ValidateSet('Wait', 'Fail')] $Behavior | ||
) | ||
|
||
$exitCode = $null | ||
|
||
# Not only does a process remain running after vs_installer /uninstall finishes, but that process | ||
# pops up a message box at end! Sheesh. | ||
Write-Debug 'Looking for vs_installer.windows.exe processes spawned by the uninstaller' | ||
$installerProcesses = Get-Process -Name 'vs_installer.windows' -ErrorAction SilentlyContinue | ||
$installerProcessesCount = ($installerProcesses | Measure-Object).Count | ||
if ($installerProcessesCount -gt 0) | ||
{ | ||
if ($Behavior -eq 'Fail') | ||
{ | ||
Write-Warning "Found $installerProcessesCount vs_installer.windows.exe process(es): $($installerProcesses | Select-Object -ExpandProperty Id)" | ||
throw 'There are Visual Studio installer processes already running. Installation cannot continue.' | ||
} | ||
|
||
Write-Debug "Found $installerProcessesCount vs_installer.windows.exe process(es): $($installerProcesses | Select-Object -ExpandProperty Id)" | ||
Write-Debug "Waiting for all vs_installer.windows.exe processes to become input-idle" | ||
foreach ($p in $installerProcesses) | ||
{ | ||
[void] $p.Handle # make sure we get the exit code http://stackoverflow.com/a/23797762/266876 | ||
$p.WaitForInputIdle() | ||
} | ||
Write-Debug "Sending CloseMainWindow to all vs_installer.windows.exe processes" | ||
foreach ($p in $installerProcesses) | ||
{ | ||
$p.CloseMainWindow() | ||
} | ||
Write-Debug "Waiting for all vs_installer.windows.exe processes to exit" | ||
$installerProcesses | Wait-Process | ||
foreach ($proc in $installerProcesses) | ||
{ | ||
if ($exitCode -eq $null) | ||
{ | ||
$exitCode = $proc.ExitCode | ||
} | ||
if ($proc.ExitCode -ne 0) | ||
{ | ||
Write-Warning "vs_installer.windows.exe process $($proc.Id) exited with code $($proc.ExitCode)" | ||
if ($exitCode -eq 0) | ||
{ | ||
$exitCode = $proc.ExitCode | ||
} | ||
} | ||
else | ||
{ | ||
Write-Debug "vs_installer.windows.exe process $($proc.Id) exited with code $($proc.ExitCode)" | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
Write-Debug 'Did not find any running vs_installer.windows.exe processes.' | ||
} | ||
|
||
return $exitCode | ||
} |