This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
In this second posting about the MSN Search Web Service from MSH,
in last post
Search with MSNSearch Web API from MSH (Part 1) ,
We got an Application ID,
Generated the DLL to reference in MSH,
and generated A Web search Object from MSH, and did your first Search with it.
(If you do not have the DLL or AppID you cannot run the Script, see Part 1 on how to make them)
We will build upon this by making a function Using the MSN Search Objects,
as said in last post there where 2 thing I would like it to do,
let me go to the Next results and Making the Links Usefull,
To do this I had to ask the user for input, I decided to use the $host.ui.PromptForChoice() Method for this.
this is the Same as used by the CMDlets, so it is constistent and also provides a Help system.
I build it up to take N for Next, Q to Quit and the numbers 0-9 to select the links from the results.
The Choices now look like this :
Choice
Press Number to Start Page, Q to Quit
[0] 0 [1] 1 [2] 2 [3] 3 [4] 4 [5] 5 [6] 6 [7] 7 [8] 8 [9] 9 [N] Next [Q] Quit [?] Help (default is "N"): ?
0 -
1 -
2 -
3 -
4 -
5 -
6 -
7 -
8 -
9 -
N - Next 10
Q - Quit
[0] 0 [1] 1 [2] 2 [3] 3 [4] 4 [5] 5 [6] 6 [7] 7 [8] 8 [9] 9 [N] Next [Q] Quit [?] Help (default is "N"):
after that I check the choices made,
for the Next I needed to get the Next results,
this is very easy by setting the Offset of the SourceRequest, and doing the search again.
then I Needed a way to open a link from the results, I just use the Number returned from the $choice if it was not Next or Quit, to get the URL and use the Shell.Application COM Object to start it.
Also I added a bit of Color to make it easier to read.
with this done, we can do a Paged MSN search from MSH,
just by typing search-MSH and the test to search, you get 10 pages at a time, and you can start them by typeing there number.
the function is still a bit RAW (e.g. It will open explorer in you current folder as you choose a non existing link, and you can't browse back for example) but I think its allready very handy for a Quick Search from MSH.
Enjoy,
gr /\/\o\/\/
Tags : Monad msh# Search-MSH
# MSN Web search Function, using Web API
# /\/\o\/\/ 2006
# http://mow001.blogspot.com
[System.Reflection.Assembly]::LoadFile("g:\mowsh\MSNSearchService.dll")
set-Alias new = new-object
Function Search-MSN {
# Build Choices Menu
$Next = ([System.Management.Automation.Host.ChoiceDescription]"&Next")
$Next.helpmessage = "Next 10"
$Quit = ([System.Management.Automation.Host.ChoiceDescription]"&Quit")
$Quit.helpmessage = "Quit"
$Start = [System.Management.Automation.Host.ChoiceDescription[]]("&0","&1","&2","&3","&4","&5","&6","&7","&8","&9")
$Cl = $start
$cl += ($Next,$Quit)
Function Prompt-Continue ($Caption = "Choice", $Message = "Press Number to Start Page, N or Enter for next page, Q to Quit",$choices = $cl){
$Choice = $host.ui.PromptForChoice($caption,$Message,[System.Management.Automation.Host.ChoiceDescription[]]$choices,10)
return $Choice
}
# build-Up MSN Search Service Object
$MSN = new MSNSearchService
$s = new SourceRequest
$s.source = [sourcetype]::web
$s.ResultFields = ([ResultFieldMask]::All)
$sr = new SearchRequest
$sr.AppID = "YOUR AppID HERE !!!!!!!"
$sr.Requests = $S
$sr.Flags = [SearchFlags]::None
$sr.CultureInfo = "en-US"
# Do Inital Search
$sr.query = "$args"
$r = new sourceResponse
$r = $MSN.Search($sr)
$i = 0 ; $r.responses[0].results | foreach {
write-host -fore yellow "[$i] $($_.Title)"
write-host $_.Description
write-host -fore White $_.Url
write-host
$i += 1
}
# Process Choices till Quit is chosen
$Continue = $true
while ($Continue) {
$Choice = Prompt-Continue
switch -regEx($choice) {
10 {
$S.Offset += 10
$R = $MSN.Search($sr)
$i = 0 ; $r.responses[0].results | foreach {
write-host -fore yellow "[$i] $($_.Title)"
write-host $_.Description
write-host -fore White $_.Url
write-host
$i += 1
}
Break
}
11 {$Continue = $false;break}
[0-9] {(new-object -com shell.application).ShellExecute($r.responses[0].results[($_)].url)}
}
}
}