当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 导出 Windows 操作系统版本号

作者:精品下载站 日期:2024-12-14 18:42:36 浏览:16 分类:玩电脑

导出 Windows 操作系统版本号


我们想要获取所有 AD 计算机内部版本号的列表。那是因为我们想知道所有计算机是否都是最新的。获取此报告的一个绝佳方法是使用 PowerShell。在本文中,您将了解如何将 Windows 操作系统版本号导出到 CSV 文件。

检查 Windows 内部版本号

您可以从关于 Windows 屏幕 (winver) 获取版本和操作系统内部版本号。

这是在 Windows Server 2019 上。

[玩转系统] 导出 Windows 操作系统版本号

这是在 Windows 10 上。

[玩转系统] 导出 Windows 操作系统版本号

但您想为每个系统执行此操作吗?不,你不知道。所以 PowerShell 就是答案。

导出 Windows 操作系统内部版本号 PowerShell 脚本

Get-WindowsOSBuilds.ps1 PowerShell 脚本循环遍历每台计算机,测试其是否在线,如果在线,则检查 WinRM 是否已启用。如果启用 WinRM,该脚本将检索版本、版本和操作系统内部版本。

该脚本收集每台 AD 计算机的以下信息:

  1. 计算机名

  2. 地位

  3. WinRM

  4. 版本

  5. 操作系统构建

重要提示:Windows 远程管理服务需要在每台计算机上运行才能检索信息。这可以通过部署 GPO 或其他替代方案来完成。

您可以通过以下名称来识别 WinRM 服务:

  • 服务名称: WinRM

  • 显示名称: Windows 远程管理 (WS-Management)

[玩转系统] 导出 Windows 操作系统版本号

准备 Get-WindowsOSBuilds PowerShell 脚本

在域控制器(C:)驱动器上创建两个文件夹:

  • 温度

  • 脚本

下载 Get-WindowsOSBuilds.ps1 PowerShell 脚本并将其放置在 C:\scripts 文件夹中。该脚本会将 CSV 文件导出到 C:\temp 文件夹。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Get-WindowsOSBuilds.ps1 并将其放置在 C:\scripts 文件夹中。

<#
    .SYNOPSIS
    Get-WindowsOSBuilds.ps1

    .DESCRIPTION
    Export Windows OS versions and build numbers to CSV file.

    .LINK
    www.a-d.site/export-windows-os-build-numbers

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.a-d.site
    LinkedIn:   linkedin.com/in/a-d

    .CHANGELOG
    V1.00, 04/16/2023 - Initial version
#>

# Retrieve a list of all computers in the domain
$computers = Get-ADComputer -Filter { OperatingSystem -Like "Windows*" }

# Set the registry path that will be used to retrieve the Windows build numbers
$regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"

# Initialize progress bar
$total = $computers.Count
$completed = 0
$progress = 0
Write-Progress -Activity "Retrieving Windows build numbers" -Status "Starting..." -PercentComplete $progress

# Loop through each computer and retrieve the Windows versions and build numbers (if the computer is online)
$results = foreach ($computer in $computers) {
    $computerName = $computer.Name
    $online = Test-Connection -ComputerName $computerName -Count 1 -Quiet
    if ($online) {
        $winRMEnabled = (Test-WSMan -ComputerName $computerName -ErrorAction SilentlyContinue) -ne $null
        if ($winRMEnabled) {
            $buildNumber = (Invoke-Command -ComputerName $computerName { (Get-ItemProperty -Path $using:regPath -Name "CurrentBuild").CurrentBuild })
            $revisionNumber = (Invoke-Command -ComputerName $computerName { (Get-ItemProperty -Path $using:regPath -Name "UBR").UBR })
            $windowsBuildNumber = "$buildNumber.$revisionNumber"
            $edition = (Invoke-Command -ComputerName $computerName { (Get-ItemProperty -Path $using:regPath -Name "ProductName").ProductName })
            $version = (Invoke-Command -ComputerName $computerName { (Get-ItemProperty -Path $using:regPath -Name "ReleaseID" -ErrorAction Stop).ReleaseID })
        }
        else {
            $windowsBuildNumber = "N/A"
            $edition = "N/A"
            $version = "N/A"
        }
        [PSCustomObject] @{
            "ComputerName" = $computerName
            "Status"       = "Online"
            "WinRM"        = if ($winRMEnabled) { "Enabled" } else { "Disabled" }
            "Edition"      = $edition
            "Version"      = $version
            "OSBuild"      = $windowsBuildNumber
        }
    }
    else {
        [PSCustomObject] @{
            "ComputerName" = $computerName
            "Status"       = "Offline"
            "WinRM"        = "N/A"
            "Edition"      = "N/A"
            "Version"      = "N/A"
            "OSBuild"      = "N/A"
        }
    }
    $completed++
    $progress = [Math]::Round($completed / $total * 100)
    Write-Progress -Activity "Retrieving Windows build numbers" -Status "Completed $completed of $total" -PercentComplete $progress
}

# Sort the results by ComputerName in ascending order and select only the desired columns
$results | Sort-Object ComputerName | Select-Object ComputerName, Status, WinRM, Edition, Version, OSBuild | Export-Csv -Path "C:\Temp\WindowsOSBuilds.csv" -NoTypeInformation

这就是它的样子。

[玩转系统] 导出 Windows 操作系统版本号

运行 Get-WindowsOSBuilds PowerShell 脚本

使用 PowerShell 获取所有 AD 计算机的 Windows 操作系统版本。更改脚本文件夹的路径。之后,运行脚本 Get-WindowsOSBuilds.ps1。

PS C:\> cd c:\scripts\
PS C:\scripts> .\Get-WindowsOSBuilds.ps1

打开 Windows 操作系统构建报告 CSV 文件

Get-WindowsOSBuilds.ps1 PowerShell 脚本会将 Active Directory 计算机内部版本号导出到 CSV 文件。在路径 C:\temp 中查找文件 WindowsOSBuilds.csv

[玩转系统] 导出 Windows 操作系统版本号

使用您喜欢的应用程序打开 CSV 文件。在我们的示例中,它是 Microsoft Excel。

[玩转系统] 导出 Windows 操作系统版本号

Windows 操作系统构建报告看起来非常出色。

重要提示:如果组织中有这样的政策,请不要忘记禁用 Windows 远程管理 (WinRM)。

这是否可以帮助您将 Windows 操作系统版本号导出到 CSV 文件?

了解更多:在 Windows 中卸载并禁用 SMBv1 »

结论

您学习了如何使用 PowerShell 导出 Windows 操作系统版本号。使用 Get-WindowsOSBuilds PowerShell 脚本获取 Windows 操作系统构建报告并进行查看。出于安全目的,请确保所有 Windows AD 计算机都是最新的。

您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 脚本检查 Windows 上的可用磁盘空间。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯