通过 Microsoft graph API 请求使用 $filter

Using $filter through Microsoft graph API request

我正在尝试通过此处所述的小型 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 参数。 谢谢

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

使用反引号 (`) 转义 $,它应该有效:

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

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