如何使用 Powershell 通过 Google 安全浏览 API 查询多个 URL?
How can I query multiple URL's with Google Safe Browsing API using Powershell?
下面的脚本可以很好地检查单个 URL,但是在一个查询中检查 URL 列表的最简单方法是什么?将 $URL
转换为数组不起作用(仅检查第一个条目)。
$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 中获取其值的是请求正文。
将您的 URL 值准备为数组,然后获取创建请求正文并调用 Invoke-RestMethod
的代码,并将其放入循环(或 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
}
下面的脚本可以很好地检查单个 URL,但是在一个查询中检查 URL 列表的最简单方法是什么?将 $URL
转换为数组不起作用(仅检查第一个条目)。
$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 中获取其值的是请求正文。
将您的 URL 值准备为数组,然后获取创建请求正文并调用 Invoke-RestMethod
的代码,并将其放入循环(或 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
}