# Get-OfficeLatestBuild -Channel ChannelName -Count Num) # 指定したチャネル(既定、Current)の最新5ビルド(既定、-Count #で調整可能)、 # 利用可能なのがそれ以下の場合は利用可能なビルドのみリストアップ function Get-OfficeLatestBuild { [CmdletBinding()] param ( [ValidateSet( 'Current', 'CurrentPreview', 'MonthlyEnterprise', 'SemiAnnual', 'SemiAnnualPreview', 'BetaChannel', 'PerpetualVL2024', 'PerpetualVL2021', 'PerpetualVL2019' )] [string]$Channel = 'Current', [ValidateRange(1, 1000)] [int]$Count = 5 ) $uri = 'https://clients.config.office.net/releases/v1.0/OfficeReleases' try { $releases = Invoke-RestMethod -Uri $uri -Method Get -Headers @{ 'Cache-Control' = 'no-cache' } -ErrorAction Stop $selected = $releases | Where-Object { $_.channelId -eq $Channel } | Select-Object -First 1 if (-not $selected.officeVersions) { throw "チャネル '$Channel' のバージョン情報が取得できませんでした。" } $selected.officeVersions | Sort-Object { [version]$_.legacyVersion } -Descending | Select-Object -First $Count | ForEach-Object { [PSCustomObject]@{ Channel = $Channel Version = $_.releaseVersion Build = [version]$_.legacyVersion AvailabilityDate = [datetimeoffset]$_.availabilityDate } } } catch { $PSCmdlet.ThrowTerminatingError($_) } } # Select-OfficeBuild -Versions $versions # ビルドのコレクションをメニュー化 # 最新(1)以外を選択した場合は、更新設定を確認 function Select-OfficeBuild { [CmdletBinding()] [OutputType([version])] param ( [Parameter(Mandatory)] [object[]]$Versions ) $items = @($Versions) if ($items.Count -eq 0) { throw '選択できるバージョンがありません。' } for ($i = 0; $i -lt $items.Count; $i++) { Write-Host ('{0}. Channel:{1}, Version:{2}(Build:{3}), Release Date:{4}' -f ($i + 1), $items[$i].Channel, $items[$i].Version, $items[$i].Build, $items[$i].AvailabilityDate) } while ($true) { $answer = Read-Host "番号を選択してください(1~$($items.Count))" $number = 0 if ( [int]::TryParse($answer, [ref]$number) -and $number -ge 1 -and $number -le $items.Count ) { if ($number -ne 1) { $autoUpdateEnabled = $null # ポリシーを優先して確認 $policyPaths = @( 'HKLM:\SOFTWARE\Policies\Microsoft\cloud\office\16.0\Common\OfficeUpdate' 'HKLM:\SOFTWARE\Policies\Microsoft\Office\16.0\Common\OfficeUpdate' ) foreach ($path in $policyPaths) { $policy = Get-ItemProperty -Path $path -ErrorAction SilentlyContinue if ($null -ne $policy.EnableAutomaticUpdates) { $autoUpdateEnabled = [int]$policy.EnableAutomaticUpdates -eq 1 break } } # ポリシー未設定ならOffice側の設定を確認 if ($null -eq $autoUpdateEnabled) { $config = Get-ItemProperty ` -Path 'HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration' ` -ErrorAction SilentlyContinue if ($null -ne $config.UpdatesEnabled) { $autoUpdateEnabled = [string]$config.UpdatesEnabled -eq 'True' } } if ($autoUpdateEnabled -eq $true) { Write-Warning '自動更新が有効です。選択したバージョンに更新し、そのバージョンを維持するには、Microsoft 365アプリ(WordやExcelなど)の[ファイル]→[アカウント]→[更新オプション]で[更新を無効にする]を選択してから続行してください。' } elseif ($null -eq $autoUpdateEnabled) { Write-Warning '自動更新の状態を確認できませんでした。選択したバージョンを維持する場合は、Microsoft 365アプリ(WordやExcelなど)の更新設定を確認し、無効になっていることを確認してから続行してください。' } } return [version]$items[$number - 1].Build } Write-Host '表示された範囲の番号を入力してください。' -ForegroundColor Yellow } } # Get-OfficeInstalledChannel # インストールされているMicrosoft 365アプリの更新チャネルを取得 # クラウド設定、ポリシー設定、ローカル設定から取得、優先ソースは # https://learn.microsoft.com/en-us/microsoft-365-apps/updates/change-update-channels#policy-precedence function Get-OfficeInstalledChannel { [CmdletBinding()] [OutputType([string])] param () $configPath = 'HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration' $sources = @( @{ Path = 'HKLM:\SOFTWARE\Policies\Microsoft\cloud\office\16.0\Common\officeupdate' Names = @('UpdatePath', 'UpdateBranch') } @{ Path = 'HKLM:\SOFTWARE\Policies\Microsoft\office\16.0\Common\officeupdate' Names = @('UpdatePath', 'UpdateBranch') } @{ Path = $configPath Names = @('UpdateUrl', 'UnmanagedUpdateURL', 'CDNBaseUrl') } ) $channels = @{ '492350f6-3a01-4f97-b9c0-c7c6ddf67d60' = 'Current' '64256afe-f5d9-4f86-8936-8840a6a4f5be' = 'CurrentPreview' '55336b82-a18d-4dd6-b5f6-9e5095c314a6' = 'MonthlyEnterprise' '7ffbc6bf-bc32-4f92-8982-f9dd17fd3114' = 'SemiAnnual' 'b8f9b850-328d-4355-9145-c59439a0c4cf' = 'SemiAnnualPreview' '5440fd1f-7ecb-4221-8110-145efaa6372f' = 'BetaChannel' '7983bac0-e531-40cf-be00-fd24fe66619c' = 'PerpetualVL2024' '5030841d-c919-4594-8d2d-84ae4f96e58e' = 'PerpetualVL2021' 'f2e724c1-748f-4b47-8fb8-8e0d210e9208' = 'PerpetualVL2019' } # ポリシーで使われる名前と旧名称 $aliases = @{ Current = 'Current' Monthly = 'Current' CurrentPreview = 'CurrentPreview' FirstReleaseCurrent = 'CurrentPreview' InsiderSlow = 'CurrentPreview' Insiders = 'CurrentPreview' MonthlyEnterprise = 'MonthlyEnterprise' SemiAnnual = 'SemiAnnual' Deferred = 'SemiAnnual' Broad = 'SemiAnnual' SemiAnnualPreview = 'SemiAnnualPreview' FirstReleaseDeferred = 'SemiAnnualPreview' Targeted = 'SemiAnnualPreview' BetaChannel = 'BetaChannel' InsiderFast = 'BetaChannel' PerpetualVL2024 = 'PerpetualVL2024' PerpetualVL2021 = 'PerpetualVL2021' PerpetualVL2019 = 'PerpetualVL2019' } if (-not (Test-Path -LiteralPath $configPath -ErrorAction Stop)) { throw 'Office Click-to-Runのインストール情報がありません。' } foreach ($source in $sources) { if (-not (Test-Path -LiteralPath $source.Path -ErrorAction Stop)) { continue } $settings = Get-ItemProperty -LiteralPath $source.Path -ErrorAction Stop foreach ($name in $source.Names) { $property = $settings.PSObject.Properties[$name] if ($null -eq $property) { continue } $value = ([string]$property.Value).Trim() if ([string]::IsNullOrWhiteSpace($value)) { continue } Write-Verbose "判定元: $($source.Path)\$name = $value" if ($aliases.ContainsKey($value)) { return $aliases[$value] } if ($value -match '(?i)[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}') { $id = $Matches[0].ToLowerInvariant() if ($channels.ContainsKey($id)) { return $channels[$id] } } # 上位設定が共有フォルダーなどの場合、下位設定からは断定しない throw "優先設定からチャネルを判定できません:$($source.Path)\$name = '$value'" } } throw '更新チャネルの設定が見つかりませんでした。' } # メインコード # $currentver = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\Configuration" -ErrorAction SilentlyContinue).VersionToReport $channel = $null $versions = $null try { $channel = Get-OfficeInstalledChannel -ErrorAction Stop $versions = Get-OfficeLatestBuild -Channel $channel -ErrorAction Stop } catch { Write-Error "取得に失敗しました:$($_.Exception.Message)" return } Write-Host "---------------------------------------" Write-Host "Microsoft 365 アプリの情報:" Write-Host " 更新チャネル : $($versions[0].Channel)" Write-Host " 現在のバージョン: $($currentver)" Write-Host " 最新のバージョン: $($versions[0].Build)" Write-Host "---------------------------------------" Write-Host "" $selectedBuild = Select-OfficeBuild -Versions $versions if ([version]$currentver -eq $selectedBuild) { Write-Host "アプリは既に選択されたバージョンに更新済みです。" return } else { do { Write-Host "選択されたビルド: $($selectedBuild.ToString())" $answer = (Read-Host '更新を開始しますか?(Y/N)').Trim() } until ($answer -in @('Y', 'N')) if ($answer -eq 'N') { Write-Host '処理を中止します。' return } } #アプリの更新 $M365UpdateCmd = "C:\Program Files\Common Files\Microsoft Shared\ClickToRun\OfficeC2RClient.exe" $M365UpdateArg = "/update user displaylevel=True updatetoversion=$($selectedBuild)" if (Test-Path -Path $M365UpdateCmd) { Start-Process -FilePath $M365UpdateCmd -ArgumentList $M365UpdateArg Write-Host "更新を開始しました。このウィンドウは閉じて構いません。" }