forked from dfinke/PowerShell_Search_Bing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bing.ps1
57 lines (50 loc) · 1.64 KB
/
bing.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#requires -Version 3
function Invoke-BingQuery
{
Param(
[Parameter(Mandatory = $true)]
$Query,
[Parameter(Mandatory = $true)]
$APIAuthorizationKey,
[Parameter()]
[Switch]$RawData
)
begin
{
$bytes = [System.Text.Encoding]::UTF8.GetBytes("$APIAuthorizationKey`:$APIAuthorizationKey")
$Authorization = 'Basic {0}' -f [System.Convert]::ToBase64String($bytes)
}
process
{
foreach($BingQuery in $Query)
{
$BingUrl = "https://api.datamarket.azure.com/Bing/Search/Web?Query='$($BingQuery)'&`$format=JSON"
Write-Verbose -Message "Query URL is $BingUrl"
Write-Verbose -Message 'Will perform the query now...'
$results = (Invoke-RestMethod -Uri $BingUrl -Headers @{
Authorization = $Authorization
})
Write-Verbose -Message 'Processing results'
If(-not ($RawData))
{
Write-Verbose -Message 'Displaying results as objects'
foreach($result in $($results.d.results))
{
[PSCustomObject][Ordered]@{
Title = $result.title
Description = $result.description
URI = $result.URL
}
}
}
else
{
Write-Verbose -Message 'Displaying results as Raw Data'
Write-Output -InputObject $results
}
}
}
end
{
}
}