NJE/CF-KL.ps1

$baseUri = "https://portal.api.cloudfactory.dk"
$refreshToken = "oKnsev1oc-XDbix0ofDm6Q-LchTkStBawa761iC_WbGuW"
$accessTokenEndpoint = "$baseUri/Authenticate/ExchangeRefreshToken/$refreshToken"

if(-not ($accessTokenResponse)) {
    $accessTokenResponse = Invoke-RestMethod -Uri $accessTokenEndpoint -Method Get
}

$accessToken = $accessTokenResponse.access_token
$headers = @{
    Authorization = 'Bearer ' + $accessToken
    accept = 'text/plain'
}
$customersEndpoint = "$baseUri/v2/customers/Customers"

Write-Host "[CF] Fetching customers from CloudFactory API..."
$CFcustomers = @()
$nextLink = $customersEndpoint + "?PageSize=250&PageIndex=1"
do {
    $response = Invoke-RestMethod -Uri $nextLink -Method Get -Headers $headers
    $CFcustomers += $response.results

    $metadata = $response.metadata
    if([int]$metadata.page -lt [int]$metadata.totalPages) {
        $nextLink = $customersEndpoint +"?PageSize=250&PageIndex=$($metadata.page + 1)"
        Write-Host "[CF] Fetching page $($metadata.page + 1) via Next page: $nextLink"
    } else {
        $nextLink = $null
    }
} while ($null -ne $nextLink)
Write-Host "[CF] Fetched $($CFcustomers.Count) customers from CloudFactory API."

#exit
$curanetSubscriptions = Get-CuranetCustomerSubscriptions -Account 3850

foreach ($subscription in $curanetSubscriptions) {
    $license = Get-CuranetM365Licenses -SubscriptionId $subscription.id -Account 3850
    #if ($license -eq $null) { continue }
    

    if(-not $license) {
        Write-Host "[Curanet] No tenantId for subscription $($subscription.id) - skipping"
        continue
    }

    $tenantId = $license[0].tenantId

    #Write-Host "[Curanet] Customer: $($subscription.description) - TenantId: $tenantId - SubscriptionRef: $($subscription.subscriptionRef)"

    $CFCustomer = $CFcustomers | Where-Object { $_.externalServices.MICROSOFT -eq $tenantId }

    if (-not $CFCustomer) {
        #Write-Host "[CF] No customer found in CloudFactory for tenantId $tenantId - skipping - CustomerName: $($subscription.description)" -ForegroundColor Red
        continue
    }

    #Write-Host "[CF] Matched customer: $($CFCustomer.name) - TenantId: $tenantId - SubscriptionRef: $($subscription.subscriptionRef)" -ForegroundColor Green

    if (-not ($CFCustomer.customerReference -eq $subscription.subscriptionRef) ) {
        #Write-Host "[CF] Setting Customer Reference from Curanet to CloudFactory on customer $($CFcustomer.name)" -ForegroundColor Yellow
        
        $payload = @([ordered]@{
            op = "replace"
            path = "/customerReference"
            value = [string]$subscription.subscriptionRef
        })
        #make sure that the payload is an json array
        $payload = $payload | ConvertTo-Json -Depth 10 -AsArray

        $uri = "$baseUri/v2/customers/Customers/$($CFCustomer.id)"
        $patchResponse = Invoke-RestMethod -Uri $uri -Method Patch -Headers $headers -Body $payload -ContentType "application/json-patch+json"
        Write-Host "[CF] Customer reference set to $($subscription.subscriptionRef) for customer $($CFCustomer.name)" -ForegroundColor Green
    }

    else {
        #Write-Host "[CF] Customer reference already set to $($subscription.subscriptionRef) - skipping" -ForegroundColor Cyan
    }

}