如何使用Powershell使用Google Safe Browsing API查询多个URL? [英] How can I query multiple URL's with Google Safe Browsing API using Powershell?
问题描述
下面的脚本可以很好地检查单个URL,但是在一个查询中检查URL列表的最简单方法是什么?将$URL
转换为数组不起作用(仅检查第一个条目).
The script below works fine to check a single URL, but what is the simplest way to check a list of URL's in one query? Turning $URL
into an array doesn't work (only the first entry is checked).
$HEADERS = @{ 'Content-Type' = "application/json" }
$GOOGLE_API_KEY='[API Key]'
$Uri = 'https://safebrowsing.googleapis.com/v4/threatMatches:find?key='+ $GOOGLE_API_KEY
$URL = 'http://www.sitetocheck.com'
$BODY = @()
$BODY +=[pscustomobject]@{"client" = @{"clientId" = "company"; "clientVersion" = "1.0"}; "threatInfo" = @{"threatTypes" = "MALWARE","SOCIAL_ENGINEERING","THREAT_TYPE_UNSPECIFIED","UNWANTED_SOFTWARE","POTENTIALLY_HARMFUL_APPLICATION"; "platformTypes" = "ANY_PLATFORM"; "threatEntryTypes" = "URL"; "threatEntries" = @{"url" = $URL}}}
$JSONBODY = $BODY | ConvertTo-Json
Invoke-RestMethod -Method 'POST' -Uri $Uri -Body $JSONBODY -Headers $HEADERS
推荐答案
如果查看现有代码,您会发现从目标URL派生其值的唯一原因是请求正文.
If you look at your existing code, you'll find that the only thing deriving it's value from the target URL is the request body.
将您的URL值准备为数组,然后使用创建请求正文并调用Invoke-RestMethod
的代码,并将其放入循环(或ForEach-Object
)中:
Prepare your URL values as an array, then take the code that creates the request body and calls Invoke-RestMethod
, and put that inside a loop (or ForEach-Object
):
$URLs = 'http://www.sitetocheck.com','http://www.othersitetocheck.com','http://somethingelse.com'
$HEADERS = @{ 'Content-Type' = "application/json" }
$GOOGLE_API_KEY='[API Key]'
$Uri = 'https://safebrowsing.googleapis.com/v4/threatMatches:find?key='+ $GOOGLE_API_KEY
foreach($URL in $URLs) {
$BODY = @([pscustomobject]@{"client" = @{"clientId" = "company"; "clientVersion" = "1.0"}; "threatInfo" = @{"threatTypes" = "MALWARE","SOCIAL_ENGINEERING","THREAT_TYPE_UNSPECIFIED","UNWANTED_SOFTWARE","POTENTIALLY_HARMFUL_APPLICATION"; "platformTypes" = "ANY_PLATFORM"; "threatEntryTypes" = "URL"; "threatEntries" = @{"url" = $URL}}})
$JSONBODY = $BODY | ConvertTo-Json
Invoke-RestMethod -Method 'POST' -Uri $Uri -Body $JSONBODY -Headers $HEADERS
}
这篇关于如何使用Powershell使用Google Safe Browsing API查询多个URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!