ein*_*ica 7 windows cpu powershell windows-10
假设我在 Windows 计算机上运行(电源)shell。
有没有我可以用来获得的单行:
注意:我只想要一个数字作为命令输出,而不是任何标题或文本。
您可以使用[Environment]::ProcessorCount来获取逻辑处理器计数。它也适用于非 Windows。
https://learn.microsoft.com/en-us/dotnet/api/system.environment.processorcount
\n
笔记:
\n下面的答案假设具有多个内核的单个处理器(插槽)。
\n注意:以下内容是从文档中收集的;我个人无法验证。根据 CIM/WMI 类的文档,每个处理器(插槽)Win32_Processor都存在该类的实例,因此,如果您的系统安装了多个处理器(每个处理器都有多个内核),请使用类似以下内容来获取逻辑的总数核心 ( ); 用于物理核心:. NumberOfLogicalProcessors.NumberOfCores
$totalLogicalCores = (\n (Get-CimInstance \xe2\x80\x93ClassName Win32_Processor).NumberOfLogicalProcessors |\n Measure-Object -Sum\n).Sum\nRun Code Online (Sandbox Code Playgroud)\n.NumberOfEnabledCores属性(Windows 10+、Windows Server 2016+),它指的是已启用的物理核心。超线程倍增因子似乎没有明显的属性,因此为了确定启用的逻辑核心数,您可能必须使用.NumberOfEnabledCores * (.ThreadCount / .NumberOfCores)Ran Turner 的答案提供了关键的指导,但可以通过两种方式进行改进:
\nCIM cmdlet(例如,Get-CimInstance)取代了 PowerShell v3(2012 年 9 月发布)中的 WMI cmdlet(例如,Get-WmiObject)。因此,应该避免使用 WMI cmdlet,尤其是因为未来所有工作都将集中在的 PowerShell(核心)(v6+)甚至不再拥有它们。但请注意,WMI 仍然是CIM cmdlet 的基础。有关更多信息,请参阅此答案。
Format-Table与所有Format-*cmdlet 一样,旨在为人类观察者生成用于显示的格式,而不是输出适合以后编程处理的数据(有关更多信息,请参阅此答案)。
Select-ObjectFormat-TableFormat-List所以:
\n# Creates a [pscustomobject] instance with \n# .NumberOfCores and .NumberOfLogicalProcessors properties.\n$cpuInfo =\n Get-CimInstance \xe2\x80\x93ClassName Win32_Processor | \n Select-Object -Property NumberOfCores, NumberOfLogicalProcessors\n\n# Save the values of interest in distinct variables, using a multi-assignment.\n# Of course, you can also use the property values directly.\n$coreCountPhysical, $coreCountLogical = $cpuInfo.NumberOfCores, $cpuInfo.NumberOfLogicalProcessors\nRun Code Online (Sandbox Code Playgroud)\n当然,如果您只对值感兴趣(CPU 仅算作数字),则不需要中间对象,并且可以省略Select-Object上面的调用。
至于单行:
\n如果您想要一个能够创建不同变量的单行代码,而不需要重复昂贵的Get-CimInstance调用,则可以使用 aux. 利用 PowerShell 将赋值用作表达式的能力的变量:
$coreCountPhysical, $coreCountLogical = ($cpuInfo = Get-CimInstance -ClassName Win32_Processor).NumberOfCores, $cpuInfo.NumberOfLogicalProcessors\nRun Code Online (Sandbox Code Playgroud)\n要将数字保存在不同的变量中并输出它们(将它们作为 2 元素数组返回),请将整个语句括在(...).
要仅输出数字,只需省略该$coreCountPhysical, $coreCountLogical =部分即可。
| 归档时间: |
|
| 查看次数: |
8948 次 |
| 最近记录: |