【问题标题】:Using $filter through Microsoft graph API request通过 Microsoft 图形 API 请求使用 $filter
【发布时间】:2021-09-06 15:01:55
【问题描述】:

我正在尝试通过一个小的 Powershell 脚本使用 MS 图形 API,如此处所述 https://docs.microsoft.com/en-us/graph/api/riskyusers-list?view=graph-rest-beta&tabs=http 这个过滤器没有给出任何结果,如果我删除它它可以工作,但没有给出完整的列表(只有几个结果)

$ApplicationID = "45xxxxxxxx"
$TenatDomainName = "2a3xxxxx"
$AccessSecret = Read-Host "Enter Secret"

$Body = @{    
Grant_Type    = "client_credentials"
Scope         = "https://graph.microsoft.com/.default"
client_Id     = $ApplicationID
Client_Secret = $AccessSecret
} 

$ConnectGraph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenatDomainName/oauth2/v2.0/token" -Method POST -Body $Body

$token = $ConnectGraph.access_token

#$GrapRisk = "https://graph.microsoft.com/v1.0/identityProtection/riskyUsers"
$GrapRisk = "https://graph.microsoft.com/beta/identityProtection/riskyUsers?$filter=riskLevel eq microsoft.graph.riskLevel'medium'"


$riskyList= (Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $GrapRisk -Method Get).value |select userPrincipalName, riskState, riskLastUpdatedDateTime, riskLevel

$riskyList

我通过 Powershell ISE 执行此脚本,但即使使用 curl 和终端,我也无法使用 filter 参数。 谢谢

【问题讨论】:

  • 将 url 中的 ?$filter 更改为 ?`$filter $ 将阻止 PowerShell 尝试将其解析为插值变量

标签: azure powershell graph


【解决方案1】:

当字符串文字使用双引号 ("'s) 时,PowerShell 将其解释为 可扩展,这意味着 PowerShell 在评估时将尝试解析和扩展变量表达式(如 $filter)字符串。

使用反引号 (`) 转义 $,它应该可以工作:

$GrapRisk = "https://graph.microsoft.com/beta/identityProtection/riskyUsers?`$filter=riskLevel eq microsoft.graph.riskLevel'medium'"

有关 PowerShell 中不同类型字符串文字语义的更多详细信息,请阅读about_Quoting_Rules help topic

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 2018-05-10
    相关资源
    最近更新 更多