none
PowerShell Variable To Internet Search RRS feed

  • Question

  • I am trying to use a PowerShell variable as an internet (say Google) search term.

    Usually I search around for a couple of hours before asking questions here, but I cannot find anything at all about this.

    Can someone start me in the right direction please?

    Wednesday, October 29, 2014 3:03 AM

Answers

  • we just get the raw html results in Invoke-WebRequest so it is hard to distinguish all the links in the page with the links of the search results.

    This is one approach but not guaranteed to work always.

    PS> ((Invoke-WebRequest ($BING_SEARCH + $TERM)).Links|?{($_.href.contains('http://')) -AND !($_.href.contains('bing'))})[0].href
    http://ja.wikipedia.org/wiki/Windows_PowerShell
    To be more accurate, you need to parse into the HTML to see if we can distinguish the search results.

    Wednesday, October 29, 2014 4:26 AM

All replies

  • Are you looking for something like this?

    $TERM = 'powershell'
    
    $BING_SEARCH ='http://www.bing.com/search?q='
    
    Invoke-WebRequest ($BING_SEARCH + $TERM)
    
    (Invoke-WebRequest ($BING_SEARCH + $TERM)).Links
    

    Wednesday, October 29, 2014 3:37 AM
  • Are you looking for something like this?

    $TERM = 'powershell'
    
    $BING_SEARCH ='http://www.bing.com/search?q='
    
    Invoke-WebRequest ($BING_SEARCH + $TERM)
    
    (Invoke-WebRequest ($BING_SEARCH + $TERM)).Links


    Wow, nice!

    Is there a way to return the first search term only, to get the URL of that first result?
    Wednesday, October 29, 2014 4:06 AM
  • we just get the raw html results in Invoke-WebRequest so it is hard to distinguish all the links in the page with the links of the search results.

    This is one approach but not guaranteed to work always.

    PS> ((Invoke-WebRequest ($BING_SEARCH + $TERM)).Links|?{($_.href.contains('http://')) -AND !($_.href.contains('bing'))})[0].href
    http://ja.wikipedia.org/wiki/Windows_PowerShell
    To be more accurate, you need to parse into the HTML to see if we can distinguish the search results.

    Wednesday, October 29, 2014 4:26 AM
  • There are webservice APIs for Bing and Google that allow targeted searches and return nicely structured results as XML.


    ¯\_(ツ)_/¯

    Wednesday, October 29, 2014 4:59 AM
  • we just get the raw html results in Invoke-WebRequest so it is hard to distinguish all the links in the page with the links of the search results.

    This is one approach but not guaranteed to work always.

    PS> ((Invoke-WebRequest ($BING_SEARCH + $TERM)).Links|?{($_.href.contains('http://')) -AND !($_.href.contains('bing'))})[0].href
    http://ja.wikipedia.org/wiki/Windows_PowerShell
    To be more accurate, you need to parse into the HTML to see if we can distinguish the search results.

    I've tried some code that selects the 'href' item. This grabs the results great, but the first 8 or so are random rubbish but the remaining entries are the http links to the results.
    Is there a way to select the first entry in an array with the text "http://"?

    $TERM = 'test'
    
    $BING_SEARCH ='http://www.bing.com/search?q='
    
    $Result = (Invoke-WebRequest ($BING_SEARCH + $TERM)).Links
    
    $Result | Select-Object -Property href

    Thanks for any help.
    Wednesday, October 29, 2014 7:19 AM
  • we just get the raw html results in Invoke-WebRequest so it is hard to distinguish all the links in the page with the links of the search results.

    This is one approach but not guaranteed to work always.

    PS> ((Invoke-WebRequest ($BING_SEARCH + $TERM)).Links|?{($_.href.contains('http://')) -AND !($_.href.contains('bing'))})[0].href
    http://ja.wikipedia.org/wiki/Windows_PowerShell
    To be more accurate, you need to parse into the HTML to see if we can distinguish the search results.

    I've tried some code that selects the 'href' item. This grabs the results great, but the first 8 or so are random rubbish but the remaining entries are the http links to the results.
    Is there a way to select the first entry in an array with the text "http://"?

    $TERM = 'test'
    
    $BING_SEARCH ='http://www.bing.com/search?q='
    
    $Result = (Invoke-WebRequest ($BING_SEARCH + $TERM)).Links
    
    $Result | Select-Object -Property href

    Thanks for any help.

    I've worked something out, thought I'd share the code I've worked out so far. It's a little rough, but it'll do for now. What I'm trying to do is get a link to WSUS updates via the KB number provided by my PowerShell code for working out what critical updates are available.
    Thank you everyone for your help with this issue.

    $TERM = 'KB3005628'
    
    $BING_SEARCH ='http://www.bing.com/search?q='
    
    $Result = (Invoke-WebRequest ($BING_SEARCH + $TERM)).Links
    
    $HrefResult = $Result | Select-Object -Property href
    
    $FirstHtmlEntry = $HrefResult | Select-String -Pattern "https://" | Select-Object -First 1
    
    $FirstHtmlEntry = $FirstHtmlEntry -Replace "@{href=","" -Replace "}",""
    
    $FirstHtmlEntry

    Wednesday, October 29, 2014 7:36 AM