简体   繁体   English

在PowerShell中将对象属性组合为一个对象

[英]combine object properties into one object in PowerShell

I am trying to combine multiple object properties into one object. 我正在尝试将多个对象属性组合到一个对象中。

When I have the following code the objects properties are combined. 当我有以下代码时,将对象属性组合在一起。

$computer = gwmi win32_computersystem | select numberOfProcessors, NumberOfLogicalProcessors, HypervisorPresent

$osInfo = gwmi win32_operatingsystem | select version, caption, serialnumber, osarchitecture

Foreach($p in Get-Member -InputObject $osInfo -MemberType NoteProperty)
{

 Add-Member -InputObject $computer -MemberType NoteProperty  -Name $p.Name -Value $osInfo.$($p.Name) -Force

}

$computer

However, if I replace the above computer and osInfo variables with 但是,如果我将上述计算机osInfo变量替换为

$computer = Get-Process | Select processname, path
$osInfo = Get-Service | Select name, status

then the $computer variables does not have the properties of the $osInfo variable after the for loop is executed. 那么执行for循环后, $computer变量将不具有$osInfo变量的属性。 ie: the second object is not combined with the first object. 即:第二个对象未与第一个对象合并。

The original code deals with cmdlets that returns two single objects relating to the same source. 原始代码处理cmdlet,该cmdlet返回与同一源有关的两个单个对象。

You're trying to use it with cmdlets that return arrays of multiple objects. 您正在尝试将其与返回多个对象数组的cmdlet一起使用。

The following basically merges the two arrays. 以下基本上合并了两个数组。

$computer = 'Server01'

$collection = @()

$services = Get-Service | Select name, status

$processes = Get-Process | Select processname, path

foreach ($service in $services) {
    $collection += [pscustomobject] @{
        ServiceName   = $service.name
        ServiceStatus = $service.status
        ProcessName   = ""
        ProcessPath   = ""
    }
}

foreach ($process in $processes) {
    $collection += [pscustomobject] @{
        ServiceName   = ""
        ServiceStatus = ""
        ProcessName   = $process.processname
        ProcessPath   = $process.path
    }
}

$collection

Personally, I'd just use the two lines for $services and $processes and be done. 就个人而言,我只需要在$services$processes使用这两行就可以了。

Your problem comes from the bad usage of Get_Member in the case of a collection. 您的问题来自在集合情况下Get_Member的错误使用。

Get-Member -InputObject ACollection gives the members of the collection. Get-Member -InputObject ACollection提供集合的成员。

ACollection | Get-Member ACollection | Get-Member gives the members of each element of the collection. ACollection | Get-Member给出集合中每个元素的成员。

So in you case it will work with : 因此,在您的情况下,它将适用于:

Foreach($p in ($osInfo | Get-Member  -MemberType NoteProperty))
{
}

Edited 已编辑

$computer is also à collection so what do you expect. $computer也是àcollection,所以您期望什么。 I add the code of what I think you expect. 我添加了我认为您期望的代码。

$processes = Get-Process | Select processname, path, Id

Foreach($p in $processes)
{
 $services = Get-WmiObject "Win32_Service" -filter "ProcessId=$($p.Id)"
 Add-Member -InputObject $p -MemberType NoteProperty  -Name "Services" -Value $(($services | % {$_.name}) -join ',') -Force
}

$processes

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

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