{"id":3280,"date":"2013-07-04T00:01:00","date_gmt":"2013-07-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/07\/04\/use-powershell-to-translate-airport-code-to-city-name\/"},"modified":"2013-07-04T00:01:00","modified_gmt":"2013-07-04T00:01:00","slug":"use-powershell-to-translate-airport-code-to-city-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-translate-airport-code-to-city-name\/","title":{"rendered":"Use PowerShell to Translate Airport Code to City Name"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and a web service to translate an airport code to a city name.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One thing that I have never really understood is the relationships between airport codes and the airport name. Now, some of them make sense. CLT for Charlotte is not too much of a stretch. LGA for Laguardia in New York also makes sense. But MSY, BNA, and others seem to be completely random.<\/p>\n<p>However, before I get too carried away and try to use <strong>Get-Random<\/strong> to do the translation, I thought I would conduct a Bing search and see if I could find something a bit more helpful. I came up with a free web service. I happen to love web services&mdash;especially since the <strong>New-WebServiceProxy<\/strong> cmdlet made its way into Windows PowerShell&nbsp;2.0. In fact, over the past several years, I have written about <a href=\"http:\/\/social.technet.microsoft.com\/Search\/en-US?query=new-webserviceproxy&amp;beta=0&amp;rn=Hey%2c+Scripting+Guy!+Blog&amp;rq=site:blogs.technet.com\/b\/heyscriptingguy\/&amp;ac=3\" target=\"_blank\">New-WebServiceProxy<\/a> 15 times. Check out these posts for more excellent examples of how to use this easy-to-use cmdlet.<\/p>\n<p>The airport web service exposes a number of methods. One of the cool things about many web services, is that when I hit the service page, it automatically exposes the web service schema as shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4478.hsg-7-4-13-01.png\"><img decoding=\"async\" title=\"Image of script\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4478.hsg-7-4-13-01.png\" alt=\"Image of script\" \/><\/a><\/p>\n<h3>Creating the script<\/h3>\n<p>The script to connect to the airport web service is really simple. The first thing I do is create a variable to hold the WSDL for the web service. This is the entry point for using the web service, and I obtained it from a directory of web services on the Internet. There are many such directories of free web services available. The one I used is <a href=\"http:\/\/www.webservicex.net\/WS\/WSDetails.aspx?WSID=20&amp;CATID=7\" target=\"_blank\">Airport Information Webservice<\/a>. The web page for the service mentions the WSDL schema location, and all I did was copy it and store it in a variable:<\/p>\n<p style=\"padding-left: 30px\">$wsdl = &#8216;http:\/\/www.webservicex.net\/airport.asmx?WSDL&#8217;<\/p>\n<p>Next, I use the <strong>New-WebServiceProxy<\/strong> cmdlet and feed it the URL that is stored in the <strong>$wsdl<\/strong> variable. The <strong>Namespace<\/strong> parameter and the <strong>Class<\/strong> parameter values are not used in this script, but I need to give them something. I often use the values from script to script. I store the returned proxy in the <strong>$Proxy<\/strong> variable. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">$proxy = New-WebServiceProxy -Uri $wsdl -Namespace proxy -Class ip<\/p>\n<p><span style=\"font-size: 12px\">Now, I call the <\/span><strong style=\"font-size: 12px\">GetAirportInformationByAirportCode<\/strong><span style=\"font-size: 12px\"> method. Yeah, I know it is a long method name, but if I have run the script prior to getting to this point, the Tab expansion will pick up the method name, and therefore, I avoid typing it. In addition, by populating the <\/span><strong style=\"font-size: 12px\">$proxy<\/strong><span style=\"font-size: 12px\"> variable, I know if I have messed anything up, and the script is easier to troubleshoot.<\/span><\/p>\n<p>I cast the returned information into an XML document to make it easier to navigate the information I want to obtain. I have hardcoded the CLT airport into the script, but you would want to change that by including a parameter (or even turning this into a function) and creating a module to expose all of the interesting methods. Here is the line of script:<\/p>\n<p style=\"padding-left: 30px\">[xml]$rtn = $proxy.getAirportInformationByAirportCode(&#8220;CLT&#8221;)<\/p>\n<p>Now to display the information. By picking up the <strong>NewDataSet.Table<\/strong> property, I show all of the information. I found this by playing around with the output. One thing that might make sense is to combine several properties and display the Lat\/Long as a single entity&mdash;or maybe not. Who knows, it is up to you. Here is my line of script:<\/p>\n<p style=\"padding-left: 30px\">$rtn.NewDataSet.Table<\/p>\n<p>That is it. Four lines of script.<\/p>\n<h3>The complete script<\/h3>\n<p>The complete script is only four lines long, and it could be written in a single line if one were so inclined. Here is the script.<\/p>\n<p style=\"padding-left: 30px\">$wsdl = &#8216;http:\/\/www.webservicex.net\/airport.asmx?WSDL&#8217;<\/p>\n<p style=\"padding-left: 30px\">$proxy = New-WebServiceProxy -Uri $wsdl -Namespace proxy -Class ip<\/p>\n<p style=\"padding-left: 30px\">[xml]$rtn = $proxy.getAirportInformationByAirportCode(&#8220;CLT&#8221;)<\/p>\n<p style=\"padding-left: 30px\">$rtn.NewDataSet.Table<\/p>\n<p>When the script runs, it returns the data in a Table property. The output is shown here in the Windows PowerShell ISE output pane:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6215.hsg-7-4-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6215.hsg-7-4-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using Windows PowerShell and a web service to translate an airport code to the city name. Join me tomorrow when I will have more Windows PowerShell goodness.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and a web service to translate an airport code to a city name. Microsoft Scripting Guy, Ed Wilson, is here. One thing that I have never really understood is the relationships between airport codes and the airport name. Now, some of them make sense. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,167,413,45],"class_list":["post-3280","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-using-the-internet","tag-web-service","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and a web service to translate an airport code to a city name. Microsoft Scripting Guy, Ed Wilson, is here. One thing that I have never really understood is the relationships between airport codes and the airport name. Now, some of them make sense. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3280"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3280\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}