注意事項
如果您是美國政府客戶,請使用適用於美國政府的適用於端點的 Microsoft Defender 客戶中列出的 URI。
提示
為了獲得更好的效能,請使用更靠近您的地理位置的伺服器:
- us.api.security.microsoft.com
- eu.api.security.microsoft.com
- uk.api.security.microsoft.com
- au.api.security.microsoft.com
- swa.api.security.microsoft.com
- ina.api.security.microsoft.com
- aea.api.security.microsoft.com
使用 PowerShell 執行進階查詢。 如需詳細資訊,請參閱 進階搜捕 API。
在本節中,我們會共用 PowerShell 範例來擷取權杖,並使用它來執行查詢。
開始之前
您首先需要 創建一個應用程序。
準備說明
開啟 PowerShell 視窗。
如果您的原則不允許您執行 PowerShell 命令,您可以執行下列命令:
Set-ExecutionPolicy -ExecutionPolicy Bypass
如需詳細資訊,請參閱 PowerShell 檔。
取得代幣
- 執行下列命令:
$tenantId = '00000000-0000-0000-0000-000000000000' # Paste your own tenant ID here
$appId = '11111111-1111-1111-1111-111111111111' # Paste your own app ID here
$appSecret = '22222222-2222-2222-2222-222222222222' # Paste your own app secret here
$resourceAppIdUri = 'https://api.securitycenter.microsoft.com'
$oAuthUri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
$body = [Ordered] @{
resource = "$resourceAppIdUri"
client_id = "$appId"
client_secret = "$appSecret"
grant_type = 'client_credentials'
}
$response = Invoke-RestMethod -Method Post -Uri $oAuthUri -Body $body -ErrorAction Stop
$aadToken = $response.access_token
其中
- $tenantId:您要代表其執行查詢的租用戶識別碼 (也就是說,查詢會在此租用戶的資料上執行)
- $appId:Microsoft Entra應用程式的識別碼 (應用程式必須具有適用於端點的 Defender 的「執行進階查詢」許可權)
- $appSecret:Microsoft Entra應用程序的秘密
執行查詢
執行下列查詢:
$token = $aadToken
$query = 'DeviceRegistryEvents | limit 10' # Paste your own query here
$url = "https://api.securitycenter.microsoft.com/api/advancedqueries/run"
$headers = @{
'Content-Type' = 'application/json'
Accept = 'application/json'
Authorization = "Bearer $aadToken"
}
$body = ConvertTo-Json -InputObject @{ 'Query' = $query }
$webResponse = Invoke-WebRequest -Method Post -Uri $url -Headers $headers -Body $body -ErrorAction Stop
$response = $webResponse | ConvertFrom-Json
$results = $response.Results
$schema = $response.Schema
- $results包含查詢的結果
- $schema包含查詢結果的結構描述
複雜查詢
如果您想要執行複雜的查詢 (或多行查詢) ,請將查詢儲存在檔案中,然後執行下列命令,而不是上述範例中的第一行:
$query = [IO.File]::ReadAllText("C:\myQuery.txt"); # Replace with the path to your file
使用查詢結果工作
您現在可以使用查詢結果。
若要在檔案 file1.csv 中以 CSV 格式輸出查詢結果,請執行下列命令:
$results | ConvertTo-Csv -NoTypeInformation | Set-Content C:\file1.csv
若要在檔案file1.json中以 JSON 格式輸出查詢結果,請執行下列命令:
$results | ConvertTo-Json | Set-Content file1.json
相關文章
提示
想要深入了解? 在我們的技術社區中與Microsoft安全社區Engage:適用於端點的 Microsoft Defender技術社區。