简体   繁体   English

打印出核心数

[英]Print out the number of cores

I want to print out the number of cores.我想打印出核心数。

$N_cores = Get-WmiObject –class Win32_processor | ft NumberOfCores 
Write-Host $N_cores | Select-Object -Property NumberOfCores

This give me following:这给了我以下信息:

Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Mi
crosoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Micros
oft.PowerShell.Commands.Internal.Format.FormatEndData

How can I display the NumberOfCores?如何显示 NumberOfCores?

you need to extract just the value of the cores, try this你只需要提取核心的价值,试试这个

$N_cores = Get-WmiObject –class Win32_processor | Select-Object -ExpandProperty NumberOfCores

Write-Host "Number of cores is: $N_cores"

The confusing ouput you're seeing comes from ft (alias for Format-Table ).您看到的令人困惑的输出来自ftFormat-Table别名)。

To trim the number of properties you want from Get-WmiObject , use Select-Object instead.要从Get-WmiObject修剪所需的属性数量,请改用Select-Object

For example, to store the NumberOfCores property in $N_cores , and subsequently display only its raw value you could do:例如,要将NumberOfCores属性存储在$N_cores中,然后仅显示其原始值,您可以这样做:

# This will create an object with a `NumberOfCores` property
$N_cores = Get-WmiObject –class Win32_processor |Select-Object NumberOfCores 

# print just the value of the property
Write-Host $N_cores.NumberOfCores

There's a few issues here这里有几个问题

  1. You are calling Format-Table Format-Table returns an output of format objects您正在调用 Format-Table Format-Table 返回格式对象的 output
  2. You are Piping your Write-Output to Select-Object, which is generally not how you want to got about this...您正在将您的写入输出传递给 Select-Object,这通常不是您想要的方式......

I suggest you follow the best practice of Filter Left, Format Right我建议您遵循Filter Left, Format Right的最佳实践

When you pass those objects into the pipe to another function, that function attempt to interpret those objects, if you pass format object to the select-object function it no-longer has the NumberOfCores property. When you pass those objects into the pipe to another function, that function attempt to interpret those objects, if you pass format object to the select-object function it no-longer has the NumberOfCores property.

Ultimately the code that you would want is最终你想要的代码是

`$N_cores = Get-WmiObject –class Win32_processor | Select-Object -Property NumberOfCores
`Format-Table $N_cores |Write-Host

It should also be mentioned that if you intend to do anything with this script besides print to the console, you should not use Format-Table or Write-Host, instead use `Write-Output $N_cores as your second line.还应该提到的是,如果您打算使用此脚本执行除打印到控制台之外的任何操作,则不应使用 Format-Table 或 Write-Host,而应使用 `Write-Output $N_cores 作为第二行。 This will write the result not only to the console, but also pass the objects in the $N_cores variable to the pipeline Write-Host vs Write-Output这不仅会将结果写入控制台,还会将 $N_cores 变量中的对象传递给管道Write-Host vs Write-Output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM