添加成员到 WMI 对象最佳实践?

Add-Member to WMI object best practice?

我正在编写一个使用 WMI 对象获取 Windows 版本的函数。但我想将 Windows 10 ReleaseId ("1709") (来自注册表项)添加到对象中。

这是一个愚蠢的想法吗? (它有效,我只是不知道这样做是否明智。)

function Get-OSVersion {
    [version]$OSVersion = (Get-WmiObject -Class Win32_OperatingSystem).Version

    if ($OSVersion.Major -ge '10') {
        $OSVersion | Add-Member -MemberType NoteProperty -Name ReleaseId -Value $([int]::Parse($(Get-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" ReleaseId).ReleaseId)) -Force
    }

    return $OSVersion
}

$OSVersion = Get-OSVersion
if ($OSVersion -ge '6.1') {"At least Win7"} else {"Too old"}
if ($OSVersion.ReleaseID -ge '1703') {"At least 1703."} else {"Too old"}

此外,覆盖成员 "Revision"(值始终为 -1)而不是添加新成员 "ReleaseId" 是否不明智?

扩展我的评论:

I wouldn't suggest changing a wmi class that you don't need to, but you're not doing that. I don't see anything wrong about your approach besides adding a member to a defined standard library class (System.Version) and doing a number comparison against a string.

我建议做的是与您需要的成员一起创建 [pscustomobject]

function Get-OSVersion {
    $OSVersion = [version](Get-CimInstance -ClassName Win32_OperatingSystem).Version

    if ($OSVersion.Major -ge 10) {
        [pscustomobject]@{
            Version = $OSVersion
            ReleaseId = [int](Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion').ReleaseId
        }
    } else {
        [pscustomobject]@{
            Version = $OSVersion
        }
    }
}

正在使用:

$OS = Get-OSVersion
if ($OS.Version -ge [version]'6.1') {
    'At least Win7'
} else {
    'Too old'
}
if ($OS.ReleaseId -ge 1703) {
    'At least 1703.'
} else {
    'Too old'
}

提供替代方案:使用哈希表,因为看起来您只是在进行 key/value 访问和比较,而没有任何方法实现。

function Get-OSVersion {
    $OS = @{
        Version = [version](Get-CimInstance -ClassName Win32_OperatingSystem).Version
    }

    if ($OS.Version.Major -ge 10) {
        $OS['ReleaseId'] = [int](Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion').ReleaseId
    }

    return $OS
}

我的建议是使用 calculated properties:

function Get-OSVersion {
    Get-WmiObject -Class Win32_OperatingSystem |
        Select-Object @{n='Version';e={[version]$_.Version}},
            @{n='ReleaseId';e={
                if (([version]$_.Version).Major -ge '10') {
                    [int](Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion').ReleaseId
                }
            }}
}