[本文为预发布文档,可能会发生变化。]
Power Platform API 可用于从您的 Microsoft Power Platform 环境(有或没有 Microsoft Dataverse 数据库)中提取各种详细信息和元数据。 此 API 由目前可用的各个客户端(如 PowerShell)在内部使用。
在本教程中,将了解如何:
- 创建使用 Power Platform API 进行身份验证的 Power Automate 或逻辑应用工作流 (Azure) 或 PowerShell 脚本。
- 调用“列出环境”终结点来检索您的 Microsoft Power Platform 环境详细信息。
- 迭代访问容量对象来检索实际消耗量。
- 将消耗数据保存成表显示。
此场景的一个示例是,客户在寻找处理其容量消耗的方法,以便他们可以更好地了解按部门对总租户容量进行的分配。 这有助于客户执行一些内部成本核算功能,并根据每个部门消耗的总可用容量进行退款。 此客户在使用“环境说明”调出负责每个环境的部门。
重要提示
Power Platform API 处于预览阶段。 主机名和数据契约可能会在终结点正式发布时更改。
连接和变量设置
使用以下有关以编程方式连接到的详细信息 Power Platform 。 您可以在 Azure 体验或 PowerShell 脚本之间进行选择。
创建工作流和设置变量
首先,在本教程中,我们使用逻辑应用工作流。 另外也接受 Power Automate 流,以及您的公司更喜欢用于自动化的任何其他编排引擎。 检索数据的所有调用都使用 RESTful API,因此任何支持 REST 的工具都适用于本教程。
访问 Azure 门户,然后创建一个新的逻辑应用并命名:
完成预配后,使用设计器编辑工作流,并将定期触发器设置为每天运行:
接下来,我们需要初始化 5 个变量:
- SPN-Id —这是您的服务委托人 ClientID。 稍后使用它在服务主体上下文中执行身份验证。 如果您使用用户名/密码上下文,则可以跳过此变量。
- DBCapacity —这是已使用数据库容量(以 MB 为单位)的 Float 变量。
- FileCapacity —这是一个 Float 变量,用于表示已使用的文件容量(以 MB 为单位)。
- LogCapacity —这是已使用日志容量(以 MB 为单位)的 Float 变量。
- SimplifiedEnvironmentArray-Init –这是一个数组变量,我们用一些环境属性填充了它。 这将大大简化最终的 HTML 表报表输出。
接下来,我们使用 Microsoft Entra API 进行身份验证并检索用于调用 API 的 Power Platform 令牌。 如果您尚未完成 Microsoft Entra 设置,请参阅身份验证 - 旧.
在本教程中,我们使用密钥保管库来存储我们的服务主体机密值。 这样,IT 管理员可以安全地为您的工作流提供此值。 然后,在 POST 调用 Microsoft Entra 中填充此令牌以检索令牌。
然后,我们在“解析 JSON”操作中使用此 JSON 架构将 Microsoft Entra 令牌响应解析为类型化对象:
{
"properties": {
"access_token": {
"type": "string"
},
"expires_in": {
"type": "integer"
},
"ext_expires_in": {
"type": "integer"
},
"token_type": {
"type": "string"
}
},
"type": "object"
}
提取环境
在本节中,我们将获取您管理的环境列表。 这可以通过 API 和 PowerShell 来完成。
调用“列出环境”终结点
现在该调用 Power Platform API 了。 使用 List Environments 终结点检索我们的所有环境及其元数据,特别是使用 $expand 参数进行容量。 另外还将使用上一节中从 Microsoft Entra ID 接收的带有持有者令牌的授权标头。 如果您使用了用户名/密码上下文,也可以在此步骤中输入该持有者令牌。
然后,我们使用此 JSON 架构和“解析 JSON”操作将 API 回复解析 Power Platform 为类型化对象:
{
"properties": {
"value": {
"items": {
"properties": {
"description": {
"type": "string"
},
"id": {
"type": "string"
},
"location": {
"type": "string"
},
"name": {
"type": "string"
},
"properties": {
"properties": {
"addons": {
"type": "array"
},
"azureRegion": {
"type": "string"
},
"capacity": {
"items": {
"properties": {
"actualConsumption": {
"type": "number"
},
"capacityType": {
"type": "string"
},
"capacityUnit": {
"type": "string"
},
"ratedConsumption": {
"type": "number"
},
"updatedOn": {
"type": "string"
}
},
"required": [
"capacityType",
"actualConsumption",
"ratedConsumption",
"capacityUnit",
"updatedOn"
],
"type": "object"
},
"type": "array"
},
"clientUris": {
"properties": {
"admin": {
"type": "string"
},
"maker": {
"type": "string"
}
},
"type": "object"
},
"cluster": {
"properties": {
"category": {
"type": "string"
},
"number": {
"type": "string"
}
},
"type": "object"
},
"connectedGroups": {
"type": "array"
},
"createdBy": {
"properties": {
"displayName": {
"type": "string"
},
"id": {
"type": "string"
},
"type": {
"type": "string"
}
},
"type": "object"
},
"createdTime": {
"type": "string"
},
"creationType": {
"type": "string"
},
"databaseType": {
"type": "string"
},
"displayName": {
"type": "string"
},
"environmentSku": {
"type": "string"
},
"isDefault": {
"type": "boolean"
},
"linkedEnvironmentMetadata": {
"properties": {
"backgroundOperationsState": {
"type": "string"
},
"baseLanguage": {
"type": "number"
},
"createdTime": {
"type": "string"
},
"domainName": {
"type": "string"
},
"friendlyName": {
"type": "string"
},
"instanceApiUrl": {
"type": "string"
},
"instanceState": {
"type": "string"
},
"instanceUrl": {
"type": "string"
},
"platformSku": {
"type": "string"
},
"resourceId": {
"type": "string"
},
"scaleGroup": {
"type": "string"
},
"uniqueName": {
"type": "string"
},
"version": {
"type": "string"
}
},
"type": "object"
},
"protectionStatus": {
"properties": {
"keyManagedBy": {
"type": "string"
}
},
"type": "object"
},
"provisioningState": {
"type": "string"
},
"retentionDetails": {
"properties": {
"backupsAvailableFromDateTime": {
"type": "string"
},
"retentionPeriod": {
"type": "string"
}
},
"type": "object"
},
"retentionPeriod": {
"type": "string"
},
"runtimeEndpoints": {
"properties": {
"microsoft.ApiManagement": {
"type": "string"
},
"microsoft.BusinessAppPlatform": {
"type": "string"
},
"microsoft.CommonDataModel": {
"type": "string"
},
"microsoft.Flow": {
"type": "string"
},
"microsoft.PowerApps": {
"type": "string"
},
"microsoft.PowerAppsAdvisor": {
"type": "string"
}
},
"type": "object"
},
"states": {
"properties": {
"management": {
"properties": {
"id": {
"type": "string"
}
},
"type": "object"
},
"runtime": {
"properties": {
"id": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"updateCadence": {
"properties": {
"id": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"type": {
"type": "string"
}
},
"required": [
"id",
"type",
"location",
"name",
"properties"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
迭代访问容量对象
这是教程中最复杂的部分。 在这里,我们使用循环内的循环来迭代 List 环境回复中的每个环境,每个环境都有一个容量详细信息数组,我们也要迭代这些详细信息。 这样,我们就可以捕获容量报告表中每个环境行的必要信息。
For-each 和解析
我们来逐步完成此任务。 首先,我们使用 Parse-List-Response 输出的 'value' 的 For Each 控件:
然后,我们使用以下 JSON 架构将此单个环境解析为类型化对象:
{
"properties": {
"description": {
"type": "string"
},
"id": {
"type": "string"
},
"location": {
"type": "string"
},
"name": {
"type": "string"
},
"properties": {
"properties": {
"addons": {
"type": "array"
},
"azureRegion": {
"type": "string"
},
"capacity": {
"items": {
"properties": {
"actualConsumption": {
"type": "number"
},
"capacityType": {
"type": "string"
},
"capacityUnit": {
"type": "string"
},
"ratedConsumption": {
"type": "number"
},
"updatedOn": {
"type": "string"
}
},
"required": [
"capacityType",
"actualConsumption",
"ratedConsumption",
"capacityUnit",
"updatedOn"
],
"type": "object"
},
"type": "array"
},
"clientUris": {
"properties": {
"admin": {
"type": "string"
},
"maker": {
"type": "string"
}
},
"type": "object"
},
"cluster": {
"properties": {
"number": {
"type": "string"
}
},
"type": "object"
},
"connectedGroups": {
"type": "array"
},
"createdBy": {
"properties": {
"displayName": {
"type": "string"
},
"id": {
"type": "string"
},
"type": {
"type": "string"
}
},
"type": "object"
},
"createdTime": {
"type": "string"
},
"creationType": {
"type": "string"
},
"databaseType": {
"type": "string"
},
"displayName": {
"type": "string"
},
"environmentSku": {
"type": "string"
},
"isDefault": {
"type": "boolean"
},
"linkedEnvironmentMetadata": {
"properties": {
"backgroundOperationsState": {
"type": "string"
},
"baseLanguage": {
"type": "integer"
},
"createdTime": {
"type": "string"
},
"domainName": {
"type": "string"
},
"friendlyName": {
"type": "string"
},
"instanceApiUrl": {
"type": "string"
},
"instanceState": {
"type": "string"
},
"instanceUrl": {
"type": "string"
},
"resourceId": {
"type": "string"
},
"scaleGroup": {
"type": "string"
},
"uniqueName": {
"type": "string"
},
"version": {
"type": "string"
}
},
"type": "object"
},
"protectionStatus": {
"properties": {
"keyManagedBy": {
"type": "string"
}
},
"type": "object"
},
"provisioningState": {
"type": "string"
},
"retentionDetails": {
"properties": {
"backupsAvailableFromDateTime": {
"type": "string"
},
"retentionPeriod": {
"type": "string"
}
},
"type": "object"
},
"retentionPeriod": {
"type": "string"
},
"runtimeEndpoints": {
"properties": {
"microsoft.ApiManagement": {
"type": "string"
},
"microsoft.BusinessAppPlatform": {
"type": "string"
},
"microsoft.CommonDataModel": {
"type": "string"
},
"microsoft.Flow": {
"type": "string"
},
"microsoft.PowerApps": {
"type": "string"
},
"microsoft.PowerAppsAdvisor": {
"type": "string"
}
},
"type": "object"
},
"states": {
"properties": {
"management": {
"properties": {
"id": {
"type": "string"
}
},
"type": "object"
},
"runtime": {
"properties": {
"id": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"updateCadence": {
"properties": {
"id": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
},
"type": {
"type": "string"
}
},
"type": "object"
}
接下来,我们使用另一个使用 Parse-CurrentItem 输出的 'capacity' 的 For Each 控件。 然后,我们使用以下 JSON 架构将其解析为类型化对象:
现在,我们可以在 Parse-Capacity 输出中的 CapacityType 属性上使用 Switch 控件。 这是 'Database'、'File' 或 'Log' 的值。 在每种切换情况下,将相关的“actualConsumption”属性捕获到相关变量中。 在以下情况下,您会看到我们正在捕获 Database capacity:
作为“针对每个环境”循环的最后一步,我们现在可以在报表中捕获此行的环境详细信息。 使用“Append to array variable”控件,使用以下 JSON 架构:
{
"properties": {
"actualConsumption": {
"type": "number"
},
"capacityType": {
"type": "string"
},
"capacityUnit": {
"type": "string"
},
"ratedConsumption": {
"type": "number"
},
"updatedOn": {
"type": "string"
}
},
"type": "object"
}
生成报表表
恭喜,您现在已经进入简单工作阶段了! 现在我们已经有完全填充的简化的环境容量数组了,我们可以以表格格式显示它。
使用 HTML 表连接器
运行逻辑应用,我们现在可以看到 HTML 表报表的输出:
在此示例中,可以出于成本核算目的选择将报表通过电子邮件发送给利益干系人,或者可以将数据保存到数据库中以进行进一步的分析,了解历史趋势。