【发布时间】:2019-10-11 18:16:58
【问题描述】:
我正在尝试使用 Azure Application Insights REST API 调用来使用 Powershell 获取一些摘要信息。 (https://docs.microsoft.com/en-us/rest/api/monitor/alertsmanagement/alerts/getsummary)
所以我继续在 Azure AD 中创建应用注册(称为 AppInsightsRESTTest),使用委托选项为其分配权限(App Insights-->Data-->Read)。我也为它生成了密钥。现在我复制了这个应用程序的应用程序 ID 和密钥,并在下面的 powershell 中使用它。我还将我在 Azure AD 中注册的这个应用程序添加到我的 App Insights 实例中作为 CONTRIBUTOR 角色。
#SPN ClientId and Secret
$ClientID = "25325f5d-XXXXXXXXXXXXX26fef568d" #Key from Azure AD App ID Created
$ClientSecret = "uX[CNk[XXXXXXXXXXXXgGcN4" #Secret key from Azure AD
$tennantid = "fa061982-XXXXXXXXXXX1ce727" #Directory ID for THREE PROJECt
$SubscriptionID = "d2e4beXXXXXXXXXXXXXXX686542c"
$TokenEndpoint = {https://login.microsoftonline.com/{0}/oauth2/token} -f $tennantid
$ARMResource = "https://management.core.windows.net/";
$Body = @{
'resource'= $ARMResource
'client_id' = $ClientID
'grant_type' = 'client_credentials'
'client_secret' = $ClientSecret
}
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{'accept'='application/json'}
Body = $Body
Method = 'Post'
URI = $TokenEndpoint
}
$token = Invoke-RestMethod @params
$token | select access_token, @{L='Expires';E={[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($_.expires_on))}} | fl *
$SubscriptionURI = "https://management.azure.com/subscriptions/$SubscriptionID" +'/providers/Microsoft.AlertsManagement/alertsSummary?groupby=alertRule&api-version=2018-05-05'
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{
'authorization'="Bearer $($Token.access_token)"
}
Method = 'Get'
URI = $SubscriptionURI
}
Invoke-RestMethod @params
上面的代码给我返回了Token,但是Invoke方法返回的是空值并且没有Json。如果我使用 AUTHO BEARER TOKEN 从浏览器尝试相同的操作,它会返回正确的 Json 和适当的数据。有人可以帮我解决我在这里错过的事情,因为我的 powershell 没有返回任何东西吗?
有什么帮助吗?
【问题讨论】:
标签: azure-application-insights azure-rest-api