NJE/CF-CHECK_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." $missingReference = @() Write-Host "[CF] Finding tenants without reference and adding theri company domain to the list..." -ForegroundColor Yellow $cfcustomers | where customerReference -eq '' | ForEach-Object { $customer = $_ $customerId = $customer.id $customerName = $customer.name $tenantId = $customer.externalServices.MICROSOFT $m365endpoint = $baseUri + "/v2/microsoft/customer/" + $customer.id $m365result = Invoke-RestMethod -Uri $m365endpoint -Method Get -Headers $headers if($m365result -ne $null) { #add customer to list $missingReference += [PSCustomObject]@{ CustomerId = $customerId CustomerName = $customerName TenantId = $tenantId domain = $m365result.companyProfile.domain } } } pause $curanetSubscriptions = Get-CuranetCustomerSubscriptions -Account 3850 $curanetCustomers = @() foreach ($subscription in $curanetSubscriptions) { $license = Get-CuranetM365Licenses -SubscriptionId $subscription.id -Account 3850 if($license) { $tenantId = $license[0].tenantId } else { $tenantId = $null } $tenantDomains = $null if(-not $tenantId) { Write-Host "[Curanet] No tenantId for subscription $($subscription.id) - FETCHING DOMAINS" -ForegroundColor Red $tenantDomainsURI = "https://api.curanet.dk/microsoft365/v1/Subscriptions/"+$subscription.id+"/Domains" $tenantDomains = Invoke-CuranetAPI -Uri $tenantDomainsURI -Method Get -Account 3850 -ErrorAction SilentlyContinue if($tenantDomains -eq $null) { Write-Host "[Curanet] No domains found for subscription $($subscription.id) - skipping" -ForegroundColor Red } } #Add customer to list $curanetCustomers += [PSCustomObject]@{ SubscriptionId = $subscription.id SubscriptionRef = $subscription.subscriptionRef CustomerName = $subscription.description TenantId = $tenantId Domains = $tenantDomains.domainName } } $missingReference | ForEach-Object { $currentCustomer = $_ $match = $curanetCustomers | Where-Object Domains -Contains $currentCustomer.domain if ($match) { $payload = @([ordered]@{ op = "replace" path = "/customerReference" value = [string]$match.SubscriptionRef }) #make sure that the payload is an json array $payload = $payload | ConvertTo-Json -Depth 10 -AsArray $uri = "$baseUri/v2/customers/Customers/$($_.CustomerId)" $patchResponse = Invoke-RestMethod -Uri $uri -Method Patch -Headers $headers -Body $payload -ContentType "application/json-patch+json" Write-Host "[CF] Customer reference set to $($match.SubscriptionRef) for customer $($currentCustomer.CustomerName)" -ForegroundColor Green } else { Write-Host "[CF] No match found for customer $($currentCustomer.CustomerName) - skipping" -ForegroundColor Red } } |