I have a process to interact with an API and write the data to a SQL table. Hitting the two endpoints separately and getting the data is no issue. I am stuck now on how to combine them so I can make one write to SQL as the loop processes. Endpoint #1 gets me all the data “/api/v1/employee/$($.eecode)" . I need to hit Endpoint #2 "/api/v1/employee/$($.eecode)/customfield”
with the same eecode from the first call. I want to keep them in the same object so I can write the row to SQL with data from both endpoints. I had to remove some code since the API for the system I am interacting with isn’t public.
function Export-Feed {
Begin {
$serverInstance = “”
$database = “”
$tableName = “”
$employees = Get-Directory | Where-Object eestatus -eq A
}
Process {
$result = $employees | ForEach-Object -Parallel {
$user = Connect-Api -apiEndpoint “/api/v1/employee/$($_.eecode)”
[pscustomobject][ordered]@{
‘firstname’ = $user.firstname
‘middlename’ = $user.middlename
‘lastname’ = $user.lastname
‘ADID’ = (Connect-Api -apiEndpoint “/api/v1/employee/$($_.eecode)/customfield”).CustomText04.value
‘hire_date’ = $use.hire_date
‘rehire_date’ = $user.rehiredate
‘position_title’ = $user.position_title
‘termination_date’ = if ($null) { $_.termination_date }else { “NULL” }
‘work_email’ = $user.work_email
’employee_code’ = $user.employee_code
‘clocksequencenumber’ = $user.clocksequencenumber
‘department_code’ = $user.department_code
‘department_description’ = $user.department_description
’employee_status’ = $user.employee_status
‘supervisor_primary’ = $user.supervisor_primary
‘supervisor_primary_code’ = $user.supervisor_primary_code
‘supervisor_secondary’ = $user.supervisor_secondary
‘supervisor_secondary_code’ = $user.supervisor_secondary_code
‘supervisor_tertiary’ = $user.supervisor_tertiary
‘supervisor_tertiary_code’ = $user.supervisor_tertiary_code
‘supervisor_quaternary’ = $user.supervisor_quaternary
‘cat1’ = $user.cat1
‘cat1desc’ = $user.cat1desc
‘cat2’ = $user.cat2
‘cat2desc’ = $user.cat2desc
‘cat3’ = $user.cat3
‘cat3desc’ = $user.cat3desc
‘cat4’ = $user.cat4
‘cat4desc’ = $user.cat4desc
‘cat5’ = $user.cat5
‘cat5desc’ = $user.cat5desc
‘cat6’ = $user.cat6
‘cat6desc’ = $user.cat6desc
‘cat7’ = $user.cat7
‘cat7desc’ = $user.cat7desc
‘cat8’ = $user.cat8
‘cat8desc’ = $user.cat8desc
‘cat9’ = $user.cat9
‘cat9desc’ = $user.cat9desc
}
}
}
End {
#$result | Out-File c:\temp\test.txt
Write-SqlTableData -DatabaseName $database -TableName $tableName -ServerInstance $serverInstance -SchemaName dbo -InputData $result -force
}
}