Auto-Update VS Code on Terminal Servers (RDS / Citrix)

Of course, in corona times, we also work mostly from home. To access the companies’ resources, we use Microsoft’s RDS as our terminal server solution. Aswell for our developers. Here, however, we repeatedly encountered the problem that we could only update Visual Studio Code with difficulty. Sometimes there’s still open processes of users (that forgot to logout in the evening or work 24/7 :-D), sometimes it’s just annoying to install it manually. To solve this problem, we started looking for solutions to automate this.

My colleague Andreas Fischer searched the mighty internet and found some useful approaches.
Here is our result based on this script:

Auto-Update VS Code
  • Running as a nightly cronjob on the RDS hosts
  • Check the latest version on GitHub
  • Check the currently installed version of VS Code
  • Close all open processes, download, and install silently

Auto-Update VS Code – PowerShell script

#Auto-Update VS Code
#Get the latest version from GitHub
$releases = Invoke-RestMethod "https://api.github.com/repos/Microsoft/vscode/releases"
$latest = [Version] ($releases.tag_name | Sort-Object -Descending | Select-Object -First 1)
Write-Host "Latest available version: " $latest

#Get the currently installed version from program files
$current = [Version] ((Get-Content 'C:\Program Files\Microsoft VS Code\resources\app\package.json' | ConvertFrom-Json).Version)
Write-Host "Currently installed version: " $current

#Compare these versions
if($latest -gt $current)
{
    Write-Host "Visual Studio Code update started"

    #Are there any running instances on the maschine?
    $codeIsRunning = @(Get-Process -name Code -ErrorAction Ignore).Count -gt 0
    if($codeIsRunning)
    {
        Stop-Process -name Code
    }
    
    #Download and install the new version
    Invoke-WebRequest "https://code.visualstudio.com/sha/download?build=stable&os=win32-x64" -OutFile $env:TEMP/vscode_latest.exe
    Start-Process -FilePath $env:TEMP/vscode_latest.exe -Argument "/verysilent /mergetasks=!runcode"
}

Just create a cronjob on your RDS hosts that runs the script once a night/week/month… This should Auto-Update VS Code.

... is a technical consultant and developer at Comsol Unternehmenslösungen AG in Kronberg/Taunus. Major tasks are the architecture and implementation of complex, usually cross-system applications in and around Microsoft Dynamics 365 Business Central.

Leave a Reply

Your email address will not be published. Required fields are marked *