Bad Server Response when Creating Migration Job

Hi,

I am using the following code to move a device from one sub-estate to another:

function Move-Device {
    param (
        [string]$apiKey,
        [string]$fromTenantId,
        [string]$toTenantId,
        [string]$deviceId,
        [string]$OrgID
    )

    # Define headers for create job
    $headers = @{
        "Authorization" = "Bearer $apiKey"
        "X-Tenant-ID" = $toTenantId
        "X-Organization-ID" = $OrgID
        "Content-Type" = "application/json"
    }

    # Body for Create migration job
    $migrationJobBody = @{
        fromTenant = $fromTenantId
        endpoints = @($deviceId)
    } | ConvertTo-Json

    try {
        $migrationJobResponse = Invoke-RestMethod -Uri "https://api-us01.central.sophos.com/endpoint/v1/migrations" -Method Post -Headers $headers -Body $migrationJobBody
        $migrationJobId = $migrationJobResponse.id
        $migrationJobToken = $migrationJobResponse.token
    } catch {
        Write-Error "Failed to create migration job: $_"
        return
    }

    # Define headers for trigger
    $headers = @{
        "Authorization" = "Bearer $apiKey"
        "X-Tenant-ID" = $fromTenantId
        "X-Organization-ID" = $OrgID
        "Content-Type" = "application/json"
    }
    
    # Body for Create migration trigger
    $migrationTriggerBody = @{
        token = $migrationJobToken
        endpoints = @($deviceId)
    } | ConvertTo-Json

    try {
        # Trigger migration job
        $triggerResponse = Invoke-RestMethod -Uri "https://api-us01.central.sophos.com/endpoint/v1/migrations/$migrationJobId" -Method Put -Headers $headers -Body $migrationTriggerBody
    } catch {
        Write-Error "Failed to trigger migration job: $_"
        return
    }

    # Check migration status
    $status = "pending"
    while ($status -ne "completed") {
        Start-Sleep -Seconds 10
        try {
            $statusResponse = Invoke-RestMethod -Uri "https://api-us01.central.sophos.com/endpoint/v1/migrations/$migrationJobId/endpoints" -Method Get -Headers $headers
            $status = $statusResponse.items.status
            Write-Output "Current status: $status"
        } catch {
            Write-Error "Failed to check migration status: $_"
            return
        }
    }

    Write-Output "Migration completed successfully!"
}

The problem is, when creating the migration job, powershell returns "BadServerResponse". As far as I can tell the headers and body are appropriate, and the token is fine as I am able to use it in other API calls. Any ideas?

Parents Reply Children
No Data