将 API 个结果合并到 Powershell 中的单个对象中
Combine API results into single object in Powershell
我有两个 API 端点来访问和检索用户信息并将数据写入 SQL table。端点在 URL 中使用员工 ID,因此我遍历每个用户以获取他们的数据。端点 #2 包含用户的“自定义字段”。我正在尝试合并两个端点的 returns 并将它们写为 SQL table.
中每个用户的一行
他们return PSCustomObject 作为散列table。
端点 # 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
}
}
要合并两个自定义对象([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
[= 返回的所有属性的集合32=].
PowerShell 允许将属性动态添加到任何 属性,这是其 ETS (Extended Type System) 的特性。
[pscustomobject]
个实例仅包含 个这样的动态属性。
.Add()
方法允许将另一个对象的 属性 的副本添加到一个对象; $true
表示不需要校验,这样可以加快运算速度。
我有两个 API 端点来访问和检索用户信息并将数据写入 SQL table。端点在 URL 中使用员工 ID,因此我遍历每个用户以获取他们的数据。端点 #2 包含用户的“自定义字段”。我正在尝试合并两个端点的 returns 并将它们写为 SQL table.
中每个用户的一行他们return PSCustomObject 作为散列table。 端点 # 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
}
}
要合并两个自定义对象([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
[= 返回的所有属性的集合32=].
PowerShell 允许将属性动态添加到任何 属性,这是其 ETS (Extended Type System) 的特性。
[pscustomobject]
个实例仅包含 个这样的动态属性。
.Add()
方法允许将另一个对象的 属性 的副本添加到一个对象; $true
表示不需要校验,这样可以加快运算速度。