1

我有两个 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 
    }
}
4

1 回答 1

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(扩展类型系统)的功能。 [pscustomobject]实例包含这样的动态属性。该.Add()方法允许将另一个对象的属性的副本添加到对象;表示$true不需要执行验证,这加快了操作。

于 2020-10-15T20:20:47.163 回答