简体   繁体   English

在 Powershell 中将 API 结果合并为单个对象

[英]Combine API results into single object in Powershell

I have two API endpoints to hit and retrieve user info and write the data into a SQL table.我有两个 API 端点来访问和检索用户信息并将数据写入 SQL 表。 The endpoint uses employee ID in the URL so I am looping through each user to grab their data.端点在 URL 中使用员工 ID,因此我循环遍历每个用户以获取他们的数据。 Endpoint #2 contains "custom fields" for the user.端点 #2 包含用户的“自定义字段”。 I am trying to combine the returns of both endpoints and write them as a single row for each user in the SQL table.我正在尝试将两个端点的返回值组合起来,并将它们写为 SQL 表中每个用户的一行。

They return PSCustomObject as a hash table.他们将 PSCustomObject 作为哈希表返回。 Endpoint # 1: cat6 NoteProperty string cat6=NONE端点#1:cat6 NoteProperty string cat6=NONE

Endpoint #2: CustomText94 NoteProperty System.Management.Automation.PSCustomObject CustomText94=@{description=;端点 #2:CustomText94 NoteProperty System.Management.Automation.PSCustomObject CustomText94=@{description=; value=}价值=}

function Export-Feed {

    Begin {
        $serverInstance = ""
        $database = ""
        $tableName = ""
        $employees = Get-Directory | Where-Object eestatus -eq A
    }

    Process {
        $result = $employees | ForEach-Object -Parallel {
            $params = @(
                'firstname',
                'middlename',
                'lastname',
                @{n='ADID';e={(Connect-Api -apiEndpoint "/api/v1/employee/$($_.eecode)/customfield").CustomText04.value}},
                'hire_date',
                'rehire_date',
                'position_title',
                'termination_date',
                'work_email',
                'employee_code',
                'clocksequencenumber',
                'department_code',
                'department_description',
                'employee_status',
                'supervisor_primary',
                'supervisor_primary_code',
                'supervisor_secondary',
                'supervisor_secondary_code',
                'supervisor_tertiary',
                'supervisor_tertiary_code',
                'supervisor_quaternary',
                'cat1',
                'cat1desc',
                'cat2',
                'cat2desc',
                'cat3',
                'cat3desc',
                'cat4',
                'cat4desc',
                'cat5',
                'cat5desc',
                'cat6',
                'cat6desc',
                'cat7',
                'cat7desc',
                'cat8',
                'cat8desc',
                'cat9',
                'cat9desc'
            )


            Connect-Api -apiEndpoint "/api/v1/employee/$($_.eecode)" | Select-Object $params

        }
    }
    
    End {
        $result | Out-File c:\temp\test.txt
        #Write-SqlTableData -DatabaseName $database -TableName $tableName -ServerInstance $serverInstance -SchemaName dbo -InputData $result -force 
    }
}

To merge two custom objects ( [pscustomobject] instances, which are not the same as hashtables), use the following technique:合并两个自定义对象( [pscustomobject]实例中,它们是一样的哈希表),使用以下技术:

# Two sample input objects.
$o1 = [pscustomobject] @{ one  = 1; two  = 2; three = 3 }
$o2 = [pscustomobject] @{ four = 4; five = 5; six   = 6 }

# Merge the two, by appending the properties of the 2nd to the 1st.
# Note: This assumes that there is no overlap between the property names.
foreach ($prop in $o2.psobject.Properties) {
  $o1.psobject.Properties.Add($prop, $true)
}

# $o1 now contains the union of both property sets; 
# Output it.
$o1 | Format-Table

The above yields:以上产生:

one two three four five six
--- --- ----- ---- ---- ---
  1   2     3    4    5   6

The above relies on every object in PowerShell having a hidden .psobject property that provides reflection information about the object, notably the collection of all properties returned by the .Properties property.上述依靠在PowerShell中的每一个对象具有隐藏在.psobject属性提供有关该对象反射的信息,用返回的所有属性的特别是收集.Properties属性。

PowerShell allows properties to be dynamically added to any property, which is feature of its ETS (Extended Type System) . PowerShell 允许将属性动态添加到任何属性,这是其ETS(扩展类型系统)的功能 [pscustomobject] instances contain only such dynamic properties. [pscustomobject]实例包含此类动态属性。 The .Add() method allows adding a copy of another object's property to an object; .Add()方法允许将另一个对象的属性的副本添加到一个对象; the $true indicates that no validation need be performed, which speeds up the operation. $true表示不需要执行验证,这加快了操作。

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

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