SV
Azure AD Automation

Bulk User Invitations

Gebruikers uitnodigen naar Azure AD-tenant via Graph API

Setup

Environment configuratie

Maak eerst een .env bestand in je project root met de volgende configuratie:

.envEnvironment
# Oude tenant credentials (bron)
OLD_Tenant_ID=12345678-1234-1234-1234-123456789abc
OLD_Client_ID=87654321-4321-4321-4321-cba987654321
OLD_Client_Secret=ABC123def456GHI789jkl012MNO345pqr678STU901vwx234

# Nieuwe tenant credentials (doel)
NEW_Tenant_ID=abcdef12-5678-9012-3456-789012345678
NEW_Client_ID=fedcba98-7654-3210-9876-543210987654
NEW_Client_Secret=XYZ789abc123DEF456ghi789JKL012mno345PQR678stu901

# Redirect URL voor uitnodigingen
INVITE_REDIRECT_URL=https://portal.company.com

Vereiste Azure App Registrations:

  • Oude tenant: App met User.Read.All en Group.Read.All permissions
  • Nieuwe tenant: App met User.Invite.All, User.Read.All en GroupMember.ReadWrite.All permissions
  • Beide apps moeten Admin Consent hebben gekregen op application niveau
Code

Het complete script

azure-tenant-migration.js
JavaScript
// azure-tenant-migration.js
require('dotenv').config();
const { Client } = require('@microsoft/microsoft-graph-client');
const { ClientSecretCredential } = require('@azure/identity');
require('isomorphic-fetch');
const Table = require('cli-table3');
const readline = require('readline');

async function main() {
  // Load tenant credentials
  const tenantIdOld     = process.env.OLD_Tenant_ID;
  const clientIdOld     = process.env.OLD_Client_ID;
  const clientSecretOld = process.env.OLD_Client_Secret;
  const tenantIdNew     = process.env.NEW_Tenant_ID;
  const clientIdNew     = process.env.NEW_Client_ID;
  const clientSecretNew = process.env.NEW_Client_Secret;
  const inviteRedirectUrl = process.env.INVITE_REDIRECT_URL || 'https://portal.company.com';

  if (!tenantIdOld || !clientIdOld || !clientSecretOld ||
      !tenantIdNew || !clientIdNew || !clientSecretNew) {
    console.error('Please ensure all tenant credentials are set in .env');
    process.exit(1);
  }

  // Initialize Graph clients
  const credOld = new ClientSecretCredential(tenantIdOld, clientIdOld, clientSecretOld);
  const graphOld = Client.initWithMiddleware({
    authProvider: {
      getAccessToken: async () =>
        (await credOld.getToken('https://graph.microsoft.com/.default')).token
    }
  });

  // ... rest of the script
}

main().catch(e => {
  console.error('Fatal error:', e);
  process.exit(1);
});

Hulp nodig met Azure AD automation?

Ontdek hoe user provisioning en bulk operations voorspelbaar en volledig geautomatiseerd kunnen worden uitgevoerd.