考虑到这一点,经过大量挖掘,我找到了解决方案:
基本上,我们需要一个自定义磁贴来显示从我们的 REST api 检索到的数据。
1.在新的或现有的仪表板上创建一个新的空“Markdown”磁贴,为其指定一个“标题”
2。共享仪表板
3。导航到所有服务,在 ResourceGroup 过滤器中按“仪表板”过滤
- 单击包含“Markdown”图块的仪表板
- 记下“资源 ID”
在我们的场景中,我们使用了 Azure 自动化 Runbook。在这个场景中,我们使用了 Azure 资源管理器 REST api。
4.创建新的 RunBook [Powershell Runbook]
以下步骤涉及以下内容:
- 登录到 ResourceManagerAPI
- 按 ID 获取 Azure 资源 [上面的资源 ID]
- 按 ID [上述资源 ID] 更新 Azure 资源
在我们继续之前,我们需要获取我们的客户端凭据。为此:
- 点击门户菜单栏中的 Cloud Shell
- 输入“az”
- Type 'az ad sp create-for-rpac -n "runbooks"' //runbooks 只是一个名称,可以随意输入不同的字符串
- 上面的命令应该列出所需的凭据。如果发生错误,请联系您的 Azure 管理员并从他们的帐户运行它。
5.在空的 powershell Runbook 中,添加以下 2 个变量:
$ExpectedTileName = "Extension/HubsExtension/PartType/MarkdownPart"
$MarkdownTileTitle = "<The Markdown title you've set in the first step>"
6.获取 Access_Token [变量 表示从上一步检索到的值]
#Get Bearer Token
$TenantId = '<Your tenantID>'
$LoginUri = "https://login.microsoftonline.com/"+$TenantId+"/oauth2/token"
$params = @{
"grant_type"="client_credentials";
"client_id"="<appId>";
"client_secret"="<password>";
"resource"="https://management.azure.com";
}
$LoginResponse = Invoke-RestMethod -Uri $LoginUri -Method Post -Body $params
$Access_Token = $LoginResponse.access_token;
$Access_TokenString = "Bearer " + $Access_Token
7.通过 ResourceID 获取 DashboardResource:
#Get Resource
$RMUri = "https://management.azure.com/"+ $DashboardId +"?api-version=2015-08-01-preview"
$DashboardResource = (Invoke-RestMethod -Uri $RMUri -Method Get -Headers @{'Authorization'=$Access_TokenString}) | ConvertTo-Json -Depth 100 | ConvertFrom-Json
8.循环浏览仪表板中的所有磁贴。请注意,tile 不包含在数组中,因此您可能需要增加/减少 for 循环的长度。
#Loop through all tiles within the dashboard
$Parts = $DashboardResource.properties.lenses.0.0.parts
For ($i=0; $i -lt 200; $i++)
{
$Part = $Parts | Select-Object -Property $i.toString()
if($Part.$i)
{
if($Part.$i.metadata.type -eq $ExpectedTileName -And $Part.$i.metadata.settings.content.settings.title -eq $MarkdownTileTitle)
{
$Part.$i.metadata.settings.content.settings.content = <CustomValue ex: invoke a get request to your api>
}
}
else
{
break
}
}
9.最后我们需要更新仪表板资源
#Update Resource
$UpdateUri = "https://management.azure.com/"+ $DashboardId +"?api-version=2015-08-01-preview"
$JsonValue = $DashboardResource | ConvertTo-Json -Depth 100
Invoke-RestMethod -Uri $UpdateUri -Method Put -Headers @{'Authorization'=$Access_TokenString; 'Content-type'='application/json'} -Body $JsonValue
总结一下:
$ExpectedTileName = "Extension/HubsExtension/PartType/MarkdownPart"
$MarkdownTileTitle = "<The Markdown title you've set in the first step>"
#Get Bearer Token
$TenantId = '<Your subscriptionID>'
$LoginUri = "https://login.microsoftonline.com/"+$TenantId+"/oauth2/token"
$params = @{
"grant_type"="client_credentials";
"client_id"="<appId>";
"client_secret"="<password>";
"resource"="https://management.azure.com";
}
$LoginResponse = Invoke-RestMethod -Uri $LoginUri -Method Post -Body $params
$Access_Token = $LoginResponse.access_token;
$Access_TokenString = "Bearer " + $Access_Token
#Get Resource
$RMUri = "https://management.azure.com/"+ $DashboardId +"?api-version=2015-08-01-preview"
$DashboardResource = (Invoke-RestMethod -Uri $RMUri -Method Get -Headers @{'Authorization'=$Access_TokenString}) | ConvertTo-Json -Depth 100 | ConvertFrom-Json
#Loop through all tiles within the dashboard
$Parts = $DashboardResource.properties.lenses.0.0.parts
For ($i=0; $i -lt 200; $i++)
{
$Part = $Parts | Select-Object -Property $i.toString()
if($Part.$i)
{
if($Part.$i.metadata.type -eq $ExpectedTileName -And $Part.$i.metadata.settings.content.settings.title -eq $MarkdownTileTitle)
{
$Part.$i.metadata.settings.content.settings.content = <CustomValue ex: invoke a get request to your api>
}
}
else
{
break
}
}
#Update Resource
$UpdateUri = "https://management.azure.com/"+ $DashboardId +"?api-version=2015-08-01-preview"
$JsonValue = $DashboardResource | ConvertTo-Json -Depth 100
Invoke-RestMethod -Uri $UpdateUri -Method Put -Headers @{'Authorization'=$Access_TokenString; 'Content-type'='application/json'} -Body $JsonValue
结论
有了这个新创建的运行手册,我们现在可以安排它每 1 小时运行一次。在我们的例子中,1 小时太多了。以下文章展示了我们如何安排 Runbook 每 1 分钟运行一次。
https://blogs.technet.microsoft.com/stefan_stranger/2017/06/21/azure-scheduler-schedule-your-runbooks-more-often-than-every-hour/