1. 関数の概要

2. PowerShellコード(バージョン取得関数)

この関数は、レジストリを確認してOfficeのバージョンを取得します。Officeがインストールされていない場合や、エラーが発生した場合は適切なメッセージを表示し、nullを返します。

Function Get-OfficeVersion {
    try {
        # Officeのバージョン情報が格納されている可能性のあるレジストリパス
        $officeRegPaths = @(
            "HKLM:\\SOFTWARE\\Microsoft\\Office",
            "HKLM:\\SOFTWARE\\WOW6432Node\\Microsoft\\Office"
        )

        $versionFound = $false
        foreach ($path in $officeRegPaths) {
            if (Test-Path $path) {
                # サブキーを列挙してバージョン情報を探す
                $subKeys = Get-ChildItem $path
                foreach ($key in $subKeys) {
                    if ($key.Name -match "\\d+\\.\\d+$") {
                        $versionFound = $true
                        return $key.Name.Split("\\")[-1]
                    }
                }
            }
        }

        if (-not $versionFound) {
            throw "Office is not installed or not found in the registry."
        }
    } catch {
        Write-Error "An error occurred while trying to determine the Office version: $_"
        return $null
    }
}