简体   繁体   English

如何在PowerShell中将新属性动态添加到自定义对象

[英]How to dynamically add new properties to custom object in PowerShell

I have a custom object that contains device information that looks something like this. 我有一个自定义对象,其中包含看起来像这样的设备信息。

name,model,sn
PC1,Elitebook 850, ABC123,
PC2,EliteDesk 600,123ABC

I have a function that retrieves threats detected by an antivirus product. 我有一个功能,可以检索防病毒产品检测到的威胁。 These are returned as an array of objects. 它们作为对象数组返回。 There are more properties than below but this is just an example 有比以下更多的属性,但这只是一个例子

file,md5
bad.exe,adfdfdfd
evil.exe,fdfdffdf

I would like to add each member as properties to the custom object so the final output is similar to this. 我想将每个成员添加为自定义对象的属性,因此最终输出与此类似。

name,model,sn,01_file,01_md5,02_file,02_md5

Currently, my script does this: 目前,我的脚本执行此操作:

foreach($device in $devices){
    $threats = Get-Threats $device
    [pscustomobject] @{
        name = $device.device_name
        make = $device.make
        sn = $device.sn
        ThreatFileName = $threats.File -join ", "
        Threat_md5 = $threats.md5 -join ", "
    }
}

This works ok but I'd really like each object returned by the 'Get-Threats' function to be listed as its own set of properties. 这工作正常,但我真的希望'Get-Threats'函数返回的每个对象都被列为它自己的一组属性。 I need this to be generated dynamically because I don't know how many threats will be returned for each device. 我需要动态生成它,因为我不知道每个设备将返回多少威胁。

Any thoughts on how to go about this? 有关如何去做的任何想法?

You can add properties to objects at any time with the Add-Member cmdlet. 您可以随时使用Add-Member cmdlet向对象添加属性。 Maybe start with an empty object and loop through the elements returned by Get-Threats, adding a member each time? 也许从一个空对象开始并循环遍历Get-Threats返回的元素,每次都添加一个成员?

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-member?view=powershell-6 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-member?view=powershell-6

Edit: Example code to answer for reference. 编辑:回答示例代码以供参考。

$o = [pscustomobject]@{
    MemberA='aaa'
    MemberB='bbb'
    MemberC='ccc'
    MemberD='ddd'
}

"Before"
$o | ft

$o | Add-Member -MemberType NoteProperty -Name 'MemberE' -Value 'eee'
$o | Add-Member -MemberType NoteProperty -Name 'MemberF' -Value 'fff'

"After"
$o | ft

The answer from @krome got me pointed in the right direction although that answer wouldn't work for me as there could be multiple threats for each device. @krome的回答让我指出了正确的方向,尽管这个答案对我不起作用,因为每个设备可能存在多个威胁。

I used the answer from @scobi on Dynamically get PSCustomObject property and values to arrive at this answer which meets my requirement that the new properties be generated dynamically. 我使用@scobi上的答案动态获取PSCustomObject属性和值来得到这个满足我要求动态生成新属性的要求的答案。

foreach($device in $devices){
    $threats = Get-Threats $device
    if($null -ne $threats){
        $i = 1
        foreach($threat in $threats){
            $threat | Get-Member -MemberType NoteProperty | % Name | %{                
                Add-Member -InputObject $device -NotePropertyName ("Threat"+$i.ToString() + "_" + $_) -NotePropertyValue $threat.$_ -Force            
            }
            $i++
        }
    }
}
Write-Output $devices
  • I loop over each device in the devices array and call my Get-Threats function. 我遍历devices阵列中的每个设备并调用我的Get-Threats功能。
  • The if statement prevents the loop from running for any devices that don't have threats. if语句阻止循环为任何没有威胁的设备运行。
  • $i is used as my counter to increment the property name for each threat found so the properties will all have unique names $i用作我的计数器来增加找到的每个威胁的属性名称,因此属性都具有唯一的名称
  • I then loop over each threat found piping to Get-Member to retrieve the property name and values 然后我将找到的每个威胁循环到Get-Member以检索属性名称和值
  • I use Add-Member to add additional properties for each threat found to each device in the loop, using the counter to give each propery a unique name 我使用Add-Member为循环中每个device发现的每个威胁添加其他属性,使用计数器为每个属性提供唯一的名称

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

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