从2个cmdlet输出的选择对象 [英] Select-Object with output from 2 cmdlets
本文介绍了从2个cmdlet输出的选择对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我具有以下PowerShell脚本:
Suppose I have the following PowerShell script:
Get-WmiObject -Class Win32_Service |
Select DisplayName,@{Name="PID";Expression={$_.ProcessID}} |
Get-Process |
Select Name,CPU
这将:
第1行:在本地计算机上获取所有服务
Line 1: Get all services on the local machine
第2行:使用DisplayName和PID创建一个新对象.
Line 2: Create a new object with the DisplayName and PID.
第3行:调用Get-Process获取有关每个服务的信息.
Line 3: Call Get-Process for information about each of the services.
第4行:使用进程名称和CPU使用率创建一个新对象.
Line 4: Create a new object with the Process Name and CPU usage.
但是,在第4行中,我也想拥有在第2行中获得的DisplayName-可以吗?
However, in Line 4 I want to also have the DisplayName that I obtained in Line 2 - is this possible?
推荐答案
一种方法是在收集所需属性后输出自定义对象.示例:
One way to do this is to output a custom object after collecting the properties you want. Example:
Get-WmiObject -Class Win32_Service | foreach-object {
$displayName = $_.DisplayName
$processID = $_.ProcessID
$process = Get-Process -Id $processID
new-object PSObject -property @{
"DisplayName" = $displayName
"Name" = $process.Name
"CPU" = $process.CPU
}
}
这篇关于从2个cmdlet输出的选择对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文