Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Last active January 17, 2016 13:37
Show Gist options
  • Save PrateekKumarSingh/3c04b3169d513b3ad887 to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/3c04b3169d513b3ad887 to your computer and use it in GitHub Desktop.
Function Get-Direction()
{
Param(
[Parameter(Mandatory=$true,Position=0)] $Origin,
[Parameter(Mandatory=$true,Position=1)] $Destination,
[Parameter(Position=2)] [ValidateSet('driving','bicycling','walking')] $Mode ="driving",
[Switch] $InMiles
)
$Units='metric' # Default set to Kilometers
# If Switch is selected, use 'Miles' as the Unit
If($InMiles){$Units = 'imperial'}
#Requesting Web Page
$webpage = Invoke-WebRequest "https://maps.googleapis.com/maps/api/directions/xml?origin=$Origin&destination=$Destination&mode=$($mode.toLower())&units=$Units&key=AIzaSyAjkqgmCw15XhKat2Z-cIGZ-rzmE0xXHjc" -UseBasicParsing -ErrorVariable +err
# Capturing the HTML output
$content = $webpage.Content
#To Clear unwanted data from the String
Function Clean-String($Str)
{
$str = $Str.replace('<div style="font-size:0.9em">','')
$str = $str.replace('</div>','')
$str = $str.replace('<b>','')
$str = $str.replace('</b>','')
$str = $str.replace('&nbsp;','')
Return $str
}
# Data Mining information from the XML content
$status = (Select-Xml -Content $content -xpath '//status').Node.InnerText
If($status -eq 'OK')
{
$Mode = (Select-Xml -Content $content -xpath '//route/leg/step/travel_mode').Node.InnerText
$Duration = (Select-Xml -Content $content -xpath '//route/leg/step/duration/text').Node.InnerText
$Distance = (Select-Xml -Content $content -xpath '//route/leg/step/distance/text').Node.InnerText
$Instructions = (Select-Xml -Content $content -xpath '//route/leg/step/html_instructions').Node.InnerText | %{ Clean-String $_}
$Object = @()
for($i=0;$i -le $instructions.count;$i++)
{
$Object += New-Object psobject -Property @{TravelMode=$Mode[$i];Duration=$Duration[$i];Distance= $Distance[$i];"Instructions"= $Instructions[$i]}
}
Return $Object
}
else
{
# In case the no data is recived due to incorrect parameters
Write-Host "Zero Results Found : Try changing the parameters" -fore Yellow
}
}
@jdhitsolutions
Copy link

A recommended best practice is to not include any formatting directives in your function. It should simple write an object to the pipeline. Instead you would run:
Get-Direction | format-table

Or learn how to use PowerShell's extensible type system to add a custom type to your object. Then you can create a custom format extension so that PowerShell will always display the result as a table that you design. Otherwise, this is pretty slick.

@jdhitsolutions
Copy link

Oh, and why not make Driving the default value for Mode?

[Parameter(Position=2)] [ValidateSet('driving','bicycling','walking')] $Mode = "driving",

Then you don't need the If statement and help will display the default value.

@DelaneyJM
Copy link

Cool script.

What about extending it to allow for public transit as one of the travel modes?

@PrateekKumarSingh
Copy link
Author

Thanks for your valuable suggestions Jeff, I've revised the script accordingly.

@PrateekKumarSingh
Copy link
Author

DelaneyJM, well lot more to be done on this ;), we can modify the function identify all possible alternatives, which includes Public transit as well

@DelaneyJM
Copy link

I think I just identified a pull request :)

@LarryWeiss
Copy link

Line 17 ends with
-ErrorVariable +err
What does the + character do?

@PrateekKumarSingh
Copy link
Author

'+' is used to add the next error to the same error variable, making it a type of Error Object Array.
TRY THIS -

Invoke-WebRequest "badurl.com" -ErrorVariable +err
$err

then again the same commands and see how error messages are added to same array :)

@LarryWeiss
Copy link

Thanks. It seems I learn new things about PowerShell every day for the past 6 years.

Is the use of -ErrorVariable left in the function for debugging purposes? The value of $err is not used by the function itself.

@LarryWeiss
Copy link

Also, what is the text that flashes on screen when I run the function?
It disappears so fast that I can't read it. It is a side-effect of the Invoke-WebRequest call.

@LarryWeiss
Copy link

The &key= clause shown below
$webpage = Invoke-WebRequest "https://maps.googleapis.com/maps/api/directions/xml?origin=$Origin&destination=$Destination&mode=$($mode.toLower())&units=$Units&key=AIzaSyAjkqgmCw15XhKat2Z-cIGZ-rzmE0xXHjc" -UseBasicParsing -ErrorVariable +err
can be omitted.
I have reformatted that logic to become:
$url = 'https://maps.googleapis.com/maps/api/directions/xml?'
$url += "origin=$Origin"
$url += "&destination=$Destination"
$url += "&amp;mode=$($mode.toLower())"
$url += "&units=$Units"
$Parameters = @{
Uri = $url
UseBasicParsing = $true
}
$webpage = Invoke-WebRequest @parameters

@g60wall
Copy link

g60wall commented Jan 9, 2016

Great work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment