Public/Set/Set-NinjaOneDeviceCustomFields.ps1
function Set-NinjaOneDeviceCustomFields { <# .SYNOPSIS Sets the value of the specified device custom fields. .DESCRIPTION Sets the value of the specified device custom fields using the NinjaOne v2 API. .FUNCTIONALITY Device Custom Fields .OUTPUTS A powershell object containing the response. .EXAMPLE PS> Set-NinjaOneDeviceCustomFields -deviceId 1 -customFields @{ CustomField1 = 'Value1'; CustomField2 = 'Value2' } Set `CustomField1` to `Value1` and `CustomField2` to `Value2` respectively for the device with id 1. #> [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')] [OutputType([Object])] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', Justification = 'Uses dynamic parameter parsing.')] Param( # The device to set the custom field value(s) for. [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)] [Alias('id')] [Int]$deviceId, # The custom field(s) body object. [Parameter(Mandatory, Position = 1, ValueFromPipelineByPropertyName)] [Alias('customFields', 'body')] [Object]$deviceCustomFields ) try { $Device = Get-NinjaOneDevice -deviceId $deviceId if ($Device) { Write-Verbose ('Setting custom fields for device {0}.' -f $Device.SystemName) $Resource = ('v2/device/{0}/custom-fields' -f $deviceId) } else { throw ('Device with id {0} not found.' -f $deviceId) } $RequestParams = @{ Resource = $Resource Body = $deviceCustomFields } if ($PSCmdlet.ShouldProcess(('Custom Fields for {0}' -f $Device.SystemName), 'Set')) { $CustomFieldUpdate = New-NinjaOnePATCHRequest @RequestParams if ($CustomFieldUpdate -eq 204) { $OIP = $InformationPreference $InformationPreference = 'Continue' Write-Information ('Custom fields for {0} updated successfully.' -f $Device.SystemName) $InformationPreference = $OIP } } } catch { New-NinjaOneError -ErrorRecord $_ } } |