通过 PowerShell 获取AD域中所有计算机版本

在系统管理和维护中,获取计算机的操作系统信息是至关重要的。利用 PowerShell,我们可以方便地提取Windows计算机的信息。
本文将介绍如何使用 PowerShell 脚本获取域控中计算机版本信息,并根据实际需要输出想要的格式。


1.脚本结构

  • 定义转换函数:用于将 Windows 版本的构建号转换为更易于理解的版本名称。
  • 获取计算机信息:提取启用的计算机的相关属性,包括计算机名、IP 地址、操作系统、构建版本和上次登录时间。
  • 输出结果:将结果输出到 GridView 进行查看,导出到 CSV 文件以便后续分析,或进行分组统计。

2.定义转换函数

我们首先定义一个名为 ConvertWindowsBuild 的函数,用于将 Windows 操作系统构建版本号转换为易于理解的名称,这个函数会根据操作系统和版本号返回相应的版本名称。

$WinBuilds 变量中我已经更新覆盖到最新版本,几乎包含了所有Win10和Win11版本。

function ConvertWindowsBuild {
    param(
        [string] $OperatingSystem,
        [string] $OperatingSystemVersion
    )

    # 定义 Windows 构建版本字典
    $WinBuilds = @{
        '10.0 (10240)' = "Windows 10 1507"
        '10.0 (10586)' = "Windows 10 1511"
        '10.0 (14393)' = "Windows 10 1607"
        '10.0 (15063)' = "Windows 10 1703"
        '10.0 (16299)' = "Windows 10 1709"
        '10.0 (17134)' = "Windows 10 1803"
        '10.0 (17763)' = "Windows 10 1809"
        '10.0 (18362)' = "Windows 10 1903 (19H1)"
        '10.0 (18363)' = "Windows 10 1909 (19H2)"
        '10.0 (19041)' = "Windows 10 2004 (20H1)"
        '10.0 (19042)' = "Windows 10 20H2"
        '10.0 (19043)' = "Windows 10 21H1"
        '10.0 (19044)' = "Windows 10 21H2"
        '10.0 (19045)' = "Windows 10 22H2"
        '10.0 (22000)' = "Windows 11 21H2"
        '10.0 (22621)' = "Windows 11 22H2"
        '10.0 (22631)' = "Windows 11 23H2"
        '10.0 (26100)' = "Windows 11 24H2"
    }

    # 检查操作系统并返回相应的构建版本,只需处理Win10和Win11
    if (($OperatingSystem -like '*Windows 10*') -or ($OperatingSystem -like 'Windows 11*')) {
        $WinBuild = $WinBuilds[$OperatingSystemVersion]
    } else { $WinBuild = $OperatingSystem }

    # 其他情况
    if ($WinBuild) {
        $WinBuild
    }
    else {
        '未知版本'
    }
}

3. 获取计算机信息

接下来,我们将获取启用的计算机列表,并提取所需的属性。我们使用 Get-ADComputer 命令来查询AD中的计算机信息。

$CompList = Get-ADComputer -Filter { Enabled -eq $True } -Properties IPv4Address, OperatingSystem, LastLogonDate, OperatingSystemVersion -SearchBase "OU=Baiyang,DC=baiyang,DC=org" | ForEach-Object {
    # 创建自定义数据对象
    [PSCustomObject] @{
        "计算机名" = $_.Name
        "IP地址" = $_.IPv4Address
        "操作系统" = $_.OperatingSystem
        "构建版本" = ConvertWindowsBuild -OperatingSystem $_.OperatingSystem -OperatingSystemVersion $_.OperatingSystemVersion
        "上次登录时间" = $_.LastLogonDate
    }
}
  • 根据实际情况修改 -SearchBase 参数以指定搜索范围,或者直接去掉,默认就是全域搜索。

4. 输出结果

最后,我们将处理后的计算机信息输出到 GridView,以便管理员可以方便地查看。同时,我们也可以选择将结果导出到 CSV 文件以便于后续分析,还可以对结果进行分类,统计数量。

# 1.输出结果到 GridView
$CompList | Out-GridView

# 2.输出结果到 GridView 并导出到csv表格
# $CompList | Export-Csv -Path .\windows_version_report.csv -NoTypeInformation

# 3.根据“构建版本”属性进行分组并对输出结果格式化
# $CompList | Group-Object -Property "构建版本" | Format-Table -Property Name, Count
  • 根据实际需要去掉前面的#注释即可启用生效。

输出结果1: GridView网格视图

在这里插入图片描述


输出结果2: 导出到csv表格

在这里插入图片描述


输出结果3: 分组统计操作系统的数量

在这里插入图片描述


5. 附完整代码

# 定义转换Winwods版本名称的函数
function ConvertWindowsBuild {
    [CmdletBinding()]
    param(
        [string] $OperatingSystem,
        [string] $OperatingSystemVersion
    )

    # 定义 Windows 构建版本字典
    $WinBuilds = @{
        '10.0 (10240)' = "Windows 10 1507"
        '10.0 (10586)' = "Windows 10 1511"
        '10.0 (14393)' = "Windows 10 1607"
        '10.0 (15063)' = "Windows 10 1703"
        '10.0 (16299)' = "Windows 10 1709"
        '10.0 (17134)' = "Windows 10 1803"
        '10.0 (17763)' = "Windows 10 1809"
        '10.0 (18362)' = "Windows 10 1903 (19H1)"
        '10.0 (18363)' = "Windows 10 1909 (19H2)"
        '10.0 (19041)' = "Windows 10 2004 (20H1)"
        '10.0 (19042)' = "Windows 10 20H2"
        '10.0 (19043)' = "Windows 10 21H1"
        '10.0 (19044)' = "Windows 10 21H2"
        '10.0 (19045)' = "Windows 10 22H2"
        '10.0 (22000)' = "Windows 11 21H2"
        '10.0 (22621)' = "Windows 11 22H2"
        '10.0 (22631)' = "Windows 11 23H2"
        '10.0 (26100)' = "Windows 11 24H2"
    }

    # 检查操作系统并返回相应的构建版本,只需处理Win10和Win11
    if (($OperatingSystem -like '*Windows 10*') -or ($OperatingSystem -like 'Windows 11*')) {
        $WinBuild = $WinBuilds[$OperatingSystemVersion]
    }
    else { $WinBuild = $OperatingSystem }

    # 其他情况
    if ($WinBuild) {
        $WinBuild
    }
    else {
        '未知版本'
    }
}


# 获取启用的计算机并提取所需属性
$CompList = Get-ADComputer -Filter { Enabled -eq $True } -Properties IPv4Address, OperatingSystem, LastLogonDate, OperatingSystemVersion -SearchBase "OU=Baiyang,DC=baiyang,DC=org" | ForEach-Object {
    # 创建自定义数据对象
    [PSCustomObject] @{
        "计算机名" = $_.Name
        "IP地址" = $_.IPv4Address
        "操作系统" = $_.OperatingSystem
        "构建版本" = ConvertWindowsBuild -OperatingSystem $_.OperatingSystem -OperatingSystemVersion $_.OperatingSystemVersion
        "上次登录时间" = $_.LastLogonDate
    }
}

# 1.输出结果到 GridView
$CompList | Out-GridView

# 2.输出结果到 GridView 并导出到csv表格
# $CompList | Export-Csv -Path .\windows_version_report.csv -NoTypeInformation

# 3.根据“构建版本”属性进行分组并对输出结果格式化
# $CompList | Group-Object -Property "构建版本" | Format-Table -Property Name, Count

6.总结

通过以上 PowerShell 脚本,管理员可以方便地获取AD域中的计算机信息,不仅提高了工作效率,还能确保每台计算机的操作系统版本得到准确的识别和管理。希望这篇文章能帮助你在日常运维中更好地使用 PowerShell!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值