PrivateFunctions/Upload-EntitiesToApi.ps1
Function Upload-EntitiesToApi { [CmdletBinding()] Param( [Parameter(Mandatory=$True)] [ValidateNotNull()] [Object[]] $EntitiesToUpload ) Validate-Context [Array]$ExistingCustomEntities= Get-DqEntities -CustomOnly $Imported = 0; Write-Verbose "Uploaden van entiteiten naar DQ Monitor API..." ForEach($Entity in $EntitiesToUpload) { $ExistingEntity = $ExistingCustomEntities | Where-Object { $_.Name -eq $Entity.Entity.Name } $EntityData = [System.Text.Encoding]::UTF8.GetBytes(($Entity | ConvertTo-Json)) Try { Write-Verbose "Entiteit '$($Entity.entity.name)' uploaden..." If ($ExistingEntity) { $Url = "$($DqContext.GetEntityControllerUrl())/$($ExistingEntity.Id)" $Response = Invoke-WebRequest -UseBasicParsing -Uri $Url -Method Put -Header $DqContext.Headers -Body $EntityData } Else { $Response = Invoke-WebRequest -UseBasicParsing -Uri $DqContext.GetEntityControllerUrl() -Method Post -Header $DqContext.Headers -Body $EntityData } If ($ExistingEntity) { Write-Host "Entiteit '$($Entity.entity.Name)' is geüpdatet." } Else { Write-Host "Entiteit '$($Entity.entity.Name)' is opgevoerd." } $Imported += 1; } Catch [System.Net.WebException] { $Stream = $_.Exception.Response.GetResponseStream() $StreamReader = New-Object System.IO.StreamReader($Stream) $StreamReader.BaseStream.Position = 0 $ResponseBody = $StreamReader.ReadToEnd() | ConvertFrom-Json -ErrorAction SilentlyContinue $StreamReader.Close() $ErrorMessage = "Fout bij opvoeren/uploaden entiteit '$($Entity.entity.name)': '$($_.Exception.Message)'." If ($ResponseBody -and $ResponseBody.Message) { $ErrorMessage = $ErrorMessage + [char]10 + [char]13 + " + DetailedMessage: $($ResponseBody.Message)" } Write-Warning $ErrorMessage } } Write-Host "$($Imported) entiteit(en) opgevoerd." } |