能否通过Get-ADComputer获取CPU核心数、处理器名称?需更换命令吗?
关于AD计算机查询CPU信息的问题
嘿,我来帮你理清这个问题!首先直接给结论:你当前使用的Get-ADComputer命令没办法直接获取CPU核心数、处理器名称这类硬件详情——因为Active Directory默认不会存储客户端的硬件信息,它只保存计算机账户的基础属性(比如你已经在查询的名称、操作系统、最后登录时间这些内容)。
要获取CPU这类硬件详情,得直接远程查询目标计算机的系统信息,这里有两种常用的PowerShell方案:
方法1:使用Get-CimInstance(推荐,WMI的现代替代)
先通过AD筛选出启用的计算机,再逐个远程查询它们的CPU信息,示例命令如下:
# 先获取所有启用的AD计算机列表 $enabledComputers = Get-ADComputer -Filter {enabled -eq $true} -Properties Name, DNSHostName, OperatingSystem, LastLogonDate # 遍历每个计算机,查询并整合CPU信息 foreach ($computer in $enabledComputers) { try { # 远程查询CPU属性 $cpuInfo = Get-CimInstance -ClassName Win32_Processor -ComputerName $computer.DNSHostName -ErrorAction Stop # 输出整合后的信息 [PSCustomObject]@{ ComputerName = $computer.Name DNSHostName = $computer.DNSHostName OperatingSystem = $computer.OperatingSystem LastLogonDate = $computer.LastLogonDate ProcessorName = $cpuInfo.Name NumberOfPhysicalCores = $cpuInfo.NumberOfCores NumberOfLogicalProcessors = $cpuInfo.NumberOfLogicalProcessors } } catch { # 处理无法连接的异常情况 Write-Warning "无法连接到计算机 $($computer.Name) : $($_.Exception.Message)" } }
方法2:使用Get-WmiObject(兼容旧版PowerShell)
如果你的环境还在使用PowerShell 5.1之前的版本,可以用传统的WMI命令:
$enabledComputers = Get-ADComputer -Filter {enabled -eq $true} -Properties Name, DNSHostName, OperatingSystem, LastLogonDate foreach ($computer in $enabledComputers) { try { $cpuInfo = Get-WmiObject -Class Win32_Processor -ComputerName $computer.DNSHostName -ErrorAction Stop [PSCustomObject]@{ ComputerName = $computer.Name DNSHostName = $computer.DNSHostName OperatingSystem = $computer.OperatingSystem LastLogonDate = $computer.LastLogonDate ProcessorName = $cpuInfo.Name NumberOfPhysicalCores = $cpuInfo.NumberOfCores NumberOfLogicalProcessors = $cpuInfo.NumberOfLogicalProcessors } } catch { Write-Warning "无法连接到计算机 $($computer.Name) : $($_.Exception.Message)" } }
注意事项
- 执行这些命令需要你拥有目标计算机的管理员权限,否则会因权限不足无法远程查询。
- 如果目标计算机离线,或者防火墙阻止了WMI/CIM连接(默认端口为TCP 5985/5986),会返回连接失败的警告,你可以根据提示排查网络或权限问题。
内容的提问来源于stack exchange,提问作者Carl Hussain
相关产品推荐
相关产品推荐

