Azure Advisor API可能的限制和筛选问题

时间:2017-11-06 17:18:46

标签: api powershell azure

我管理了许多订阅,因此当前的Azure Advisor虽然有趣,但要求您浏览太多屏幕,并且无法下载CSV建议并使用PowerShell进行编译。这导致尝试与API接口。我遇到的问题是,它似乎限制你200条记录,所以改变顶部什么都不做。许多记录都是通用安全警告,没有风险。我试图过滤掉它们,但我对API过滤器的了解很差,Microsoft提供的文档可能更好。以下是我正在使用的PowerShell命令:

呼叫:

$Response = Invoke-RestMethod -Uri "https://management.azure.com/subscriptions/${SubscriptionId}/providers/Microsoft.Advisor/Recommendations?api-version=2017-04-19&`$top=999&`$filter=risk -ne None" -Method GET -Headers @{"Authorization" = "$AccessToken"} -Verbose 

响应:

没有过滤参数

https://management.azure.com/subscriptions/<SubID>/providers/microsoft.Advisor/recommendations?api-version=2017-04-19&$top=200&$s
kiptoken=<Token>

使用过滤参数

Invoke-RestMethod -Uri "https://management.azure.com/subscriptions/${SubscriptionId}/providers/Microsoft.Advisor/Recommendations?api-version=2017-04-19&`$top=999&`$filter=risk -eq 'None'" -Method GET -Headers @{"Authorization" = "$AccessToken"} -Verbose
VERBOSE: GET https://management.azure.com/subscriptions/<sub ID>/providers/Microsoft.Advisor/Recommendations?api-version=2017-04-1
9&$top=999&$filter=risk -eq 'None' with 0-byte payload
Invoke-RestMethod : {"message":"Invalid $filter param"}
At line:1 char:13
+ $Response = Invoke-RestMethod -Uri "https://management.azure.com/subs ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

2 个答案:

答案 0 :(得分:1)

我今天一直在苦苦挣扎,因此我找到了你的文章。 我设法通过使用更高版本的API来解决我的问题,并稍微更改了过滤器

例如:

$filter="Category eq 'Cost'"
$url = "https://management.azure.com/subscriptions/$Subscriptionid/providers/microsoft.Advisor/recommendations?api-version=2017-04-19&`$top=999&`$filter=$filter"

答案 1 :(得分:0)

您的API中存在错误。正确的API语法为&$filter而非$filter,您可以看到自己的API,但却丢失了&

以下脚本适合我。

##get token
$TENANTID=""
$APPID=""
$PASSWORD=""
$result=Invoke-RestMethod -Uri https://login.microsoftonline.com/$TENANTID/oauth2/token?api-version=1.0 -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://management.core.windows.net/"; "client_id" = "$APPID"; "client_secret" = "$PASSWORD" }
$token=$result.access_token

##set subscriptionId 
$subscriptionId=""


$Headers=@{
    'authorization'="Bearer $token"
    'host'="management.azure.com"
    'contentype'='application/json'
}
$url="https://management.azure.com/subscriptions/$subscriptionID/providers/Microsoft.Advisor/Recommendations?api-version=2017-03-31&`$top=999`&$filter=risk -eq 'None'"


Invoke-RestMethod  -Uri $url  -Headers $Headers -Method GET

enter image description here