Get-CimInstance CIM_Processor 返回多个“名称”

发布于 2025-01-19 20:53:20 字数 873 浏览 1 评论 0原文

我正在尝试获取我域中所有计算机的 cpu 名称。我正在运行以下命令,

Get-CimInstance -ComputerName $i.DNSHostName -Class CIM_Processor | Select-Object "PSComputerName", "Name", "NumberOfCores"

我正在使用此结果来构建自定义对象

 $workstationWithIp = [PSCustomObject]@{
            ComputerName = $i.DNSHostName
            CPU = $workstationObj.Name
            Cores = $workstationObj.NumberOfCores
            IP = $ip
            Memory = $memory
            Uptime = $uptimeHours
            OS = $os.Caption + " " + $os.Version
        }

我的问题是,某些机器有多个处理器,因此“名称”将返回多个结果。所有这些服务器都有多个相同的模型,那么我怎样才能得到结果,以便我可以将它作为字符串放入我的自定义对象中?我正在使用这些自定义对象来制作 CSV,其中一些将具有 cpu 名称和核心,但具有多个 cpu 的对象将为我提供 System.Object[] 作为 csv 中的名称和核心计数

输入图像描述这里

I'm trying to get the cpu name for all of the machines on my domain. I'm running the following

Get-CimInstance -ComputerName $i.DNSHostName -Class CIM_Processor | Select-Object "PSComputerName", "Name", "NumberOfCores"

I'm taking the results of this to build a custom object

 $workstationWithIp = [PSCustomObject]@{
            ComputerName = $i.DNSHostName
            CPU = $workstationObj.Name
            Cores = $workstationObj.NumberOfCores
            IP = $ip
            Memory = $memory
            Uptime = $uptimeHours
            OS = $os.Caption + " " + $os.Version
        }

My issue is that some of the machines have more than one processor so the "Name" will return multiple results. All of these servers have multiple of the same model so how can I get the result knocked down so I can put it into my custom object as a string? I'm using those custom objects to make a CSV and some will have the cpu name and the core fine but the ones with multiple cpus will give me System.Object[] for the name and core count in the csv

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2025-01-26 20:53:20

正如评论中提到的,从潜在的数组/值集合中选择最多 1 个项目的安全方法是通过管道传输到 Select-Object -First 1

[PSCustomObject]@{
    ComputerName = $i.DNSHostName
    CPU = $workstationObj.Name |Select-Object -First 1
    Cores = $workstationObj.NumberOfCores |Select-Object -First 1
    IP = $ip
    Memory = $memory
    Uptime = $uptimeHours
    OS = $os.Caption + " " + $os.Version
}

As mentioned in the comments, a safe way to pick at most 1 item from a potential array/collection of values is by piping to Select-Object -First 1:

[PSCustomObject]@{
    ComputerName = $i.DNSHostName
    CPU = $workstationObj.Name |Select-Object -First 1
    Cores = $workstationObj.NumberOfCores |Select-Object -First 1
    IP = $ip
    Memory = $memory
    Uptime = $uptimeHours
    OS = $os.Caption + " " + $os.Version
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文