24

Update, Script is working with PowerShell V3.0, Thanks @ Doug

I want to use the following PowerShell script to get flight status information from Lufthansa. I can see flight status information in the browser, but I haven't found any way to access this information with my script.

I want to get the following information from the website:

  • flight status
  • time (departure, arrival)
  • airport (departure, arrival)
  • Flight Number (Only Lufthansa)

Script:

$flight = " LH3396"
$url = "http://bing.com?q=flight status for $flight"
$result = Invoke-WebRequest $url
$elements = $result.AllElements | Where Class -eq "ans" | Select -First 1 -ExpandProperty innerText 

#[string[]]$resultArray

$resultArray = @()

foreach($element in $elements.Split("`n")){
    if($element.Length -gt "2")
    {$resultArray += $element}
}

2 Answers 2

37

Here is a way to query Bing in PowerShell v3

function Get-FlightStatus {
    param($query)

    $url = "http://bing.com?q=flight status for $query"

    $result = Invoke-WebRequest $url

    $result.AllElements | 
        Where Class -eq "ans" |
        Select -First 1 -ExpandProperty innerText    
}
Get-FlightStatus LH3102

Flight status for Lufthansa 3102

To depart · Jan 30, 2012

From: Hamburg (HAM) 05:35 PM terminal 2

To: Vienna (VIE) 07:05 PM


Bing Travel   Source: www.flightstats.com, 1 minute ago
Sign up to request clarification or add additional context in comments.

4 Comments

Is there an easy way to convert the output to a array?
try Get-FlightStatus LH3102 -split "rn"
in case it doesn't work: if you just use bing.com in Belgium it will change the url to be.bing.com and the flight status will not work (because bing is still in beta in Belgium). To fix this just add ?cc=us to the url: "bing.com?cc=us&q=flight status for $query"
Can you please update the script so I can use and learn from it thanks.
11

You could use the Html Agility Pack.

Here's an article on using it with PowerShell: HTML Agility Pack Rocks Your Screen Scraping World

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.