-
-
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(' ','') | |
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 | |
} | |
} |
Cool script.
What about extending it to allow for public transit as one of the travel modes?
Thanks for your valuable suggestions Jeff, I've revised the script accordingly.
DelaneyJM, well lot more to be done on this ;), we can modify the function identify all possible alternatives, which includes Public transit as well
I think I just identified a pull request :)
Line 17 ends with
-ErrorVariable +err
What does the + character do?
'+' 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 :)
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.
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.
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 += "&units=$Units"
$Parameters = @{
Uri = $url
UseBasicParsing = $true
}
$webpage = Invoke-WebRequest @parameters
Great work
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.