首页 > 解决方案 > 有没有办法获取用户上次在 Google Workspace 中更改密码的日期和时间

问题描述

我们正在从 AD 转移到 Google Work 空间作为我们的 LDAP 服务。我的任务之一是将我们在 AD 上拥有的一些工具复制到 Google Workspace。其中一个工具是能够在密码即将到期前 14 天,然后在 7 天前警告用户,并在 4 天前向我们(IT 管理员)发送警告,以便在他们被锁定之前抓住它。所以我在 Windows 和 LDAP 上的 PowerShell 中创建了这个工具,我正在尝试使用 Google App Scripts 来执行此操作。我了解到 Directory API 不会向用户公开此元数据(https://developers.google.com/admin-sdk/directory/v1/guides/manage-users) 我必须使用管理报告 API 并在日志中搜索一个名为 CHANGE_PASSWORD 的事件名称,然后构建一个用户电子邮件地址数组以及他们上次更改密码的时间。

我已经成功地向我展示了电子邮件地址的 HTML 输出,以及在加载 Web 应用程序时按需生成的表格中还有多少天他们的密码过期,但我注意到我们的组织中有 120 个用户和只有 78 个用户出现在列表中。所以后来我意识到谷歌管理员报告的报告部分只存储了 6 个月的日志。对我来说,报告部分不是确定用户上次更改密码的可靠来源。关于如何准确获取 Google Workspace 用户上次更改密码的日期,是否有人有任何其他想法?如果有人想以此为基础,这是我目前拥有的:

请注意,您必须将 Admin SDK API 服务添加到您的脚本中,并且运行该脚本的用户可以在您的域中生成报告。这不是精巧的代码,而只是概念证明,因此请谨慎对待您对我草率代码的回复和评论

代码.gs

const maxPasswordAge = 90;

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index');
}

function getListOfUsers() {
 const userKey = 'all';
  const applicationName = 'admin';
  const optionalArgs = {
    eventName: 'CHANGE_PASSWORD'
  };

  const logs = AdminReports.Activities.list(userKey, applicationName, optionalArgs);
  const activities = logs.items;
  
  if (activities && activities.length > 0) {
    var passwordLastChanged = new Object();
    for (i = 0; i < activities.length; i++) {
      const activity = activities[i];
      const key = activity.events[0].parameters[0]['value'];
      const value = activity.id.time;

      // If the key does not exist then add it
      if (!passwordLastChanged.hasOwnProperty(key)) {
        passwordLastChanged[key] = value;
      }
    }
  } else {
    Logger.log('No results found.');
  }

  // You will now have an object with emailaddress:Date last Changed

  const todaysDate = new Date();
  
  // Change the date to a number of days till it expires from today's date
  for (const [key, value] of Object.entries(passwordLastChanged)) {
    const dateLastChange = new Date(value);
    const milliBetweenDate = todaysDate - dateLastChange;
    const diffInDays = milliBetweenDate / (1000 * 3600 * 24);
    passwordLastChanged[key] = (maxPasswordAge - diffInDays).toFixed(1);

  }

  // Change the object to a sorted array based on the days left till password expires
  const entries = Object.entries(passwordLastChanged);
  const sorted = entries.sort((a, b) => a[1] - b[1]);

  return sorted;

}

索引.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
<style type="text/css">
body {background-color: gray;}
.tg {border-collapse:collapse;border-spacing:0;margin:0px auto;}
.td {border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal; color: floralwhite;}
.col1, .col2, .col3  {text-align:left;vertical-align:top}
.col4, .col5 {text-align:center;vertical-align:top}
</style>
<table id="myTable" class="tg">
<tbody>
  <tr>
    <td class="col3">Email</td>
    <td class="col5">Days Left</td>    
  </tr>
</tbody>
</table>

<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
  
  // The code in this function runs when the page is loaded.
  $(function() {
    google.script.run.withSuccessHandler(addUsersToTable).getListOfUsers();
  });

  function addUsersToTable(userArray) {
    for (var i = 0; i < userArray.length; i++) {
      $('#myTable > tbody:last-child').append('<tr><td class="col3">' + userArray[i][0] + '</td><td class="col5">' + userArray[i][1] + '</td></tr>');
    }
  }

</script>
  </body>
</html>

标签: javascriptgoogle-apps-scriptgoogle-workspace

解决方案


所以是的,他们保留日志的最长期限是 6 个月。如果您使用的是 Enterprise SKU(不太可能有 120 个用户),您可以将日志导出到 Bigquery 并进行查询。https://support.google.com/a/answer/9079365?hl=en如果您有其他东西可以摄取日志,您也可以这样做。

但是,我为暂停日期做了一些事情,因为该日期也没有在用户字段中设置。

您将需要 2 个进程。

为您的密码日期选项创建自定义架构属性。有一个定期抓取审计日志的流程。假设您每天都在查找过去 24 小时内的密码更改。然后,您相应地设置自定义架构属性的值。

您的第二个过程将监视该自定义架构属性并向用户发出警报。

首先,我必须做出一个假设。所有不符合现有标准的用户都将设置一个固定的日期。向前推进这个新流程将确保一切都是内联的。


推荐阅读