PrivateFunctions/Add-Entity.ps1
Function Add-Entity { [OutputType([Bool])] [CmdletBinding()] Param( [Parameter(Mandatory=$True)] [ValidateNotNull()] [String] $Name, [Parameter(Mandatory=$True)] [ValidateNotNullOrEmpty()] [String] $PluralName, [Parameter(Mandatory=$True)] [String] $Description ) Validate-Context Write-Verbose "Toevoegen van entiteit met naam '$($Name)'..." Try { $EntityObject = @{ Entity = @{ Name = $Name; PluralName = $PluralName; Description = $Description } } $EntityData = [System.Text.Encoding]::UTF8.GetBytes(($EntityObject | ConvertTo-Json)) $Response = Invoke-WebRequest -UseBasicParsing -Uri "$($DqContext.GetEntityControllerUrl())" -Method Post -Header $DqContext.Headers -Body $EntityData Return $True } Catch [System.Net.WebException] { $Stream = $_.Exception.Response.GetResponseStream() $StreamReader = New-Object System.IO.StreamReader($Stream) $StreamReader.BaseStream.Position = 0 $ResponseBody = $StreamReader.ReadToEnd() $ResponseBody = $ResponseBody | ConvertFrom-Json -ErrorAction SilentlyContinue $StreamReader.Close() $ErrorMessage = "Fout bij toevoegen van entiteit met naam '$($Name)': '$($_.Exception.Message)'." If ($ResponseBody -and $ResponseBody.Message) { $ErrorMessage = $ErrorMessage + [char]10 + [char]13 + " + DetailedMessage: $($ResponseBody.Message)" } Write-Error $ErrorMessage Return $False } } |