您可以使用 Google Apps 脚本查找您的 Google Workspace 域中的所有非活动用户帐户。该脚本将查找一段时间内(例如 6 个月)未登录域的所有用户。您还可以选择从 Workspace 域中删除休眠帐户并节省每月账单。
查找 Google Workspace 域中的非活跃用户
我们可以使用 Apps Script 的 Admin Directory 服务列出 Google Workspace 域中的所有用户(活动和非活动)。打开一个新脚本,转到服务部分并启用管理目录服务。
接下来,转到与您的 Apps 脚本项目关联的 Google Cloud 项目。切换到库部分,搜索 Admin SDK 并启用 API。所需的 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 } ` ) ; } ) ; } } ;
来源: https://www.labnol.org/remove-inactive-workspace-users-220307