You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Google App Script删除未同步设备(TypeError报错)问题排查

Google App Script 删除未同步Google设备报错解决

请问有没有人具备使用Google App Script删除Google设备的经验?我们需要删除60天内未同步的设备,不想通过Google Admin Console手动操作,因此编写了如下脚本,但运行时出现报错:TypeError: Cannot read properties of undefined (reading 'list'),报错位置在Code.gs第9行。

原报错脚本:

function deleteInactiveDevices() {
  // Define the number of days after which a device is considered inactive
  const SYNC_THRESHOLD_DAYS = 100;

  // Get the Admin Directory Service
  const admin = AdminDirectory.Devices;

  // Get all devices
  const devices = admin.list();

  // Loop through devices
  while (devices.items) {
    devices.items.forEach(function(device) {
      if (!device.lastSyncTime) {
        // No last sync time available, skip device
        return;
      }

      // Calculate days since last sync
      const daysSinceLastSync = Math.floor((Date.now() - new Date(device.lastSyncTime)) / 1000 / 60 / 60 / 24);

      if (daysSinceLastSync > SYNC_THRESHOLD_DAYS) {
        // Device is inactive, delete it
        console.log(`Deleting inactive device: ${device.name}`);
        admin.delete(device.deviceId);
      }
    });

    // Get next page of devices
    devices = devices.nextPageToken ? admin.list({ pageToken: devices.nextPageToken }) : null;
  }
}

错误原因

报错TypeError: Cannot read properties of undefined (reading 'list')由以下问题导致:

  • 未手动启用Admin Directory服务:Google App Script默认不加载该服务,需手动添加。
  • 设备列表接口调用错误:AdminDirectory.Devices.list()必须传入customerId参数(可用my_customer指代当前管理域名),不能无参调用。
  • 常量重赋值违规:原代码用const定义devices后又尝试重新赋值,语法错误。
  • 删除方法调用错误:正确删除设备的方法是Devices.remove(),而非delete,且需要传入客户ID和设备ID两个参数。

修正后的脚本

function deleteInactiveDevices() {
  // 定义未活跃设备的天数阈值(改为需求的60天)
  const SYNC_THRESHOLD_DAYS = 60;
  // 用my_customer代表当前管理的客户ID
  const CUSTOMER_ID = 'my_customer';

  // 调用设备列表接口,传入客户ID
  let devices = AdminDirectory.Devices.list(CUSTOMER_ID);

  // 循环处理所有分页设备
  while (devices && devices.items) {
    devices.items.forEach(function(device) {
      if (!device.lastSyncTime) {
        // 无同步时间的设备直接跳过
        return;
      }

      // 计算最后同步至今的天数
      const daysSinceLastSync = Math.floor((Date.now() - new Date(device.lastSyncTime)) / (1000 * 60 * 60 * 24));

      if (daysSinceLastSync > SYNC_THRESHOLD_DAYS) {
        console.log(`删除未活跃设备: ${device.name} (ID: ${device.deviceId})`);
        // 调用正确的删除方法,传入客户ID和设备ID
        AdminDirectory.Devices.remove(CUSTOMER_ID, device.deviceId);
      }
    });

    // 获取下一页设备数据
    if (devices.nextPageToken) {
      devices = AdminDirectory.Devices.list(CUSTOMER_ID, { pageToken: devices.nextPageToken });
    } else {
      break;
    }
  }
}

关键操作步骤

  • 启用Admin Directory服务:在App Script编辑器中,点击左侧「服务」→「添加服务」,找到「Admin Directory」并添加。
  • 配置权限:运行脚本的账号必须是Google Workspace管理员,首次运行需完成OAuth授权,授予脚本管理设备的权限。
  • 测试验证:建议先将删除逻辑改为仅打印日志,确认筛选出的设备符合预期后,再启用删除操作。

内容的提问来源于stack exchange,提问作者Caine0027

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 15:32:40