使用生命周期工作流 API 在员工最后一天的工作后自动执行离职任务

本教程提供分步指南,指导员工在最后一天工作后使用生命周期工作流 API 配置离职任务。 在此方案中,计划员工解雇,可能包括通知期。 有关计划外实时 终止方案,请参阅使用生命周期工作流 API 在工作的最后一天实时完成员工离职任务

在本教程中,你将了解如何:

  • 配置生命周期工作流,以便为特定部门中的员工检查,这些员工在工作的最后一天之后几天。
  • 配置任务以按顺序运行以下任务:
    • 删除用户的所有许可证
    • 从所有 Teams 中删除用户
    • 删除用户帐户
  • 监视工作流的状态及其关联任务。

先决条件

若要完成本教程,需要以下资源和特权:

  • 此功能需要Microsoft Entra ID 治理许可证。 若要找到适合你的要求的许可证,请参阅Microsoft Entra ID 治理许可基础知识
  • 登录到 API 客户端(例如 Graph 资源管理器),使用至少具有生命周期管理员Microsoft Entra角色的帐户调用 Microsoft Graph。
  • 授予 LifecycleWorkflows.ReadWrite.All Microsoft Graph 委托权限。
  • 创建一个测试用户帐户,用于表示离开组织的员工。 工作流运行时,会删除此测试用户帐户。 将许可证和 Teams 成员身份分配给测试用户帐户。

创建“leaver”工作流

请求

以下请求使用以下设置创建卸载工作流:

  • 它可以按需运行,但不能按计划运行。 此步骤允许我们使用测试用户的帐户验证工作流。 本教程稍后将更新工作流以按计划运行。
  • 工作流在员工的 employeeLeaveDateTime 之后七天后运行,如果他们在“市场营销”部门。
  • 启用三个工作流任务以按顺序运行:取消为用户分配所有许可证,然后从所有团队中删除,然后删除其用户帐户。
POST https://graph.microsoft.com/v1.0/identityGovernance/LifecycleWorkflows/workflows
Content-type: application/json

{
    "category": "leaver",
    "displayName": "Post-Offboarding of an employee",
    "description": "Configure offboarding tasks for employees after their last day of work",
    "isEnabled": true,
    "isSchedulingEnabled": false,
    "executionConditions": {
        "@odata.type": "#microsoft.graph.identityGovernance.triggerAndScopeBasedConditions",
        "scope": {
            "@odata.type": "#microsoft.graph.identityGovernance.ruleBasedSubjectSet",
            "rule": "department eq 'Marketing'"
        },
        "trigger": {
            "@odata.type": "#microsoft.graph.identityGovernance.timeBasedAttributeTrigger",
            "timeBasedAttribute": "employeeLeaveDateTime",
            "offsetInDays": 7
        }
    },
    "tasks": [
        {
            "category": "leaver",
            "continueOnError": false,
            "description": "Remove all licenses assigned to the user",
            "displayName": "Remove all licenses for user",
            "executionSequence": 1,
            "isEnabled": true,
            "taskDefinitionId": "8fa97d28-3e52-4985-b3a9-a1126f9b8b4e",
            "arguments": []
        },
        {
            "category": "leaver",
            "continueOnError": false,
            "description": "Remove user from all Teams memberships",
            "displayName": "Remove user from all Teams",
            "executionSequence": 2,
            "isEnabled": true,
            "taskDefinitionId": "81f7b200-2816-4b3b-8c5d-dc556f07b024",
            "arguments": []
        },
        {
            "category": "leaver",
            "continueOnError": false,
            "description": "Delete user account in Azure AD",
            "displayName": "Delete User Account",
            "executionSequence": 3,
            "isEnabled": true,
            "taskDefinitionId": "8d18588d-9ad3-4c0f-99d0-ec215f0e3dff",
            "arguments": []
        }
    ]
}

响应

HTTP/1.1 201 Created
Content-Type: application/json

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#identityGovernance/lifecycleWorkflows/workflows/$entity",
    "category": "leaver",
    "description": "Configure offboarding tasks for employees after their last day of work",
    "displayName": "Post-Offboarding of an employee",
    "lastModifiedDateTime": "2024-03-03T18:29:10.8412536Z",
    "createdDateTime": "2024-03-03T18:29:10.8412352Z",
    "deletedDateTime": null,
    "id": "15239232-66ed-445b-8292-2f5bbb2eb833",
    "isEnabled": true,
    "isSchedulingEnabled": false,
    "nextScheduleRunDateTime": null,
    "version": 1,
    "executionConditions": {
        "@odata.type": "#microsoft.graph.identityGovernance.triggerAndScopeBasedConditions",
        "scope": {
            "@odata.type": "#microsoft.graph.identityGovernance.ruleBasedSubjectSet",
            "rule": "department eq 'Marketing'"
        },
        "trigger": {
            "@odata.type": "#microsoft.graph.identityGovernance.timeBasedAttributeTrigger",
            "timeBasedAttribute": "employeeLeaveDateTime",
            "offsetInDays": 7
        }
    }
}

运行工作流

由于工作流未计划运行,因此必须按需手动运行。 在以下请求中,将按 ID df744d9e-2148-4922-88a8-633896c1e929标识工作流目标的用户。

按需运行工作流时,无论用户状态是否与范围和触发执行条件匹配,任务都会执行。 因此,即使用户不在“市场营销”部门或其 employeeLeaveDateTime 设置为 null,此命令仍为用户运行工作流中定义的任务。

请求会返回 204 No Content 响应代码。

POST https://graph.microsoft.com/v1.0/identityGovernance/LifecycleWorkflows/workflows/15239232-66ed-445b-8292-2f5bbb2eb833/activate

{
    "subjects": [
        {
            "id": "df744d9e-2148-4922-88a8-633896c1e929"
        }
    ]
}

检查任务和工作流状态

随时可以在三个级别监视工作流和任务的状态。

  • 监视用户级别的任务。
  • 在指定时间段内监视工作流的用户级结果的聚合高级摘要。
  • 检索工作流中为特定用户执行的所有任务的详细日志。

选项 1:在用户级别监视工作流的任务

请求

GET https://graph.microsoft.com/v1.0/identityGovernance/LifecycleWorkflows/workflows/15239232-66ed-445b-8292-2f5bbb2eb833/userProcessingResults

响应

HTTP/1.1 200 OK
Content-Type: application/json

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#identityGovernance/lifecycleWorkflows/workflows('15239232-66ed-445b-8292-2f5bbb2eb833')/userProcessingResults",
    "value": [
        {
            "id": "40efc576-840f-47d0-ab95-5abca800f8a2",
            "completedDateTime": "2024-03-03T18:31:00.3581066Z",
            "failedTasksCount": 0,
            "processingStatus": "completed",
            "scheduledDateTime": "2024-03-03T18:30:43.154495Z",
            "startedDateTime": "2024-03-03T18:30:46.9357178Z",
            "totalTasksCount": 3,
            "totalUnprocessedTasksCount": 0,
            "workflowExecutionType": "onDemand",
            "workflowVersion": 1,
            "subject": {
                "id": "df744d9e-2148-4922-88a8-633896c1e929"
            }
        }
    ]
}

选项 2:获取指定时间段内工作流的用户级结果的聚合高级摘要

请求

GET https://graph.microsoft.com/v1.0/identityGovernance/LifecycleWorkflows/workflows/15239232-66ed-445b-8292-2f5bbb2eb833/userProcessingResults/summary(startDateTime=2024-03-01T00:00:00Z,endDateTime=2024-03-30T00:00:00Z)

响应

HTTP/1.1 200 OK
Content-Type: application/json

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.identityGovernance.userSummary",
    "failedTasks": 0,
    "failedUsers": 0,
    "successfulUsers": 1,
    "totalTasks": 3,
    "totalUsers": 1
}

选项 3:检索工作流中为特定用户执行的所有任务的详细日志

请求

GET https://graph.microsoft.com/v1.0/identityGovernance/LifecycleWorkflows/workflows/15239232-66ed-445b-8292-2f5bbb2eb833/userProcessingResults/40efc576-840f-47d0-ab95-5abca800f8a2/taskProcessingResults

响应

HTTP/1.1 200 OK
Content-Type: application/json

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#identityGovernance/lifecycleWorkflows/workflows('15239232-66ed-445b-8292-2f5bbb2eb833')/userProcessingResults('40efc576-840f-47d0-ab95-5abca800f8a2')/taskProcessingResults",
    "value": [
        {
            "completedDateTime": "2024-03-03T18:30:50.483365Z",
            "createdDateTime": "2024-03-03T18:30:47.6125438Z",
            "id": "78650318-7238-4e7e-852f-2c36cbeff340",
            "processingStatus": "completed",
            "startedDateTime": "2024-03-03T18:30:50.0549446Z",
            "failureReason": null,
            "subject": {
                "id": "df744d9e-2148-4922-88a8-633896c1e929"
            },
            "task": {
                "category": "leaver",
                "continueOnError": false,
                "description": "Remove all licenses assigned to the user",
                "displayName": "Remove all licenses for user",
                "executionSequence": 1,
                "id": "f71246b2-269c-4ba6-ab8e-afc1a05114cb",
                "isEnabled": true,
                "taskDefinitionId": "8fa97d28-3e52-4985-b3a9-a1126f9b8b4e",
                "arguments": []
            }
        },
        {
            "completedDateTime": "2024-03-03T18:30:57.6034021Z",
            "createdDateTime": "2024-03-03T18:30:47.8824313Z",
            "id": "3d2e459d-5614-42e4-952b-0e917b5f6646",
            "processingStatus": "completed",
            "startedDateTime": "2024-03-03T18:30:53.6770279Z",
            "failureReason": null,
            "subject": {
                "id": "df744d9e-2148-4922-88a8-633896c1e929"
            },
            "task": {
                "category": "leaver",
                "continueOnError": false,
                "description": "Remove user from all Teams memberships",
                "displayName": "Remove user from all Teams",
                "executionSequence": 2,
                "id": "ed545f03-e8d8-45fb-9cbd-15c937f2a866",
                "isEnabled": true,
                "taskDefinitionId": "81f7b200-2816-4b3b-8c5d-dc556f07b024",
                "arguments": []
            }
        },
        {
            "completedDateTime": "2024-03-03T18:31:00.0894515Z",
            "createdDateTime": "2024-03-03T18:30:48.0004721Z",
            "id": "03359fa6-c63c-4573-92c2-4c9518ca98aa",
            "processingStatus": "completed",
            "startedDateTime": "2024-03-03T18:30:59.6195169Z",
            "failureReason": null,
            "subject": {
                "id": "df744d9e-2148-4922-88a8-633896c1e929"
            },
            "task": {
                "category": "leaver",
                "continueOnError": false,
                "description": "Delete user account in Azure AD",
                "displayName": "Delete User Account",
                "executionSequence": 3,
                "id": "b4cefaa0-6ceb-461d-bbf5-ec69246463fd",
                "isEnabled": true,
                "taskDefinitionId": "8d18588d-9ad3-4c0f-99d0-ec215f0e3dff",
                "arguments": []
            }
        }
    ]
}

[可选]将工作流计划为自动运行

按需运行工作流并检查一切是否正常工作后,你可能希望启用工作流,以便它可以按照租户定义的计划自动运行。 运行以下请求。

请求会返回 204 No Content 响应代码。 计划工作流时,生命周期工作流引擎每三小时检查一次处于关联执行条件的用户,并为这些用户执行配置的任务。 可以自定义此重复周期,范围为 1 小时到 24 小时。

PATCH https://graph.microsoft.com/v1.0/identityGovernance/lifecycleWorkflows/workflows/15239232-66ed-445b-8292-2f5bbb2eb833
Content-type: application/json

{
    "isEnabled": true,
    "isSchedulingEnabled": true
}