Public/New-JuribaImportDevice.ps1
|
#Requires -Version 7 function New-JuribaImportDevice { [alias("New-DwImportDevice")] <# .SYNOPSIS Creates a new Dashworks device using the import API. Provide a list of JSON objects in request payload to use bulk functionality (Max 1000 objects per request). .DESCRIPTION Creates a new Dashworks device using the import API. Provide a list of JSON objects in request payload to use bulk functionality (Max 1000 objects per request). Takes the ImportId and JsonBody as an input. .PARAMETER Instance Optional. Juriba instance to be provided if not authenticating using Connect-Juriba. For example, https://myinstance.dashworks.app:8443 .PARAMETER APIKey Optional. API key to be provided if not authenticating using Connect-Juriba. .PARAMETER ImportId ImportId for the device. .PARAMETER JsonBody Json payload with updated device details. .PARAMETER Async Optional. Send bulk requests asynchronously. Returns the job URI for polling with Wait-JuribaImportJob. Requires DPC 5.17 or later. Only applies to bulk requests. .EXAMPLE PS> New-JuribaImportDevice -ImportId 1 -JsonBody $jsonBody -Instance "https://myinstance.dashworks.app:8443" -APIKey "xxxxx" .EXAMPLE PS> New-JuribaImportDevice -ImportId 1 -JsonBody $jsonBody -Async -Instance "https://myinstance.dashworks.app:8443" -APIKey "xxxxx" #> [CmdletBinding(SupportsShouldProcess)] [OutputType([String])] param ( [Parameter(Mandatory=$false)] [string]$Instance, [Parameter(Mandatory=$false)] [string]$APIKey, [parameter(Mandatory=$true)] [int]$ImportId, [ValidateScript({ ((Test-Json $_) -and (($_ | ConvertFrom-Json).uniqueIdentifier)) }, ErrorMessage = "JsonBody is not valid json or does not contain a uniqueIdentifier" )] [parameter(Mandatory=$true)] [string]$JsonBody, [parameter(Mandatory=$false)] [switch]$Async ) 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) { #Check if version is 5.14 or newer $ver = Get-JuribaDPCVersion -Instance $instance -MinimumVersion "5.14" if ($ver) { $uri = "{0}/apiv2/imports/{1}/devices" -f $Instance, $ImportId $bulkuri = "{0}/apiv2/imports/{1}/devices/`$bulk" -f $Instance, $ImportId } else { $uri = "{0}/apiv2/imports/{1}/devices" -f $Instance, $ImportId $bulkuri = "{0}/apiv2/imports/{1}/devices/`$bulk" -f $Instance, $ImportId } if ($Async) { $asyncVer = Get-JuribaDPCVersion -Instance $Instance -MinimumVersion "5.17" if (-not $asyncVer) { throw "The -Async switch requires DPC version 5.17 or later." } $bulkuri += "?async" } $headers = @{'x-api-key' = $APIKey} try { if (($PSCmdlet.ShouldProcess(($JsonBody | ConvertFrom-Json).uniqueIdentifier)) -and (($JsonBody | ConvertFrom-Json).Length -eq 1)) { $result = Invoke-RestMethod -Uri $uri -Method POST -Headers $headers -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($JsonBody)) return $result } elseif (($PSCmdlet.ShouldProcess(($JsonBody | ConvertFrom-Json).uniqueIdentifier)) -and (($JsonBody | ConvertFrom-Json).Length -gt 1)) { <# Bulk operation request #> if ($Async) { $response = Invoke-WebRequest -Uri $bulkuri -Method POST -Headers $headers -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($JsonBody)) if ($response.Headers['Location']) { return [string]$response.Headers['Location'][0] } else { throw "No job location returned in async response headers." } } else { $result = Invoke-RestMethod -Uri $bulkuri -Method POST -Headers $headers -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($JsonBody)) return $result } } } catch { Write-Error $_ } } else { Write-Error "No connection found. Please ensure `$APIKey and `$Instance is provided or connect using Connect-Juriba before proceeding." } } |