Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Newbie question: How can I perform web searches from PowerShell?

Closed Thread
 
Thread Tools Display Modes
Old 05-04-2007   #1 (permalink)
Mark Allen
Guest


 

Newbie question: How can I perform web searches from PowerShell?

I have a dream. It's a strange dream, but there you go. I would like to be
able to run a script, supply it search terms as parameters, and get the
results back.

I'm sure it's been done, but it is very difficult to search for. Can someone
please post an example?

(Note: I've seen the blog entries about searching Windows Desktop Search,
and that's cool too, but I run Vista and it doesn't work, and ideally I'd
like to search the Internet. I wouldn't mind searching within Vista's
built-in search as well though.)

Thanks in advance,

Mark
Old 05-05-2007   #2 (permalink)
Marco Shaw
Guest


 

Re: Newbie question: How can I perform web searches from PowerShell?

Mark Allen wrote:
> I have a dream. It's a strange dream, but there you go. I would like to be
> able to run a script, supply it search terms as parameters, and get the
> results back.
>
> I'm sure it's been done, but it is very difficult to search for. Can someone
> please post an example?
>
> (Note: I've seen the blog entries about searching Windows Desktop Search,
> and that's cool too, but I run Vista and it doesn't work, and ideally I'd
> like to search the Internet. I wouldn't mind searching within Vista's
> built-in search as well though.)
>
> Thanks in advance,
>
> Mark


I'm sure there's a google API that one can tap into. The bigger issue
is how are the results going to be formatted...

I'd like your comments on the output format you'd like to see...

Marco
Old 05-05-2007   #3 (permalink)
Mark Allen
Guest


 

Re: Newbie question: How can I perform web searches from PowerShel

Thanks! Good point RE Google's API.

As for the results formatting, I'd like to see whatever the search engine
gives:

Title (from the link)
Link
Summary/description
Date (if shown)
Link to cached version
Link to similar pages (if that's even there)

Then, you could filter on whatever it was you wanted to see.

get-websearch -results 20 "Powershell" | format-list Title, Description, Link

As I typed what a sample command might look like, I realized the need to
specify how many results are returned. There's probably more like that.

"Marco Shaw" wrote:

> Mark Allen wrote:
> > I have a dream. It's a strange dream, but there you go. I would like to be
> > able to run a script, supply it search terms as parameters, and get the
> > results back.
> >
> > I'm sure it's been done, but it is very difficult to search for. Can someone
> > please post an example?
> >
> > (Note: I've seen the blog entries about searching Windows Desktop Search,
> > and that's cool too, but I run Vista and it doesn't work, and ideally I'd
> > like to search the Internet. I wouldn't mind searching within Vista's
> > built-in search as well though.)
> >
> > Thanks in advance,
> >
> > Mark

>
> I'm sure there's a google API that one can tap into. The bigger issue
> is how are the results going to be formatted...
>
> I'd like your comments on the output format you'd like to see...
>
> Marco
>

Old 05-05-2007   #4 (permalink)
Don Jones [MVP]
Guest


 

Re: Newbie question: How can I perform web searches from PowerShel

Go to www.sapienpress.com/powershell.asp. Download the book's sample
scripts. One of them retrieves RSS feeds and outputs them formatted. This
isn't exactly what you want, but I suspect it'll give you a start.

--
Don Jones
Windows PowerShell MVP
Founder: www.ScriptingAnswers.com
Co-Author: "Windows PowerShell: TFM"

"Mark Allen" <MarkAllen@discussions.microsoft.com> wrote in message
news:FA3827B0-6469-4DBE-8DF9-13677AE6A1DC@microsoft.com...
> Thanks! Good point RE Google's API.
>
> As for the results formatting, I'd like to see whatever the search engine
> gives:
>
> Title (from the link)
> Link
> Summary/description
> Date (if shown)
> Link to cached version
> Link to similar pages (if that's even there)
>
> Then, you could filter on whatever it was you wanted to see.
>
> get-websearch -results 20 "Powershell" | format-list Title, Description,
> Link
>
> As I typed what a sample command might look like, I realized the need to
> specify how many results are returned. There's probably more like that.
>
> "Marco Shaw" wrote:
>
>> Mark Allen wrote:
>> > I have a dream. It's a strange dream, but there you go. I would like to
>> > be
>> > able to run a script, supply it search terms as parameters, and get the
>> > results back.
>> >
>> > I'm sure it's been done, but it is very difficult to search for. Can
>> > someone
>> > please post an example?
>> >
>> > (Note: I've seen the blog entries about searching Windows Desktop
>> > Search,
>> > and that's cool too, but I run Vista and it doesn't work, and ideally
>> > I'd
>> > like to search the Internet. I wouldn't mind searching within Vista's
>> > built-in search as well though.)
>> >
>> > Thanks in advance,
>> >
>> > Mark

>>
>> I'm sure there's a google API that one can tap into. The bigger issue
>> is how are the results going to be formatted...
>>
>> I'd like your comments on the output format you'd like to see...
>>
>> Marco
>>


Old 05-06-2007   #5 (permalink)
Joris van Lier
Guest


 

Re: Newbie question: How can I perform web searches from PowerShell?

Mark Allen wrote:
> I have a dream. It's a strange dream, but there you go. I would like
> to be able to run a script, supply it search terms as parameters, and
> get the results back.
>
> I'm sure it's been done, but it is very difficult to search for. Can
> someone please post an example?
>
> (Note: I've seen the blog entries about searching Windows Desktop
> Search, and that's cool too, but I run Vista and it doesn't work, and
> ideally I'd like to search the Internet. I wouldn't mind searching
> within Vista's built-in search as well though.)
>
> Thanks in advance,
>
> Mark


I've made a start based on a one-liner that retrieves the IE7 Search Providers from registry. This script is searches using the default provider and reurns the HTML.
Since the HTML returned by various providers can differ, the formatting is left as an excercise to the reader (another way of saying it would be: it's late and i'm lazy)
I should warn you there's an issue with multiple arguments after the Function-Call, (Search-Web arg1 arg2 arg3) where only arg1 is taken into account, i am probably using [string]::Join in the wrong way.

Would anyone care to comment on why, or how to write a function that combines pipeline input as well as parameters?

Anyway i hope this give's you an idea about consuming multiple providers instead of tight coupling with a specific provider's webservices.

function Search-Web {
param([array]$searchterms=$args)
begin {
$defaultprovider = (gp "HKCU:\Software\Microsoft\Internet Explorer\SearchScopes").DefaultScope
$url = (gp "HKCU:\Software\Microsoft\Internet Explorer\SearchScopes\$defaultprovider").URL
$selectedprovider = $searchProviders | Where-Object {$name -like $provider}
}
process {
$searchterms += $_
}
end {
$url = $url.Replace("{searchTerms}", [string]::Join($searchterms, " "))
(new-object System.IO.StreamReader ([System.Net.WebRequest]::Create([uri]$url)).GetResponse().GetResponseStream()).ReadToEnd()
}
}



PS> Set-PSDebug -trace 2

PS> search-web powershell web search
DEBUG: 1+ search-web powershell web search
DEBUG: ! CALL function 'Search-Web'
DEBUG: 4+ $defaultprovider = (gp "HKCU:\Software\Microsoft\Internet Explorer\SearchScopes").DefaultScope
DEBUG: ! SET $defaultprovider = '{635D24EC-DF3E-4331-BE6E-A807443FDE81}'.
DEBUG: 5+ $url = (gp "HKCU:\Software\Microsoft\Internet Explorer\SearchScopes\$defaultprovider").URL
DEBUG: ! SET $url = 'http://www.google.com/search?q={searchTerms}&rls=com.micr...'.
DEBUG: 6+ $selectedprovider = $searchProviders | Where-Object {$name -like $provider}
DEBUG: 6+ $selectedprovider = $searchProviders | Where-Object {$name -like $provider}
DEBUG: ! SET $selectedprovider = ''.
DEBUG: ! CALL function 'Search-Web'
DEBUG: 9+ $searchterms += $_
DEBUG: ! SET $searchterms = 'powershell '.
DEBUG: ! CALL function 'Search-Web'
DEBUG: 12+ $url = $url.Replace("{searchTerms}", [string]::Join($searchterms, " "))
DEBUG: ! CALL method 'static System.String Join(String separator, String[] value)'
DEBUG: ! CALL method 'System.String Replace(String oldValue, String newValue)'
DEBUG: ! SET $url = 'http://www.google.com/search?q= &rls=com.microsoft:{langu...'.
DEBUG: 13+ (new-object System.IO.StreamReader
([System.Net.WebRequest]::Create([uri]$url)).GetResponse().GetResponseStream()).ReadToEnd()
DEBUG: ! CALL method 'static System.Net.WebRequest Create(Uri requestUri)'
DEBUG: ! CALL method 'System.Net.WebResponse GetResponse()'
DEBUG: ! CALL method 'System.IO.Stream GetResponseStream()'
DEBUG: ! CALL method 'System.String ReadToEnd()'
......
--
Joris van Lier
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://whizzrd.spaces.live.com

Old 05-06-2007   #6 (permalink)
Don Jones [MVP]
Guest


 

Re: Newbie question: How can I perform web searches from PowerShell?

>> Comments below.

"Joris van Lier" <whizzrd@hotmail.com> wrote in message
news:Oc%232yW%23jHHA.4624@TK2MSFTNGP03.phx.gbl...

I've made a start based on a one-liner that retrieves the IE7 Search
Providers from registry. This script is searches using the default provider
and reurns the HTML.
Since the HTML returned by various providers can differ, the formatting is
left as an excercise to the reader (another way of saying it would be: it's
late and i'm lazy)

>> Actually, the best way would be to emit a custom object for each search
>> result, using properties to contain the URL, abstract, etc. That way a
>> format.ps1xml file could be used to specify the formatting, including a
>> custom format. I'm a big advocate of ALWAYS emitting custom objects
>> rather than hardcoded formatting - that way formatting can be easily
>> changed per-person, if desired. And, that technique best leverages how
>> PowerShell is built - objects, not text.


I should warn you there's an issue with multiple arguments after the
Function-Call, (Search-Web arg1 arg2 arg3) where only arg1 is taken into
account, i am probably using [string]::Join in the wrong way.

Would anyone care to comment on why, or how to write a function that
combines pipeline input as well as parameters?

>> Right now, you're can't properly right a function to behave like a
>> cmdlet - that is, able to accept pipeline input or named arguments.
>> That's because functions don't have property get/set capability. I think
>> that capability has been suggested in Connect for the long, long "v2"
>> wish list.


Anyway i hope this give's you an idea about consuming multiple providers
instead of tight coupling with a specific provider's webservices.

function Search-Web {
param([array]$searchterms=$args)
begin {
$defaultprovider = (gp "HKCU:\Software\Microsoft\Internet
Explorer\SearchScopes").DefaultScope
$url = (gp "HKCU:\Software\Microsoft\Internet
Explorer\SearchScopes\$defaultprovider").URL
$selectedprovider = $searchProviders | Where-Object {$name -like $provider}
}
process {
$searchterms += $_
}
end {
$url = $url.Replace("{searchTerms}", [string]::Join($searchterms, " "))
(new-object System.IO.StreamReader
([System.Net.WebRequest]::Create([uri]$url)).GetResponse().GetResponseStream()).ReadToEnd()
}
}



PS> Set-PSDebug -trace 2

PS> search-web powershell web search
DEBUG: 1+ search-web powershell web search
DEBUG: ! CALL function 'Search-Web'
DEBUG: 4+ $defaultprovider = (gp "HKCU:\Software\Microsoft\Internet
Explorer\SearchScopes").DefaultScope
DEBUG: ! SET $defaultprovider =
'{635D24EC-DF3E-4331-BE6E-A807443FDE81}'.
DEBUG: 5+ $url = (gp "HKCU:\Software\Microsoft\Internet
Explorer\SearchScopes\$defaultprovider").URL
DEBUG: ! SET $url =
'http://www.google.com/search?q={searchTerms}&rls=com.micr...'.
DEBUG: 6+ $selectedprovider = $searchProviders | Where-Object
{$name -like $provider}
DEBUG: 6+ $selectedprovider = $searchProviders | Where-Object
{$name -like $provider}
DEBUG: ! SET $selectedprovider = ''.
DEBUG: ! CALL function 'Search-Web'
DEBUG: 9+ $searchterms += $_
DEBUG: ! SET $searchterms = 'powershell '.
DEBUG: ! CALL function 'Search-Web'
DEBUG: 12+ $url = $url.Replace("{searchTerms}",
[string]::Join($searchterms, " "))
DEBUG: ! CALL method 'static System.String Join(String separator,
String[] value)'
DEBUG: ! CALL method 'System.String Replace(String oldValue, String
newValue)'
DEBUG: ! SET $url = 'http://www.google.com/search?q=
&rls=com.microsoft:{langu...'.
DEBUG: 13+ (new-object System.IO.StreamReader
([System.Net.WebRequest]::Create([uri]$url)).GetResponse().GetResponseStream()).ReadToEnd()
DEBUG: ! CALL method 'static System.Net.WebRequest Create(Uri
requestUri)'
DEBUG: ! CALL method 'System.Net.WebResponse GetResponse()'
DEBUG: ! CALL method 'System.IO.Stream GetResponseStream()'
DEBUG: ! CALL method 'System.String ReadToEnd()'
......
--
Joris van Lier
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://whizzrd.spaces.live.com

Old 05-06-2007   #7 (permalink)
Joris van Lier
Guest


 

Re: Newbie question: How can I perform web searches from PowerShell?

Don Jones [MVP] wrote:
>>> Comments below.

>
> "Joris van Lier" <whizzrd@hotmail.com> wrote in message
> news:Oc%232yW%23jHHA.4624@TK2MSFTNGP03.phx.gbl...
>
> I've made a start based on a one-liner that retrieves the IE7 Search
> Providers from registry. This script is searches using the default
> provider and reurns the HTML.
> Since the HTML returned by various providers can differ, the
> formatting is left as an excercise to the reader (another way of
> saying it would be: it's late and i'm lazy)
>
>>> Actually, the best way would be to emit a custom object for each
>>> search result, using properties to contain the URL, abstract, etc.
>>> That way a format.ps1xml file could be used to specify the
>>> formatting, including a custom format. I'm a big advocate of ALWAYS
>>> emitting custom objects rather than hardcoded formatting - that way
>>> formatting can be easily changed per-person, if desired. And, that
>>> technique best leverages how PowerShell is built - objects, not
>>> text.

>
> I should warn you there's an issue with multiple arguments after the
> Function-Call, (Search-Web arg1 arg2 arg3) where only arg1 is taken
> into account, i am probably using [string]::Join in the wrong way.
>
> Would anyone care to comment on why, or how to write a function that
> combines pipeline input as well as parameters?
>
>>> Right now, you're can't properly right a function to behave like a
>>> cmdlet - that is, able to accept pipeline input or named arguments.
>>> That's because functions don't have property get/set capability. I
>>> think that capability has been suggested in Connect for the long,
>>> long "v2" wish list.

>
> Anyway i hope this give's you an idea about consuming multiple
> providers instead of tight coupling with a specific provider's
> webservices.
>
> function Search-Web {
> param([array]$searchterms=$args)
> begin {
> $defaultprovider = (gp "HKCU:\Software\Microsoft\Internet
> Explorer\SearchScopes").DefaultScope
> $url = (gp "HKCU:\Software\Microsoft\Internet
> Explorer\SearchScopes\$defaultprovider").URL
> $selectedprovider = $searchProviders | Where-Object {$name -like
> $provider} }
> process {
> $searchterms += $_
> }
> end {
> $url = $url.Replace("{searchTerms}", [string]::Join($searchterms, "
> ")) (new-object System.IO.StreamReader
> ([System.Net.WebRequest]::Create([uri]$url)).GetResponse().GetResponseStream()).ReadToEnd()
> }
> }
>
>
>
>> Set-PSDebug -trace 2

>
>> search-web powershell web search

> DEBUG: 1+ search-web powershell web search
> DEBUG: ! CALL function 'Search-Web'
> DEBUG: 4+ $defaultprovider = (gp "HKCU:\Software\Microsoft\Internet
> Explorer\SearchScopes").DefaultScope
> DEBUG: ! SET $defaultprovider =
> '{635D24EC-DF3E-4331-BE6E-A807443FDE81}'.
> DEBUG: 5+ $url = (gp "HKCU:\Software\Microsoft\Internet
> Explorer\SearchScopes\$defaultprovider").URL
> DEBUG: ! SET $url =
> 'http://www.google.com/search?q={searchTerms}&rls=com.micr...'.
> DEBUG: 6+ $selectedprovider = $searchProviders | Where-Object
> {$name -like $provider}
> DEBUG: 6+ $selectedprovider = $searchProviders | Where-Object
> {$name -like $provider}
> DEBUG: ! SET $selectedprovider = ''.
> DEBUG: ! CALL function 'Search-Web'
> DEBUG: 9+ $searchterms += $_
> DEBUG: ! SET $searchterms = 'powershell '.
> DEBUG: ! CALL function 'Search-Web'
> DEBUG: 12+ $url = $url.Replace("{searchTerms}",
> [string]::Join($searchterms, " "))
> DEBUG: ! CALL method 'static System.String Join(String separator,
> String[] value)'
> DEBUG: ! CALL method 'System.String Replace(String oldValue,
> String newValue)'
> DEBUG: ! SET $url = 'http://www.google.com/search?q=
> &rls=com.microsoft:{langu...'.
> DEBUG: 13+ (new-object System.IO.StreamReader
> ([System.Net.WebRequest]::Create([uri]$url)).GetResponse().GetResponseStream()).ReadToEnd()
> DEBUG: ! CALL method 'static System.Net.WebRequest Create(Uri
> requestUri)'
> DEBUG: ! CALL method 'System.Net.WebResponse GetResponse()'
> DEBUG: ! CALL method 'System.IO.Stream GetResponseStream()'
> DEBUG: ! CALL method 'System.String ReadToEnd()'
> .....



This tip came from #powershell irc channel:

You can add items to $args
PS> function Combine-Input { process{ $args += @($input) }end{ $args } }

PS> "zyg","bar"|Combine-Input "foo" "qux"
foo
qux
zyg
bar

Here's the updated Search-Web function

function Search-Web {
begin {
$defaultprovider = (gp "HKCU:\Software\Microsoft\Internet Explorer\SearchScopes").DefaultScope
$url = (gp "HKCU:\Software\Microsoft\Internet Explorer\SearchScopes\$defaultprovider").URL
}
process {
$args += @($input)
}
end {
$url = $url.Replace("{searchTerms}", [string]::Join(" ",$args))
(new-object System.IO.StreamReader ([System.Net.WebRequest]::Create([uri]$url)).GetResponse().GetResponseStream()).ReadToEnd()
}
}

PS> "powershell","Web","search" | search-Web "format" "results"
<html>
....


Now about formattting the results;
i've tried casting the html into xml using [xml] but it trows errors because it's not xml compatible.
Two possible approaches come to mind:
using SgmlReader (http://www.gotdotnet.com/Community/U...4-c3bd760564bc)
or HTML Tidy to convert the html into XHTML then cast tat into xml,
another way would be using regular expressions to parse out the urls and descriptions, Powershell has regexp support using -match operator but this throws us back into text mode, a more object-oriented approach would be more elegant in the powershell environment, perhaps defining a WSDL to provide the relevant information like http://samples.gotdotnet.com/quickst...lscraping.aspx

--
Joris van Lier
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://whizzrd.spaces.live.com

Old 05-07-2007   #8 (permalink)
Mark Allen, Excell Data Corporation
Guest


 

Re: Newbie question: How can I perform web searches from PowerShel

Thanks! I had seen the RSS stuff already, and that kind of sparked my
interest. In fact, I now have a quick hack of a script working using it:

param([string] $searchstring=$(throw "Please specify a search string."))
Write-Host "Searching for $searchstring..."
([xml] (new-object
net.webclient).DownloadString("http://search.live.com/results.aspx?q=" +
$searchstring + "&format=rss")).rss.channel.item | select-object -first 8 |
format-list title, description, link, pubDate

The above will use Live Search's RSS feature to search for your string and
return the first 8 results.

I really appreciate everybodys help, especially those of you in the other
fork on this thread who talk about a more generic solution. That would of
course be ideal. As I learn more I will pursue it further.

Thanks!

Mark


"Don Jones [MVP]" <don@sapien.com> wrote in message
news:CBC2F0C3-F62A-4C95-8968-F93BD37EEEC2@microsoft.com...
> Go to www.sapienpress.com/powershell.asp. Download the book's sample
> scripts. One of them retrieves RSS feeds and outputs them formatted. This
> isn't exactly what you want, but I suspect it'll give you a start.
>
> --
> Don Jones
> Windows PowerShell MVP
> Founder: www.ScriptingAnswers.com
> Co-Author: "Windows PowerShell: TFM"
>
> "Mark Allen" <MarkAllen@discussions.microsoft.com> wrote in message
> news:FA3827B0-6469-4DBE-8DF9-13677AE6A1DC@microsoft.com...
>> Thanks! Good point RE Google's API.
>>
>> As for the results formatting, I'd like to see whatever the search engine
>> gives:
>>
>> Title (from the link)
>> Link
>> Summary/description
>> Date (if shown)
>> Link to cached version
>> Link to similar pages (if that's even there)
>>
>> Then, you could filter on whatever it was you wanted to see.
>>
>> get-websearch -results 20 "Powershell" | format-list Title, Description,
>> Link
>>
>> As I typed what a sample command might look like, I realized the need to
>> specify how many results are returned. There's probably more like that.
>>
>> "Marco Shaw" wrote:
>>
>>> Mark Allen wrote:
>>> > I have a dream. It's a strange dream, but there you go. I would like
>>> > to be
>>> > able to run a script, supply it search terms as parameters, and get
>>> > the
>>> > results back.
>>> >
>>> > I'm sure it's been done, but it is very difficult to search for. Can
>>> > someone
>>> > please post an example?
>>> >
>>> > (Note: I've seen the blog entries about searching Windows Desktop
>>> > Search,
>>> > and that's cool too, but I run Vista and it doesn't work, and ideally
>>> > I'd
>>> > like to search the Internet. I wouldn't mind searching within Vista's
>>> > built-in search as well though.)
>>> >
>>> > Thanks in advance,
>>> >
>>> > Mark
>>>
>>> I'm sure there's a google API that one can tap into. The bigger issue
>>> is how are the results going to be formatted...
>>>
>>> I'd like your comments on the output format you'd like to see...
>>>
>>> Marco
>>>

>


Old 05-07-2007   #9 (permalink)
Don Jones [MVP]
Guest


 

Re: Newbie question: How can I perform web searches from PowerShel

So, the next trick is to not emit text, but to create custom objects and
emit those. Give each object an Open() method, so you could do
$results[0].Open() to have that result open in a new IE window.

--
Don Jones
Windows PowerShell MVP
Founder: www.ScriptingAnswers.com
Co-Author: "Windows PowerShell: TFM"

"Mark Allen, Excell Data Corporation" <a-markal@REDMOND.CORP.MICROSOFT.COM>
wrote in message news:AFB13C97-228F-4657-856F-5B1F3F2B3DB9@microsoft.com...
> Thanks! I had seen the RSS stuff already, and that kind of sparked my
> interest. In fact, I now have a quick hack of a script working using it:
>
> param([string] $searchstring=$(throw "Please specify a search string."))
> Write-Host "Searching for $searchstring..."
> ([xml] (new-object
> net.webclient).DownloadString("http://search.live.com/results.aspx?q=" +
> $searchstring + "&format=rss")).rss.channel.item | select-object -first 8
> | format-list title, description, link, pubDate
>
> The above will use Live Search's RSS feature to search for your string and
> return the first 8 results.
>
> I really appreciate everybodys help, especially those of you in the other
> fork on this thread who talk about a more generic solution. That would of
> course be ideal. As I learn more I will pursue it further.
>
> Thanks!
>
> Mark
>
>
> "Don Jones [MVP]" <don@sapien.com> wrote in message
> news:CBC2F0C3-F62A-4C95-8968-F93BD37EEEC2@microsoft.com...
>> Go to www.sapienpress.com/powershell.asp. Download the book's sample
>> scripts. One of them retrieves RSS feeds and outputs them formatted. This
>> isn't exactly what you want, but I suspect it'll give you a start.
>>
>> --
>> Don Jones
>> Windows PowerShell MVP
>> Founder: www.ScriptingAnswers.com
>> Co-Author: "Windows PowerShell: TFM"
>>
>> "Mark Allen" <MarkAllen@discussions.microsoft.com> wrote in message
>> news:FA3827B0-6469-4DBE-8DF9-13677AE6A1DC@microsoft.com...
>>> Thanks! Good point RE Google's API.
>>>
>>> As for the results formatting, I'd like to see whatever the search
>>> engine
>>> gives:
>>>
>>> Title (from the link)
>>> Link
>>> Summary/description
>>> Date (if shown)
>>> Link to cached version
>>> Link to similar pages (if that's even there)
>>>
>>> Then, you could filter on whatever it was you wanted to see.
>>>
>>> get-websearch -results 20 "Powershell" | format-list Title, Description,
>>> Link
>>>
>>> As I typed what a sample command might look like, I realized the need to
>>> specify how many results are returned. There's probably more like that.
>>>
>>> "Marco Shaw" wrote:
>>>
>>>> Mark Allen wrote:
>>>> > I have a dream. It's a strange dream, but there you go. I would like
>>>> > to be
>>>> > able to run a script, supply it search terms as parameters, and get
>>>> > the
>>>> > results back.
>>>> >
>>>> > I'm sure it's been done, but it is very difficult to search for. Can
>>>> > someone
>>>> > please post an example?
>>>> >
>>>> > (Note: I've seen the blog entries about searching Windows Desktop
>>>> > Search,
>>>> > and that's cool too, but I run Vista and it doesn't work, and ideally
>>>> > I'd
>>>> > like to search the Internet. I wouldn't mind searching within Vista's
>>>> > built-in search as well though.)
>>>> >
>>>> > Thanks in advance,
>>>> >
>>>> > Mark
>>>>
>>>> I'm sure there's a google API that one can tap into. The bigger issue
>>>> is how are the results going to be formatted...
>>>>
>>>> I'd like your comments on the output format you'd like to see...
>>>>
>>>> Marco
>>>>

>>

>


Old 05-07-2007   #10 (permalink)
Don Jones [MVP]
Guest


 

Re: Newbie question: How can I perform web searches from PowerShel

SWEET.

Function Get-SearchResults {
param([string] $searchstring=$(throw "Please specify a search string."))

$client = New-Object System.Net.WebClient

[xml]$results =
$client.DownloadString("http://search.live.com/results.aspx?q=" +
$searchstring + "&format=rss")
$channel = $results.rss.channel

foreach ($item in $channel.item) {
$result = New-Object PSObject
$result | Add-Member NoteProperty Title -value $item.title
$result | Add-Member NoteProperty Link -value $item.link
$result | Add-Member NoteProperty Description -value $item.description
$result | Add-Member NoteProperty PubDate -value $item.pubdate
$sb = {
$ie = New-Object -com internetexplorer.application
$ie.navigate($this.link)
$ie.visible = $true
}
$result | Add-Member ScriptMethod Open -value $sb
$result
}
}

$found = Get-SearchResults "PrimalScript"
$found[0].Open()

--
Don Jones
Windows PowerShell MVP
Founder: www.ScriptingAnswers.com
Co-Author: "Windows PowerShell: TFM"

"Mark Allen, Excell Data Corporation" <a-markal@REDMOND.CORP.MICROSOFT.COM>
wrote in message news:AFB13C97-228F-4657-856F-5B1F3F2B3DB9@microsoft.com...
> Thanks! I had seen the RSS stuff already, and that kind of sparked my
> interest. In fact, I now have a quick hack of a script working using it:
>
> param([string] $searchstring=$(throw "Please specify a search string."))
> Write-Host "Searching for $searchstring..."
> ([xml] (new-object
> net.webclient).DownloadString("http://search.live.com/results.aspx?q=" +
> $searchstring + "&format=rss")).rss.channel.item | select-object -first 8
> | format-list title, description, link, pubDate
>
> The above will use Live Search's RSS feature to search for your string and
> return the first 8 results.
>
> I really appreciate everybodys help, especially those of you in the other
> fork on this thread who talk about a more generic solution. That would of
> course be ideal. As I learn more I will pursue it further.
>
> Thanks!
>
> Mark
>
>
> "Don Jones [MVP]" <don@sapien.com> wrote in message
> news:CBC2F0C3-F62A-4C95-8968-F93BD37EEEC2@microsoft.com...
>> Go to www.sapienpress.com/powershell.asp. Download the book's sample
>> scripts. One of them retrieves RSS feeds and outputs them formatted. This
>> isn't exactly what you want, but I suspect it'll give you a start.
>>
>> --
>> Don Jones
>> Windows PowerShell MVP
>> Founder: www.ScriptingAnswers.com
>> Co-Author: "Windows PowerShell: TFM"
>>
>> "Mark Allen" <MarkAllen@discussions.microsoft.com> wrote in message
>> news:FA3827B0-6469-4DBE-8DF9-13677AE6A1DC@microsoft.com...
>>> Thanks! Good point RE Google's API.
>>>
>>> As for the results formatting, I'd like to see whatever the search
>>> engine
>>> gives:
>>>
>>> Title (from the link)
>>> Link
>>> Summary/description
>>> Date (if shown)
>>> Link to cached version
>>> Link to similar pages (if that's even there)
>>>
>>> Then, you could filter on whatever it was you wanted to see.
>>>
>>> get-websearch -results 20 "Powershell" | format-list Title, Description,
>>> Link
>>>
>>> As I typed what a sample command might look like, I realized the need to
>>> specify how many results are returned. There's probably more like that.
>>>
>>> "Marco Shaw" wrote:
>>>
>>>> Mark Allen wrote:
>>>> > I have a dream. It's a strange dream, but there you go. I would like
>>>> > to be
>>>> > able to run a script, supply it search terms as parameters, and get
>>>> > the
>>>> > results back.
>>>> >
>>>> > I'm sure it's been done, but it is very difficult to search for. Can
>>>> > someone
>>>> > please post an example?
>>>> >
>>>> > (Note: I've seen the blog entries about searching Windows Desktop
>>>> > Search,
>>>> > and that's cool too, but I run Vista and it doesn't work, and ideally
>>>> > I'd
>>>> > like to search the Internet. I wouldn't mind searching within Vista's
>>>> > built-in search as well though.)
>>>> >
>>>> > Thanks in advance,
>>>> >
>>>> > Mark
>>>>
>>>> I'm sure there's a google API that one can tap into. The bigger issue
>>>> is how are the results going to be formatted...
>>>>
>>>> I'd like your comments on the output format you'd like to see...
>>>>
>>>> Marco
>>>>

>>

>


Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: WMI and Powershell Jeff PowerShell 5 10-11-2007 11:24 AM
Newbie Question: Here-Script with PowerShell Raymund Hemmerling PowerShell 6 09-28-2007 03:34 AM
Newbie to Powershell Angelo PowerShell 1 07-04-2007 04:28 PM
Newbie Question - Uses for PowerShell Kim Finleyson PowerShell 11 06-28-2007 07:20 PM
Newbie question - Powershell/Authentication/IP address =?Utf-8?B?Ym1lcnJpMTk=?= PowerShell 3 07-20-2006 02:38 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

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