无法解析 Microsoft graph 的 url 字符串,因为使用 Invoke-MSGraphRequest 命令和查询参数

问题描述 投票:0回答:2

我无法使用当前 URL 进行解析和调用,因为当我使用 $filter 和 $select 查询参数时,它会破坏字符串,但它在 Postman 中工作得很好,并为我提供了我需要的所有数据。

Connect-MSGraph
Invoke-MSGraphRequest -Url "https://graph.microsoft.com/beta/deviceManagement/managedDevices?$select=emailaddress,id,imei,operatingSystem,ownerType,managedDeviceOwnerType&$filter=(operatingSystem eq 'iOS')" -HttpMethod GET

我需要过滤这些设备,如果所有权是个人的,我将再次使用图形 API 来使用 PATCH 更新对象设备。请帮忙解决这个问题

https://learn.microsoft.com/en-us/graph/query-parameters#filter-parameter https://learn.microsoft.com/en-us/graph/api/intune-devices-managementdevice-get?view=graph-rest-1.0

string powershell microsoft-graph-api
2个回答
3
投票

解决问题的直接方法是简单地用反引号来转义逐字

$
`
:

Invoke-MSGraphRequest -Url "https://graph.microsoft.com/beta/deviceManagement/managedDevices?`$select=emailaddress,id,imei,operatingSystem,ownerType,managedDeviceOwnerType&`$filter=(operatingSystem eq 'iOS')" -HttpMethod GET

或者使用单引号

'
来避免 PowerShell 尝试扩展 看起来 类似的变量 - URL 中的文字单引号必须通过 双倍 来转义:

Invoke-MSGraphRequest -Url 'https://graph.microsoft.com/beta/deviceManagement/managedDevices?$select=emailaddress,id,imei,operatingSystem,ownerType,managedDeviceOwnerType&$filter=(operatingSystem eq ''iOS'')' -HttpMethod GET

话虽这么说,我个人建议从更简单的部分构建查询参数:

$endpointURL = 'https://graph.microsoft.com/beta/deviceManagement/managedDevices'

# assign variable parts of the filter to a variable
$targetOperatingSystem = 'iOS'

# construct a hashtable containing all the query parameters
$GraphParameters = [ordered]@{
  '$select' = 'emailaddress,id,imei,operatingSystem,ownerType,managedDeviceOwnerType'
  '$filter' = "(operatingSystem eq '$targetOperatingSystem')"
}

# construct query string and final URL from the individual parts above
$queryString = $GraphParameters.GetEnumerator().ForEach({ $_.Key,$_.Value -join '=' }) -join '&'
$URL = $endpointURL,$queryString -join '?'

最后调用

Invoke-MSGraphRequest -Url $URL -HttpMethod Get


1
投票

如果有人在使用 Invoke-MSGraphRequest 时遇到错误:

无法验证参数“Url”的参数...

然后这解决了我的问题:不要在 URL 中使用 whitespaces,而是使用 html 编码 %20

所以代替:

Invoke-MSGraphRequest -HttpMethod GET -Url "https://graph.microsoft.com/v1.0/deviceAppManagement/managedAppRegistrations?`$filter=userId eq 'XXX'"

用途:

Invoke-MSGraphRequest -HttpMethod GET -Url "https://graph.microsoft.com/v1.0/deviceAppManagement/managedAppRegistrations?`$filter=userId%20eq%20'XXX'"

@Stephan - 我知道已经晚了,但它可能对你上面的例子有帮助。抱歉,我无法在帖子中回复,因为我没有足够的声誉:)

© www.soinside.com 2019 - 2024. All rights reserved.