我想使用get-ciminstance导出计算机数据,但还包括计算机名称

发布于 2025-01-18 22:18:39 字数 250 浏览 2 评论 0原文

现在,我正在运行的

Get-CimInstance -ComputerName $i.DNSHostName -Class CIM_Processor | Select-Object "Name", "NumberOfCores" | Export-Csv -Path .\test.csv -NoTypeInformation -Append`

是,将CPU名称和核心计数放在CSV罚款中,但是如何将主机名添加为另一列?

Right now I'm running

Get-CimInstance -ComputerName $i.DNSHostName -Class CIM_Processor | Select-Object "Name", "NumberOfCores" | Export-Csv -Path .\test.csv -NoTypeInformation -Append`

This will put the CPU name and core count in a CSV fine but how can I add in the hostname as another column?

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

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

发布评论

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

评论(2

空城旧梦 2025-01-25 22:18:39

这是使用PscustomObject的好情况,它允许您动态创建具有任意属性/值的对象。

$cimProcessor = Get-CimInstance -ComputerName $hostName -Class CIM_Processor

$row = [PSCustomObject]@{
    ComputerName = $hostName
    Name = $cimProcessor.Name
    NumberOfCores = $cimProcessor.NumberOfCores
}

$row | Export-Csv -Path test.csv -NoTypeInformation -Append

This is a good case for using a PsCustomObject, which allows you to dynamically create an object with arbitrary properties/values.

$cimProcessor = Get-CimInstance -ComputerName $hostName -Class CIM_Processor

$row = [PSCustomObject]@{
    ComputerName = $hostName
    Name = $cimProcessor.Name
    NumberOfCores = $cimProcessor.NumberOfCores
}

$row | Export-Csv -Path test.csv -NoTypeInformation -Append
温柔戏命师 2025-01-25 22:18:39

如果您想首先从远程计算机检索信息,则可以跳过计算属性,只需选择 PSComputerName 属性即可:

注意:省略号...表示原始示例之前或之后的代码。

... | Select-Object PSComputerName, Name, NumberOfCores | ...

当通过远程会话返回数据时,任何通过 WinRM 连接到远程系统的 cmdlet 都应该自动设置此属性。


如果您从本地会话运行此程序,则可以使用 Select-Object 创建计算属性,然后调用 hostname 命令来填充其值:

... | Select-Object "Name", "NumberOfCores", @{
  Name = 'ComputerName';
  Expression = { hostname };
} | ...

此解决方案通常是适用于跨平台脚本,因为 hostname 二进制文件在 Windows、MacOS 和大多数主要 Linux 发行版上都是开箱即用的。


解释计算的属性

计算的属性通过以特定格式定义哈希表并将哈希表提供为要计算的属性来工作,就像将字符串用于对象上的真实属性一样:

$property = @{
  Name = 'PropertyName';
  Expression = { 'ScriptBlock' };
}

# Note that the hashtable can be specified inline as shown above
# or as a variable like shown here
[PSCustomObject]@{ Name = 'Bender'; Loves = 'Bending' } |
  Select-Object Name, Loves, $property

请记住,在表达式 ScriptBlock 中、$PSItem/$_ 设置为管道中的当前对象。使用它来引用您从中选择信息的当前对象的静态或实例属性。

In the case where you want to retrieve information from a remote machine first, then you can skip the calculated property and simply select the PSComputerName property instead:

Note: The ellipses ... indicate the code before or after from your original sample.

... | Select-Object PSComputerName, Name, NumberOfCores | ...

Any cmdlet which connects to remote systems via WinRM should have this property automatically set when data is returned over a remote session.


If you are running this from a local session, you could use Select-Object to create a calculated property, then call the hostname command to populate its value:

... | Select-Object "Name", "NumberOfCores", @{
  Name = 'ComputerName';
  Expression = { hostname };
} | ...

This solution is often suitable for cross platform scripts since a hostname binary is available out of the box on Windows, MacOS, and most major distributions of Linux.


Explaining Calculated Properties

Calculated properties work by defining a hashtable in a specific format and providing the hashtable as a property to be computed just as you would use a string for a real property on the object:

$property = @{
  Name = 'PropertyName';
  Expression = { 'ScriptBlock' };
}

# Note that the hashtable can be specified inline as shown above
# or as a variable like shown here
[PSCustomObject]@{ Name = 'Bender'; Loves = 'Bending' } |
  Select-Object Name, Loves, $property

Keep in mind that within the Expression ScriptBlock, $PSItem/$_ is set to the current object in the pipeline. Use this to reference static or instance properties from the current object you are selecting information from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文