How can I query multiple URL's with Google Safe Browsing API using Powershell?

273 Views Asked by At

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
1

There are 1 best solutions below

0
Mathias R. Jessen On BEST ANSWER

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.

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
}