我想使用get-ciminstance导出计算机数据,但还包括计算机名称
现在,我正在运行的
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用PscustomObject的好情况,它允许您动态创建具有任意属性/值的对象。
This is a good case for using a PsCustomObject, which allows you to dynamically create an object with arbitrary properties/values.
如果您想首先从远程计算机检索信息,则可以跳过计算属性,只需选择
PSComputerName
属性即可:当通过远程会话返回数据时,任何通过 WinRM 连接到远程系统的 cmdlet 都应该自动设置此属性。
如果您从本地会话运行此程序,则可以使用
Select-Object
创建计算属性,然后调用hostname
命令来填充其值:此解决方案通常是适用于跨平台脚本,因为
hostname
二进制文件在 Windows、MacOS 和大多数主要 Linux 发行版上都是开箱即用的。解释计算的属性
计算的属性通过以特定格式定义哈希表并将哈希表提供为要计算的属性来工作,就像将字符串用于对象上的真实属性一样:
请记住,在表达式 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: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 thehostname
command to populate its value: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:
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.