Skip to content

Instantly share code, notes, and snippets.

@lennybacon
Last active January 16, 2020 10:50
Show Gist options
  • Save lennybacon/4182e3cb60aa5d22fdda379d5dbe54a7 to your computer and use it in GitHub Desktop.
Save lennybacon/4182e3cb60aa5d22fdda379d5dbe54a7 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"
#"C:\ProgramData\Microsoft\VisualStudio\Packages",
#"C:\ProgramData\Microsoft\VisualStudio\Setup"
);
foreach($cacheDir in $cacheDirs){
if(Test-Path -Path "$cacheDir"){
Remove-Item -Path "$cacheDir" -Recurse -Force -ErrorAction SilentlyContinue
}
}
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){
$devEnvPath = [IO.Path]::Combine($edition.FullName, "Common7\IDE\devenv.exe");
$fi = new-object System.IO.FileInfo($devEnvPath);
Write-Host " Found Visual Studio $($version.FullName) $($edition.Name) in version ``$($fi.VersionInfo.FileVersion)``" -ForegroundColor Gray
$vsInstallations += $edition.FullName
}
}
Stop-Service "vstsagent.tfs.$([Environment]::MachineName)" -ErrorAction SilentlyContinue
foreach($installation in $vsInstallations){
$devEnvPath = [IO.Path]::Combine($installation, "Common7\IDE\devenv.exe");
$fi = new-object System.IO.FileInfo($devEnvPath);
Write-Host "Starting update Visual Studio at ``$($installation)`` of ``$($fi.VersionInfo.FileVersion)``." -ForegroundColor Cyan
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 30 –Message "Starting update Visual Studio at ``$($installation)`` of ``$($fi.VersionInfo.FileVersion)``."
# [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 {
$fi = new-object System.IO.FileInfo($devEnvPath);
Write-Host "Successfully updated Visual Studio at ``$($installation)`` to ``$($fi.VersionInfo.FileVersion)``." -ForegroundColor Green
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 35 –Message "Successfully updated Visual Studio at ``$($installation)`` to ``$($fi.VersionInfo.FileVersion)``."
}
}
}
Start-Service "vstsagent.tfs.$([Environment]::MachineName)" -ErrorAction SilentlyContinue
$registryKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired");
if ($null -ne $registryKey){
if ($registryKey.GetValueNames().Length -ne 0) {
Write-Host "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired indicates a pending reboot.";
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 99 –Message "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired indicates a pending reboot.";
Restart-Computer -Force
}
}
$registryKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Updates");
if ($null -ne $registryKey){
$updateExeVolatile = $registryKey.GetValue("UpdateExeVolatile");
if ($null -ne $updateExeVolatile) {
Write-Host "HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile indicates a pending reboot.";
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 99 –Message "HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile indicates a pending reboot.";
Restart-Computer -Force
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment