تقنية

ابحث عن المستخدمين غير النشطين وقم بإزالتهم في نطاق Google Workspace الخاص بك


يمكنك استخدام Google Apps Script للعثور على جميع حسابات المستخدمين غير النشطة في نطاق Google Workspace الخاص بك. سيعثر البرنامج النصي على جميع المستخدمين الذين لم يسجلوا الدخول إلى المجال لفترة من الوقت (على سبيل المثال، 6 أشهر). لديك أيضًا خيار حذف الحسابات الخاملة من مجال Workspace وتوفير فواتيرك الشهرية.

ابحث عن المستخدمين غير النشطين في نطاق Google Workspace

يمكننا استخدام خدمة دليل المشرف الخاصة بـ Apps Script لإدراج جميع المستخدمين (النشطين وغير النشطين) في نطاق Google Workspace. افتح برنامج نصي جديد، وانتقل إلى قسم الخدمة وقم بتمكين خدمة دليل المسؤول.

بعد ذلك، انتقل إلى مشروع Google Cloud المرتبط بمشروع Apps Script الخاص بك. قم بالتبديل إلى قسم المكتبة، وابحث عن Admin SDK وقم بتمكين واجهة برمجة التطبيقات. نطاق OAuth المطلوب هو https://www.googleapis.com/auth/admin.directory.user وينبغي أن تكون مدرجة في الخاص بك appsscript.json ملف.

{
  "timeZone": "Asia/Kolkata",
  "dependencies": {
    "enabledAdvancedServices": [
      {
        "userSymbol": "AdminDirectory",
        "version": "directory_v1",
        "serviceId": "admin"
      }
    ]
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": ["https://www.googleapis.com/auth/admin.directory.user"],
  "runtimeVersion": "V8"
}

سيقوم البرنامج النصي بإدراج كافة المستخدمين في المجال والعثور على الحسابات الخاملة بناءً على تاريخ تسجيل الدخول الأخير. إذا لم يقم المستخدم بتسجيل الدخول إلى حسابه خلال آخر 6 أشهر، على سبيل المثال، فسيتم اعتبار المستخدم غير نشط وقد تتم إزالته.

const getInactiveAccounts = () => {
  let accounts = [];
  let pageToken = null;

  // Replace example.com with your domain name.
  do {
    const { users, nextPageToken = null } = AdminDirectory.Users.list({
      domain: 'example.com',
      customer: 'my_customer',
      maxResults: 100,
      orderBy: 'email',
      pageToken
    });

    pageToken = nextPageToken;
    accounts = [...accounts, ...users];
  } while (pageToken !== null);

  // delete users who haven't logged in the last 6 months
  const MONTHS = 6;
  const cutOffDate = new Date();
  cutOffDate.setMonth(cutOffDate.getMonth() - MONTHS);

  const inactiveAccounts = accounts
    .filter(({ isAdmin }) => isAdmin === false) // Skip users with admin priveleges
    .filter(({ lastLoginTime }) => {
      const lastLoginDate = new Date(lastLoginTime);
      return lastLoginDate < cutOffDate;
    })
    .const(({ primaryEmail }) => primaryEmail); // Get only the email address

  Logger.log(`We found ${inactiveAccounts.length} inactive accounts in the domain.`);
  Logger.log(`The list is: ${inactiveAccounts.join(', ')}`);

  // Set this to true if you really want to delete the inactive accounts
  const DELETE_USER = false;

  if (DELETE_USER) {
    // Remove the users from the domain
    inactiveAccounts.forEach((userEmail) => {
      AdminDirectory.Users.remove(userEmail);
      Logger.log(`Deleted Google Workspace account for ${userEmail}`);
    });
  }
};



Source link

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى