Public/New-JuribaCustomField.ps1
|
function New-JuribaCustomField { <# .SYNOPSIS Creates a new custom field. .DESCRIPTION Creates a new custom field using the Dashworks API v1, Supporting special characters in the naming. .PARAMETER Instance Optional. Juriba instance to be provided if not authenticating using Connect-Juriba. For example, https://myinstance.dpc.juriba.app .PARAMETER APIKey Optional. API key to be provided if not authenticating using Connect-Juriba. .PARAMETER Name Name of the new custom field. .PARAMETER CSVColumnHeader CSV Column Header for the new custom field. Must be unique and no more than 50 characters. The Juriba DPC UI only allows letters, numbers and the characters _ # $ % & @ -. Headers with other characters, such as spaces, are accepted by the API but may need to be changed before the custom field can be saved in the UI. .PARAMETER Type Type of the new custom field. One of Text, MultiText, LargeText, Number or Date. .PARAMETER ObjectTypes Object types that this new custom field applies to. Accepts multiple selections. One or more of Device, User, Application, Mailbox. .PARAMETER IsActive Set the new custom field to active or inactive. Defaults to Active. .PARAMETER AllowUpdate Optional. Sets the type of updates allowed for this custom field. Either Directly or ETL. Defaults to Directly. .PARAMETER AllowExternalUpdate Optional. Sets whether the custom field allows external updates. Defaults to $true. .OUTPUTS Text "Custom field created" .EXAMPLE PS> New-JuribaCustomField -ObjectTypes Device, User -Name "MyNewCustomField" -CSVColumnHeader "mynewcustomfield" -Type Text -IsActive $true -AllowUpdate ETL -Instance "https://myinstance.dpc.juriba.app" -APIKey "xxxxx" #> [alias("New-DwCustomField")] [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory=$false)] [string]$Instance, [Parameter(Mandatory=$false)] [string]$APIKey, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Name, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$CSVColumnHeader, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [ValidateSet("Text", "MultiText", "LargeText", "Number", "Date")] [string]$Type, [Parameter(Mandatory=$true)] [ValidateSet("Device", "User", "Application", "Mailbox")] [string[]]$ObjectTypes, [Parameter(Mandatory=$false)] [bool]$IsActive = $true, [Parameter(Mandatory=$false)] [ValidateSet("Directly", "ETL")] [string]$AllowUpdate = "Directly", [Parameter(Mandatory=$false)] [bool]$AllowExternalUpdate = $true ) if ((Get-Variable 'dwConnection' -Scope 'Global' -ErrorAction 'Ignore') -and !$APIKey -and !$Instance) { $APIKey = ConvertFrom-SecureString -SecureString $dwConnection.secureAPIKey -AsPlainText $Instance = $dwConnection.instance } if ($APIKey -and $Instance) { $typeId = switch($Type) { "Text" {1} "MultiText" {2} "LargeText" {3} "Number" {5} "Date" {4} } $updateSourceId = switch($AllowUpdate) { "Directly" {1} "ETL" {2} } $payload = @{} $payload.Add("allowExternalUpdate", $AllowExternalUpdate) $payload.Add("csvUpdateColumnHeader", $CSVColumnHeader) $payload.Add("IsActive", $IsActive) $payload.Add("isApplicationField", ($ObjectTypes -contains "Application")) $payload.Add("isComputerField", ($ObjectTypes -contains "Device")) $payload.Add("isMailboxField",($ObjectTypes -contains "Mailbox")) $payload.Add("isUserField", ($ObjectTypes -contains "User")) $payload.Add("name", $Name) $payload.Add("valueTypeId", $typeId) $payload.Add("updateSourceId", $updateSourceId) $jsonbody = $payload | ConvertTo-Json $uri = "{0}/apiv1/custom-fields" -f $Instance $headers = @{'x-api-key' = $APIKey } try { if ($PSCmdlet.ShouldProcess($Name)) { $result = Invoke-WebRequest -Uri $uri -Method POST -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($jsonbody)) -ContentType 'application/json' if ($result.StatusCode -eq 200) { Write-Information "Custom field created" -InformationAction Continue } else { throw "Error in custom field creation" } } } catch { if ($_.Exception.Response.StatusCode.Value__ -eq 409) { Write-Error ("{0}" -f (($_ | ConvertFrom-Json).detail)) } else { Write-Error $_ } } } else { Write-Error "No connection found. Please ensure `$APIKey and `$Instance is provided or connect using Connect-Juriba before proceeding." } } |