Azure Automation Runbook 使用 IMDS实例元数据服务地址来获取托管标识 Token一、概述本文目的说明二、步骤1、创建自动化账户2、创建Runbook(1) 普通环境-脚本参考(2) 沙盒环境-脚本参考(3) 通用脚本3、运行三、拓展内容使用 Power Automate获取Access Token1、添加Webhook2、Runbook脚本参考3、Flow参考一、概述本文目的使用Azure Automation Runbook 安全、自动地获取 Token然后调用任何授权范围内的 Azure 资源形成一个完整的自动化闭环。说明任何支持 Microsoft Entra ID原 Azure AD认证的 Azure 服务都可以使用 Access Token 来调用。在自动化脚本或后台服务这类没有用户交互的场景中使用托管标识与 Access Token 被认为是 “最佳实践”。优势1、无须在代码、配置文件中存储或定期轮换密钥。2、消除了密钥泄露的风险。Access Token 是短期有效的权限也是事先通过 RBAC 精确授予的。二、步骤1、创建自动化账户在Azure门户中搜索自动化账户然后创建自动化账户。选择资源组输入名称然后审阅并创建就行了2、创建Runbook填好后审阅并创建 ↓输入代码保存然后发布如果要修改代码参考下图选择在门户中编辑 ↓(1) 普通环境-脚本参考# 标准 IMDS 地址 $endpoint http://169.254.169.254/metadata/identity/oauth2/token $resource https://ai.azure.com # 构造 URL $Url $endpoint ?resource $resource api-version2018-02-01 # 获取 Token标准环境用 Metadatatrue不需要额外 header $response Invoke-RestMethod -Method Get -Uri $Url -Headers {Metadatatrue} -TimeoutSec 30 -ErrorAction Stop Write-Output ✅ 成功获取 Token Write-Output $response.access_token(2) 沙盒环境-脚本参考Azure Automation 的 Runbook 运行在受限的沙盒环境中微软对 IMDS 端点做了封装需要用 localhost 加自定义 header。# 定义所有变量 $endpoint http://localhost/metadata/identity/oauth2/token $header MSIHeaderValue $resource https://ai.azure.com # 构造 URL $Url $endpoint ?resource $resource api-version2019-08-01 # 获取 Token $response Invoke-RestMethod -Method Get -Uri $Url -Headers {X-IDENTITY-HEADER $header} -TimeoutSec 30 -ErrorAction Stop Write-Output ✅ 成功获取 Token Write-Output $response.access_token(3) 通用脚本两个环境都能跑的代码沙盒环境已测试无误$resource https://ai.azure.com # 检测环境并使用正确的 endpoint 和 header if ($env:IDENTITY_ENDPOINT) { # 沙盒环境Automation Runbook $endpoint $env:IDENTITY_ENDPOINT $headers {X-IDENTITY-HEADER $env:IDENTITY_HEADER} Write-Output 检测到沙盒环境 } else { # 普通环境VM / Function / App Service $endpoint http://169.254.169.254/metadata/identity/oauth2/token $headers {Metadata true} Write-Output 检测到普通环境 } # 构造 URL 并获取 Token $Url $endpoint ?resource $resource api-version2019-08-01 $response Invoke-RestMethod -Method Get -Uri $Url -Headers $headers -TimeoutSec 30 Write-Output ✅ 成功获取 Token Write-Output $response.access_token参考文章1、在不使用 Azure cmdlet 的情况下生成访问令牌(在自动化 Runbook 的沙盒环境中必须使用环境变量 $env:IDENTITY_ENDPOINT 和 $env:IDENTITY_HEADER)2、用 HTTP 获取令牌(标准环境下的做法使用固定的 IMDS 地址 169.254.169.254 和 Metadata: true 头)3、运行可以看到输出和日志记录启动运行有点慢要等一会儿三、拓展内容使用 Power Automate获取Access Token流程说明1、先创建一个Http触发的即时流拿到Flow的调用链接。2、通过Runbook写脚本先获取Access Token然后调用http将token传给Power Automate的Flow。3、为Runbook创建一个 Webhook保存Webhook链接。4、在Power Automate中创建一个定时Flow定时运行调用Azure Automation Webhook链接整个过程定时Flow调用Webhook —— Webhook启动 Runbook生成Token ——调用Http触发的Flow传递Token1、添加Webhook选择配置参数并运行然后点Update按钮之后下面的创建按钮才能点击另外记得要保存链接创建完就消失了。2、Runbook脚本参考记得替换代码里的Power Automate的Link# 获取 Token $endpoint http://localhost/metadata/identity/oauth2/token $header MSIHeaderValue $resource https://ai.azure.com $url $endpoint ?resource $resource api-version2019-08-01 $response Invoke-RestMethod -Method Get -Uri $url -Headers {X-IDENTITY-HEADER $header} -TimeoutSec 30 $accessToken $response.access_token # 计算剩余秒数 $expiresInSeconds 86400 # 默认24小时 if ($response.expires_on) { $expiresInSeconds $response.expires_on - [int64]((Get-Date).ToUniversalTime() - (Get-Date 1970-01-01)).TotalSeconds } if ($response.expires_in) { $expiresInSeconds $response.expires_in } # 过期时间北京时间 $expiresAt (Get-Date).AddSeconds($expiresInSeconds).AddHours(8).ToString(yyyy-MM-dd HH:mm:ss) Write-Output resource: $resource Write-Output token 前缀: $($accessToken.Substring(0, 50))... Write-Output expiresAt: $expiresAt Write-Output expiresInSeconds: $expiresInSeconds # 发送到 Power Automate $powerAutomateUrl https://default3670e84658dd465886fb00e4029045.d6.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/1d56556b0eb54970b5e4aa51eca915ea/triggers/manual/paths/invoke?api-version1sp%2Ftriggers%2Fmanual%2Frunsv1.0sigdDb-byafrIPQE9mafGkAYohXjmLrbJewIHqY_v8n5vY $body { resource $resource token $accessToken expiresAt $expiresAt expiresInSeconds $expiresInSeconds } | ConvertTo-Json Write-Output 发送的 JSON: $bodyJson Invoke-RestMethod -Method Post -Uri $powerAutomateUrl -Body $body -ContentType application/json Write-Output ✅ 发送成功3、Flow参考1 Http触发的Flow请求正文JSON架构{ type: object, properties: { resource: { type: string }, token: { type: string }, expiresAt: { type: string }, expiresInSeconds: { type: [ number, integer ] } } }2定时流调用Webhook的节点欢迎关注我的博客,与我一起学习,我将持续分享我的学习过程,我是 热爱学习的小翁同学~
Azure Automation Runbook 获取托管标识的访问令牌(Access Token)
发布时间:2026/6/10 4:12:19
Azure Automation Runbook 使用 IMDS实例元数据服务地址来获取托管标识 Token一、概述本文目的说明二、步骤1、创建自动化账户2、创建Runbook(1) 普通环境-脚本参考(2) 沙盒环境-脚本参考(3) 通用脚本3、运行三、拓展内容使用 Power Automate获取Access Token1、添加Webhook2、Runbook脚本参考3、Flow参考一、概述本文目的使用Azure Automation Runbook 安全、自动地获取 Token然后调用任何授权范围内的 Azure 资源形成一个完整的自动化闭环。说明任何支持 Microsoft Entra ID原 Azure AD认证的 Azure 服务都可以使用 Access Token 来调用。在自动化脚本或后台服务这类没有用户交互的场景中使用托管标识与 Access Token 被认为是 “最佳实践”。优势1、无须在代码、配置文件中存储或定期轮换密钥。2、消除了密钥泄露的风险。Access Token 是短期有效的权限也是事先通过 RBAC 精确授予的。二、步骤1、创建自动化账户在Azure门户中搜索自动化账户然后创建自动化账户。选择资源组输入名称然后审阅并创建就行了2、创建Runbook填好后审阅并创建 ↓输入代码保存然后发布如果要修改代码参考下图选择在门户中编辑 ↓(1) 普通环境-脚本参考# 标准 IMDS 地址 $endpoint http://169.254.169.254/metadata/identity/oauth2/token $resource https://ai.azure.com # 构造 URL $Url $endpoint ?resource $resource api-version2018-02-01 # 获取 Token标准环境用 Metadatatrue不需要额外 header $response Invoke-RestMethod -Method Get -Uri $Url -Headers {Metadatatrue} -TimeoutSec 30 -ErrorAction Stop Write-Output ✅ 成功获取 Token Write-Output $response.access_token(2) 沙盒环境-脚本参考Azure Automation 的 Runbook 运行在受限的沙盒环境中微软对 IMDS 端点做了封装需要用 localhost 加自定义 header。# 定义所有变量 $endpoint http://localhost/metadata/identity/oauth2/token $header MSIHeaderValue $resource https://ai.azure.com # 构造 URL $Url $endpoint ?resource $resource api-version2019-08-01 # 获取 Token $response Invoke-RestMethod -Method Get -Uri $Url -Headers {X-IDENTITY-HEADER $header} -TimeoutSec 30 -ErrorAction Stop Write-Output ✅ 成功获取 Token Write-Output $response.access_token(3) 通用脚本两个环境都能跑的代码沙盒环境已测试无误$resource https://ai.azure.com # 检测环境并使用正确的 endpoint 和 header if ($env:IDENTITY_ENDPOINT) { # 沙盒环境Automation Runbook $endpoint $env:IDENTITY_ENDPOINT $headers {X-IDENTITY-HEADER $env:IDENTITY_HEADER} Write-Output 检测到沙盒环境 } else { # 普通环境VM / Function / App Service $endpoint http://169.254.169.254/metadata/identity/oauth2/token $headers {Metadata true} Write-Output 检测到普通环境 } # 构造 URL 并获取 Token $Url $endpoint ?resource $resource api-version2019-08-01 $response Invoke-RestMethod -Method Get -Uri $Url -Headers $headers -TimeoutSec 30 Write-Output ✅ 成功获取 Token Write-Output $response.access_token参考文章1、在不使用 Azure cmdlet 的情况下生成访问令牌(在自动化 Runbook 的沙盒环境中必须使用环境变量 $env:IDENTITY_ENDPOINT 和 $env:IDENTITY_HEADER)2、用 HTTP 获取令牌(标准环境下的做法使用固定的 IMDS 地址 169.254.169.254 和 Metadata: true 头)3、运行可以看到输出和日志记录启动运行有点慢要等一会儿三、拓展内容使用 Power Automate获取Access Token流程说明1、先创建一个Http触发的即时流拿到Flow的调用链接。2、通过Runbook写脚本先获取Access Token然后调用http将token传给Power Automate的Flow。3、为Runbook创建一个 Webhook保存Webhook链接。4、在Power Automate中创建一个定时Flow定时运行调用Azure Automation Webhook链接整个过程定时Flow调用Webhook —— Webhook启动 Runbook生成Token ——调用Http触发的Flow传递Token1、添加Webhook选择配置参数并运行然后点Update按钮之后下面的创建按钮才能点击另外记得要保存链接创建完就消失了。2、Runbook脚本参考记得替换代码里的Power Automate的Link# 获取 Token $endpoint http://localhost/metadata/identity/oauth2/token $header MSIHeaderValue $resource https://ai.azure.com $url $endpoint ?resource $resource api-version2019-08-01 $response Invoke-RestMethod -Method Get -Uri $url -Headers {X-IDENTITY-HEADER $header} -TimeoutSec 30 $accessToken $response.access_token # 计算剩余秒数 $expiresInSeconds 86400 # 默认24小时 if ($response.expires_on) { $expiresInSeconds $response.expires_on - [int64]((Get-Date).ToUniversalTime() - (Get-Date 1970-01-01)).TotalSeconds } if ($response.expires_in) { $expiresInSeconds $response.expires_in } # 过期时间北京时间 $expiresAt (Get-Date).AddSeconds($expiresInSeconds).AddHours(8).ToString(yyyy-MM-dd HH:mm:ss) Write-Output resource: $resource Write-Output token 前缀: $($accessToken.Substring(0, 50))... Write-Output expiresAt: $expiresAt Write-Output expiresInSeconds: $expiresInSeconds # 发送到 Power Automate $powerAutomateUrl https://default3670e84658dd465886fb00e4029045.d6.environment.api.powerplatform.com:443/powerautomate/automations/direct/workflows/1d56556b0eb54970b5e4aa51eca915ea/triggers/manual/paths/invoke?api-version1sp%2Ftriggers%2Fmanual%2Frunsv1.0sigdDb-byafrIPQE9mafGkAYohXjmLrbJewIHqY_v8n5vY $body { resource $resource token $accessToken expiresAt $expiresAt expiresInSeconds $expiresInSeconds } | ConvertTo-Json Write-Output 发送的 JSON: $bodyJson Invoke-RestMethod -Method Post -Uri $powerAutomateUrl -Body $body -ContentType application/json Write-Output ✅ 发送成功3、Flow参考1 Http触发的Flow请求正文JSON架构{ type: object, properties: { resource: { type: string }, token: { type: string }, expiresAt: { type: string }, expiresInSeconds: { type: [ number, integer ] } } }2定时流调用Webhook的节点欢迎关注我的博客,与我一起学习,我将持续分享我的学习过程,我是 热爱学习的小翁同学~