【问题标题】:Azure Portal Custom Tiles - Markdown tileAzure 门户自定义磁贴 - Markdown 磁贴
【发布时间】:2019-01-21 08:16:07
【问题描述】:

是否可以在 azure 仪表板中显示自定义信息? 我正在搜索如何在 azure 仪表板中添加自定义内容,但没有找到任何内容。唯一关闭的是允许显示 html 的 markdown tile。

【问题讨论】:

  • 你需要把它分成两部分:问答
  • 你是对的,谢谢你的评论。

标签: azure powershell azure-automation azureportal azure-runbook


【解决方案1】:

考虑到这一点,经过大量挖掘,我找到了解决方案:

基本上,我们需要一个自定义磁贴来显示从我们的 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/

【讨论】:

猜你喜欢
  • 2012-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-13
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
相关资源
最近更新 更多