Chrome/Edge Chromium 內建「自動偵測新版本,提醒使用者下載更新」功能。但從資安管理角度,若新版涉及重要安全修補,晚一天更新就多曝險一天。遇到認真積極的網管,沒即時更新 Chrome 可是會被通緝的,只差沒印成懸賞海報貼在茶水間。

依據官方文件說明,Chromium 會自動下載安全更新檔,關閉瀏覽器再重新開啟時自動安裝,若瀏覽器過久未重啟,Chrome/Edge 將於右上角「更多」圖示會顯示驚嘆號(發佈後 2 天內是綠色、4 天橘色,超過一週紅色)提醒你更新。

有一些狀況可能導致 Chrome 未及時更新,例如:

  • 平日下班以睡眠取代關機,保留桌面狀態方便隔天無縫接軌(謎:無縫個屁,你睡一覺腦袋就清空了),瀏覽器可能一兩星期都不會重啟
  • 主力改用 Edge,Chrome 很久沒用

為了避免被網管盯上,我想到一個方法,寫支 PowerShell 排程定期檢查,發現有新版時寄信通知我。好啦,我承認這有點小題大作,但有機會練習寫程式耶,而且又可以改善生活,怎麼可以放過。

如何取得 Windows 目前安裝的 Chrome 版本?

Stackoverflow 找到簡便做法,先從 Registry 取得 chrome.exe 所在位置,再 Get-Item 讀取 chrome.exe 的檔案版本。

如何取得 Chrome 目前發行的最新版本?

Stackoverflow 找到兩種解法:

  • Chrome Team 有個 OmahaProxy 儀表板,能即時反映 Stable/Beta/Dev/Canary Channel 的最新版本,有 JSON 跟 CSV,評估用 CSV 比較省事
  • vergrabber 開源專案,每天午夜自動查詢 Apache2、Linux 核心、MySQL、Nginx、OpenSSL、7-zip、Acrobat Reader、Google Chrome、Microsoft Windows 10、Mozilla Firefox、OpenVPN、Oracle Java、TeamViewer、VeraCrypt 等軟體的最新版本資訊,以 JSON 格式提供

如何用 PowerShell 寄送通知信?

這個之前介紹過(借用 Outlook 寄送電子郵件),直接拿來用。

蒐集完資訊,把它們組合在一起就是成品囉~

function GetInstalledChromeVer() {
    $path = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)'
    return (Get-Item $path).VersionInfo.ProductVersion
}

function GetLatestChromeVer() {
    # omahaproxy CSV
    $csv = (Invoke-WebRequest -Uri https://omahaproxy.appspot.com/all).Content
    return $csv | ConvertFrom-Csv | Where-Object { $_.os -eq 'win' -and $_.channel -eq 'stable' } | Select-Object -ExpandProperty current_version
}

function SendMail {
    param (
        [Parameter(Mandatory = $true)]
        [string]
        $rcpt,
        [Parameter(Mandatory = $true)]
        [string]
        $subject,
        [Parameter(Mandatory = $true)]
        [string]
        $body
    )

    $outlookApp = [System.Runtime.InteropServices.Marshal]::GetActiveObject("Outlook.Application")
    if (!$outlookApp) {
        throw "請先開啟 Outlook 程式"
    }
    # Microsoft.Office.Interop.Outlook.OlItemType.olMailItem = 0
    $mailItem = $outlookApp.CreateItem(0)
    $rcpt.Split(',') | ForEach-Object {
        # Microsoft.Office.Interop.Outlook.OlMailRecipientType.olTo = 1 (olCC = 2, olBCC = 2)
        $mailItem.Recipients.Add($_).Type = 1
    }
    $mailItem.Subject = $subject
    if ($body.StartsWith('<')) {
        $mailItem.HTMLBody = $body
    }
    else {
        $mailItem.Body = $body
    }
    $mailItem.Send()
}

$currVer = GetInstalledChromeVer
$latestVer = GetLatestChromeVer
if ($currVer -eq $latestVer) {
    Write-Host "Chrome 已是最新版本 $latestVer" -ForegroundColor Green
}
else {
    $msg = "Chrome 版本過舊:$currVer 請更新至 $latestVer"
    Write-Host $msg -ForegroundColor Red
    SendMail 'user@mail.com' "Chrome 版本更新提醒 - $latesetVer" $msg
}

雖然我選擇從 CSV 查最近版號,還是順便練習了 JSON 解析:

https://vergrabber.kingu.pl/vergrabber.json

function GetLatestChromeVer_vergrabber() {
    # vergrabber
    $json = (Invoke-WebRequest -Uri https://vergrabber.kingu.pl/vergrabber.json).Content
    return (($json | ConvertFrom-Json).client.'Google Chrome'.PSObject.Properties | Select-Object -First 1).Value.Version
}

https://omahaproxy.appspot.com/all.json

function GetLatestChromeVer_omahaproxy_json() {
    # omahaproxy JSON
    $json = (Invoke-WebRequest -Uri https://omahaproxy.appspot.com/all.json).Content
    return ($json | ConvertFrom-Json) | Where-Object { $_.os -eq 'win' } | Select-Object -ExpandProperty versions | Where-Object { $_.channel -eq 'stable' } | Select-Object -ExpandProperty version
}

PowerShell example to get Chrome latest version info from web and to check if Chrome updated to newest version.


Comments

# by 小米

請教使用 VSCode 編輯 PowerShell,其中 intellisense 還行嗎?

# by Jeffrey

to 小米,我沒試過其他開發工具,指令、參數提示都有,自動完成也 OK,我感覺還行。

Post a comment