编写Azure时间序列见解脚本数据访问策略

时间:2020-03-17 08:11:07

标签: azure powershell azure-sdk azure-rest-api azure-timeseries-insights

存在使用Time Series Insights将用户添加到Data Access Policies的手动方法。
尽管如此,无法批量添加用户。

enter image description here

是否可以使用PowerShell/Azure REST API/SDK对这两种方案进行脚本化/自动化?

1 个答案:

答案 0 :(得分:0)

根据我的研究,我们可以使用以下Azure Rest API在“时序分析”策略中创建数据访问策略。有关更多详细信息,请参阅document

PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.TimeSeriesInsights/environments/{environmentName}/accessPolicies/{accessPolicyName}?api-version=2018-08-15-preview

例如

Connect-AzAccount



$tenantId="<your tenant id>"
$resource="https://management.core.windows.net/"



$context=Get-AzContext 



$token=$context.TokenCache.ReadItems() |Where-Object { ($_.TenantId -eq $tenantId) -and ($_.Resource -eq $resource)  }
$accesstoken=$token.AccessToken
$body =@{
 "properties" = @{

  "principalObjectId" ="<the object id of the user or service principal>"
  "roles"= @("Reader")
  "description"="reader" 
 }



}| ConvertTo-Json



$url="https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.TimeSeriesInsights/environments/{environmentName}/accessPolicies/{accessPolicyName}?api-version=2018-08-15-preview"
$result =Invoke-RestMethod -Uri $url  -Method Put -Headers @{"Authorization" = "Bearer $accesstoken"} -Body $body -UseBasicParsing



$result.properties

enter image description here