Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save The-Running-Dev/ba40bcb1e966e835baa2794dcdbf0765 to your computer and use it in GitHub Desktop.
Save The-Running-Dev/ba40bcb1e966e835baa2794dcdbf0765 to your computer and use it in GitHub Desktop.
Update all visual studio editions and instances equal or above 2017
$EventLogSourceName = "Visual Studio Updater"
if([System.Diagnostics.EventLog]::SourceExists($EventLogSourceName) -eq $false){
New-EventLog –LogName Application –Source $EventLogSourceName
}
$cacheDirs = @(
"C:\Program Files (x86)\Microsoft Visual Studio\Installer"
);
foreach($cacheDir in $cacheDirs){
if(Test-Path -Path "$cacheDir"){
Remove-Item -Path "$cacheDir" -Recurse -Force
}
}
Write-Host "Attempting to update all visual studio editions and instances equal or above 2017" -ForegroundColor Cyan
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 1 –Message "Attempting to update all visual studio editions and instances equal or above 2017"
# [Microsoft Developer community](https://developercommunity.visualstudio.com/content/problem/307261/unattend-self-update-of-vs-installer.html)
$vsCommunityDownloadUrl = "https://download.visualstudio.microsoft.com/download/pr/8a973d5d-2ccb-428c-8204-290a15d30e2c/be8c694b12879a8f47f34369d55d453c/vs_community.exe";
$vsSetupDirectory = "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Setup"
$vsCommunitySetupPath = "$vsSetupDirectory\vs_community.exe";
if((Test-Path -Path $vsSetupDirectory) -eq $false){
$r = md $vsSetupDirectory
}
if((Test-Path -Path $vsCommunitySetupPath) -eq $false){
Write-Host "Downloading VS Setup from ``$($vsCommunityDownloadUrl)``..." -ForegroundColor Gray
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 10 –Message "Downloading VS Setup from ``$($vsCommunityDownloadUrl)``..."
try {
(New-Object System.Net.WebClient).DownloadFile($vsCommunityDownloadUrl, $vsCommunitySetupPath);
}
catch [System.Net.WebException],[System.IO.IOException] {
Write-Host "Failed to download ``$($vsCommunityDownloadUrl)``: $($_.Message)" -ForegroundColor Red
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Error –EventID 18 –Message "Failed to download ``$($vsCommunityDownloadUrl)``: $($_.Message)"
}
catch {
Write-Host "Failed to store download ``$($vsCommunityDownloadUrl)`` at ``$($vsCommunitySetupPath)``: $($_.Message)" -ForegroundColor Red
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Error –EventID 19 –Message "Failed to store download ``$($vsCommunityDownloadUrl)`` at ``$($vsCommunitySetupPath)``: $($_.Message)"
}
}
Write-Host "Starting the VS setup to update the VS Installer at ``$($vsCommunitySetupPath)``" -ForegroundColor Gray
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 20 –Message "Starting the VS setup to update the VS Installer at ``$($vsCommunitySetupPath)``"
$installerUpdateProcess = Start-Process `
-FilePath $vsCommunitySetupPath `
-Wait `
-PassThru `
-ArgumentList @(
"--update",
"--quiet",
"--wait"
);
$installerUpdateProcess.WaitForExit();
if($installerUpdateProcess.ExitCode -ne 0){
Write-Host "The update of the Visual Studio updater ``$([IO.Path]::GetFileName($vsCommunitySetupPath))`` failed with exit code: ``$($installerUpdateProcess.ExitCode)``" -ForegroundColor Red
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Error –EventID 25 –Message "The update of the Visual Studio updater ``$([IO.Path]::GetFileName($vsCommunitySetupPath))`` failed with exit code: ``$($installerUpdateProcess.ExitCode)``"
} else {
Write-Host "The Visual Studio updater was updated successfully." -ForegroundColor Green
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 29 –Message "The Visual Studio updater was updated successfully."
$vsInstallations = @();
foreach($version in Get-ChildItem "C:\Program Files (x86)\Microsoft Visual Studio\" -Filter "20*"){
Write-Host "Found Visual Studio $($version.Name)" -ForegroundColor Gray
foreach($edition in Get-ChildItem $version.FullName){
Write-Host " Found Visual Studio $($version.FullName) $($edition.Name)" -ForegroundColor Gray
$vsInstallations += $edition.FullName
}
}
foreach($installation in $vsInstallations){
Write-Host "Starting update Visual Studio at ``$($installation)``." -ForegroundColor Cyan
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 30 –Message "Starting update Visual Studio at ``$($installation)``."
# [Microsoft Docs](https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio)
$vsUpdateProcess= Start-Process `
-FilePath "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" `
-Wait `
-PassThru `
-ArgumentList @(
"update",
"--installPath `"$installation`"",
"--norestart",
"--quiet",
"--force"
);
$vsUpdateProcess.WaitForExit();
if($vsUpdateProcess.ExitCode -ne 0){
Write-Host "The update of Visual Studio at ``$($installation)`` failed with exit code: ``$($vsUpdateProcess.ExitCode)``" -ForegroundColor Red
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Error –EventID 39 –Message "The update of Visual Studio at ``$($installation)`` failed with exit code: ``$($vsUpdateProcess.ExitCode)``"
} else {
Write-Host "Successfully updated Visual Studio at ``$($installation)``." -ForegroundColor Green
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 35 –Message "Successfully updated Visual Studio at ``$($installation)``."
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment