You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell检测VS Code文件名变化并下载64位便携包

用PowerShell实现VS Code 64位便携版条件下载

解决思路

VS Code下载页的按钮链接是动态跳转的,无法直接获取下载文件的真实地址和名称。需要先抓取页面内容,提取出对应版本的真实下载URL,再解析文件名,最后和本地文件对比,按需下载。

实现代码

# 定义VS Code下载页面URL
$downloadPageUrl = "https://code.visualstudio.com/download"

# 1. 抓取页面内容,提取64位便携版的真实下载链接
$pageContent = Invoke-WebRequest -Uri $downloadPageUrl -UseBasicParsing
# 匹配页面中64位便携版的下载链接(对应页面左下角.zip旁的64bit按钮)
$downloadUrl = ($pageContent.Links | Where-Object { $_.outerHTML -match '64 bit' -and $_.outerHTML -match 'portable' -and $_.href -like 'https://az764295.vo.msecnd.net/stable/*' }).href

if (-not $downloadUrl) {
    Write-Host "未找到64位便携版的下载链接"
    exit 1
}

# 2. 从下载链接中解析出文件名
$fileName = [System.IO.Path]::GetFileName($downloadUrl)
Write-Host "待下载文件名称: $fileName"

# 3. 检查本地是否已存在该文件(可自定义本地路径,这里用当前目录)
$localFilePath = Join-Path -Path $PWD.Path -ChildPath $fileName
if (Test-Path -Path $localFilePath) {
    Write-Host "本地已存在该文件: $fileName,无需下载"
    exit 0
}

# 4. 下载文件
Write-Host "开始下载: $fileName"
Invoke-WebRequest -Uri $downloadUrl -OutFile $localFilePath -UseBasicParsing
Write-Host "下载完成: $localFilePath"

代码说明

  • 获取真实下载链接:通过Invoke-WebRequest抓取页面内容,筛选出包含"64 bit"和"portable"标识的链接,且链接指向微软的稳定版存储地址,以此得到真实下载URL。
  • 解析文件名:利用System.IO.Path.GetFileName从下载URL中提取文件名,解决无法提前查看文件名的问题。
  • 条件下载:对比本地文件,仅当文件不存在时执行下载操作。

内容的提问来源于stack exchange,提问作者YorSubs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 10:02:23