【问题标题】:Combine API results into single object in Powershell将 API 结果合并到 Powershell 中的单个对象中
【发布时间】:2020-10-15 16:27:16
【问题描述】:

我有两个 API 端点来访问和检索用户信息并将数据写入 SQL 表。端点在 URL 中使用员工 ID,因此我遍历每个用户以获取他们的数据。端点 #2 包含用户的“自定义字段”。我正在尝试将两个端点的返回结合起来,并将它们写为 SQL 表中每个用户的单行。

它们将 PSCustomObject 作为哈希表返回。 端点 #1:cat6 NoteProperty 字符串 cat6=NONE

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

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 
    }
}

【问题讨论】:

  • @mklement0 明白了。堆栈溢出的新功能。我试着给你的答案功劳。它说有,但由于我的帐户级别,它没有公开显示。谢谢您的帮助。我接受了您对 [pscustomobject] 的解释,并得到了我满意的工作结果。
  • 很高兴听到这个消息,@wimp777 - 感谢您的接受。一旦您达到 15 个声望点,您的赞成票就会显示出来。

标签: powershell pscustomobject


【解决方案1】:

要合并两个自定义对象([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

以上产出:

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

以上内容依赖于 PowerShell 中的每个对象都具有隐藏的 .psobject 属性,该属性提供有关对象的反射信息,特别是 .Properties 属性返回的所有属性的集合。

PowerShell 允许将属性动态添加到任何属性,这是其ETS (Extended Type System) 的功能。 [pscustomobject] 实例包含此类动态属性。 .Add() 方法允许将另一个对象的属性的副本添加到一个对象; $true 表示不需要进行验证,从而加快了操作速度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 2017-11-18
    • 2016-04-25
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多