powershell Microsoft安全中心API筛选器不工作

14ifxucb  于 12个月前  发布在  Shell
关注(0)|答案(3)|浏览(112)

我尝试使用这个URI来查找特定的机器ID,这样我们就可以查询最近登录的用户。当我运行这个程序时,我会得到Defender中所有设备的输出。我被困住了不知道该何去何从
https://api.securitycenter.microsoft.com/api/machines?$filter=computerDnsName eq '计算机名在此”

ogsagwnx

ogsagwnx2#

要根据设备名称查找ID,请使用此API调用。
https://api.securitycenter.microsoft.com/api/machines?$filter=computerDnsName eq“此处显示设备名称”。一旦你得到了设备名,你就可以用这个来查找登录的用户。
https://api.securitycenter.microsoft.com/api/machines/id

eqfvzcg8

eqfvzcg83#

经过进一步调查,似乎更有可能的问题,一些id:s不匹配的值在UI中发现只影响一些设备,可能已注册多次,由于被克隆(我们使用SCCM)。我认为对于大多数设备来说,这是预期的
这里的问题是你从这个API中获得的机器的id:https://api.securitycenter.microsoft.com/api/machines,可能与您在其他API中需要的ID不同:所以我认为这是一个有效的问题,到目前为止,我不确定答案。
在powershell中,这是可行的:

$tenantId = '****' # Paste your directory (tenant) ID here
$clientId = '****' # Paste your application (client) ID here
$appSecret = '****' # Paste the thumbprint of your certificate here

$resourceAppIdUri = 'https://api.securitycenter.microsoft.com'
$oAuthUri = "https://login.windows.net/$tenantId/oauth2/token"
$authBody = [Ordered] @{
  resource = $resourceAppIdUri
  client_id = $clientId
  client_secret = $appSecret
  grant_type = 'client_credentials'
}
$authResponse = Invoke-RestMethod -Method Post -Uri $oAuthUri -Body $authBody -ErrorAction Stop
$token = $authResponse.access_token

# Create the headers for the API request
$headers = @{
    "Authorization" = "Bearer $token"
    "Content-Type"  = "application/json"
}    
$uri = "https://api.securitycenter.microsoft.com/api/machines?`$filter=computerDnsName eq 'name.domain.com'"
$response = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get
$id = $response.value[0].id

这将给予你和id说(005067aa02b68b884f2c2cf3ba2e678b8c717299),但不一定是你想要的id,没有“机器id”返回,“aadDeviceId”不能使用,它有时也是空的。返回的id有时与Defender GUI中列出的“Device id”(第二个查询所需的id)不相同。如果是这种情况,查询将返回空结果:

$uri = "https://api.securitycenter.microsoft.com/api/machines/005067aa02b68b884f2c2cf3ba2e678b8c717299"
$response = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get

如果有人有一个工作的例子,我会很有兴趣看到它,并尝试自己。这个API也有同样的问题:https://api.securitycenter.microsoft.com/api/machines/{id}/software。我不知道如何可靠地获得正确的id,除了从GUI复制它。这似乎是API中的一个bug,或者我遗漏了一些明显的东西。
我已经在Github上打开了这个问题:https://github.com/MicrosoftDocs/microsoft-365-docs/issues/12717#issuecomment-1708068386

相关问题