Public/Get-SyncHrManagerAsync.ps1
function Get-SyncHrManagerAsync { [CmdletBinding(PositionalBinding=$false)] param ( [Parameter(Mandatory=$true)][hashtable]$AuthHeader, [Parameter(Mandatory=$true)][object[]]$PersonDataInput, [Parameter(Mandatory=$false)][string]$BaseUrl = 'https://clients.synchr.com/synchr' ) Write-Log -LogText "Starting function" -LogType normal if ($AuthHeader.Authorization -notmatch 'SHR apiKey') { Write-Log -LogText "Invalid AuthHeader. Use Get-SyncHrAuthHeader first." throw "Invalid AuthHeader. Use Get-SyncHrAuthHeader first." return } $activeEmps = $PersonData | ? {$_.emplStatus -eq 'A' -and $_.empNo.Length -gt 3} $writeLogDef = ${function:Write-Log}.ToString() $progressObj = @{ count = $activeEmps.Count countDown = $activeEmps.Count countUp = 0 } $logFilePath = $Global:LogPath try { $activeEmps | Foreach-Object -ThrottleLimit 40 -Parallel { ${function:Write-Log} = $using:writeLogDef $empNo = $PSItem.empNo $first = $PSItem.fname $last = $PSItem.lname Write-Progress -Activity "Getting user's Manager: $empNo $first $last" -Status "$(($USING:progressObj).countDown) Users Remaining.." ` -PercentComplete (($(($USING:progressObj).countUp) / $($USING:progressObj.count)) * 100) -ErrorAction Ignore $($USING:progressObj).countDown-- $($USING:progressObj).countUp++ $empNoFilter = "%7b%22empNo%22%3a%5b%22eq%22%2c%22$($empNo)%22%5d%7d" $url = "$($USING:BaseUrl)/api/1.0/manager/list?filter=$empNoFilter" $errorObj = $null try { $measure = Measure-Command {$response = Invoke-RestMethod -Method: Get -Uri $url -Headers $($USING:AuthHeader) -ContentType 'application/json'} $PSItem.manager_empNo = $response.manager.EmpNo $PSItem.manager_netId = $response.manager.netId $PSItem.manager_name = $response.manager.name $PSItem.manager_role = $response.manager.role $PSItem.manager_respTime = $measure.ToString() } catch { $errorObj = $_ } if ($errorObj) { $statusCode = $errorObj.Exception.Response.StatusCode.value__ $PSItem.manager_errorMsg = $errorObj.Exception.message Write-Log "Error getting manager for $empNo $first $last" -LogType: error -ErrorObject $errorObj -LogFolderPath $($USING:logFilePath) -FunctionName 'Get-SyncHrManagerAsync' if ($statusCode -eq 401) { return } else { continue } } } Write-Progress -Activity "Complete" -Completed: $true -Status "Complete" -PercentComplete 100 -SecondsRemaining 0 } catch { Write-Log "Unhandled exception" -LogType: error -ErrorObject $_ throw $_ } Write-Progress -Activity "Complete" -Completed: $true return $PersonData } |