{"id":4149,"date":"2013-02-18T00:01:00","date_gmt":"2013-02-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/02\/18\/use-powershell-and-a-web-service-to-find-holidays\/"},"modified":"2013-02-18T00:01:00","modified_gmt":"2013-02-18T00:01:00","slug":"use-powershell-and-a-web-service-to-find-holidays","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-and-a-web-service-to-find-holidays\/","title":{"rendered":"Use PowerShell and a Web Service to Find Holidays"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary:<\/strong><span style=\"font-size: 12px\"> Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to find the date for a specific holiday by using a web service.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! &nbsp;I have a problem. I guess you hear that a lot. Anyway, my boss has given me what I think is an impossible task&mdash;to find the holidays we have coming up this year. Not just a list of the standard holidays, but the actual days they will be celebrated. I know there is a holiday thing I can turn on in Microsoft Outlook, and I thought about trying to write a script that would find holidays marked in my Outlook Calendar, but I suspect that my boss has other ideas. You see, he never tells me what is on his mind. It is always just a series of seemingly random requests. I really wish he would clue me in on what goes on in his fuzzy-headed brain, but that is a different story&mdash;so I have to try and anticipate him and that is not easy. Anyway, I suspect, eventually he wants a script that will be able to do something with holiday dates&mdash;got any clues, ideas, or suggestions, oh, scripting wizard?<\/p>\n<p>&mdash;GG<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello GG,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning seems cold. A quick look outside reveals frost on the ground. In Charlotte, North Carolina, in the southern portion of the United States, having frost on the ground is not a normal event. In fact, I would not be surprised if they called off school. But as the old song goes, the weather outside is frightful, but the fire inside is delightful. Yes, I turned on the fire when I came down and saw the glittering grass in the early morning sunlight. I also decided today would be a good <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2009\/03\/04\/how-do-i-migrate-my-vbscript-wmi-queries-to-windows-powershell.aspx\" target=\"_blank\">coffee day<\/a>. The Scripting Wife had a nice bag of Kona coffee shipped from the island of Kauai, Hawaii, for me, and today seems like a nice day to open it. When I make coffee, I grind fresh beans, use fresh spring water, and I employ my French press. I usually use turbinado raw sugar, but today I am using crushed rock sugar I brought back from Munich&mdash;it is actually a nice addition. For me, I want to taste the coffee, not cheap flavoring. Nor do I want to drink a glass of warm milk with a shot of coffee in it.<\/p>\n<h2>Use a web service to obtain holiday dates<\/h2>\n<p>GG, there may be a completely international web service that returns holiday information, but I was unable to find one doing a few Bing searches. I was able to find a web service that contains holiday information for a few countries. I found the <a href=\"http:\/\/www.holidaywebservice.com\/HolidayService_v2\/HolidayService2.asmx\" target=\"_blank\">HolidayService2<\/a> web service. The service defines six methods. Opening the URL in Internet Explorer shows the six methods.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5518.HSG-2-18-13-01.png\"><img decoding=\"async\" title=\"Image of HolidayService2 web service\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5518.HSG-2-18-13-01.png\" alt=\"Image of HolidayService2 web service\" \/><\/a><\/p>\n<p>To use a method from a web service, I use the <strong>New-WebServiceProxy<\/strong> cmdlet, and I store the returned object in a variable. When using the <strong>New-WebServiceProxy<\/strong> cmdlet, the most important parameter is the URI for the web service. For this example, the <strong>class<\/strong><em> <\/em>and the <strong>holiday<\/strong><em> <\/em>parameters do not matter. To make things more readable, I always store the URI for the web service in a separate variable. I end up with two lines of code, as shown here.<\/p>\n<p style=\"padding-left: 30px\">$URI = &#8220;http:\/\/www.holidaywebservice.com\/HolidayService_v2\/HolidayService2.asmx&#8221;<\/p>\n<p style=\"padding-left: 30px\">$proxy = New-WebServiceProxy -Uri $URI -Class holiday -Namespace webservice<\/p>\n<h2>Calling a web service method<\/h2>\n<p>All I need to do now is figure out how to use the web service. This particular web service is really nice because it is based on the .NET Framework, and it returns .NET objects&mdash;I will get to that in a minute. First, I need to know what countries or regions are supported by the web service. To do this, I call the <strong>GetCountriesAvailable<\/strong> method.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;Keep in mind this web service is case-sensitive. Therefore, UnitedStates must be cased as shown (I cannot do everything in lowercase as I often do), as it is required for the other countries or regions and holidays.<\/p>\n<p>To call <strong>GetCountriesAvailable<\/strong>, I begin typing and use the tab expansion to complete the method name. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">$proxy.GetCountriesAvailable()<\/p>\n<p>The command and associated output appear in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0181.HSG-2-18-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0181.HSG-2-18-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>OK, so I know what countries or regions the service supports. Now I need to know what holidays the service supports. To do this, I call the <strong>GetHolidaysAvailable<\/strong> method, as shown here.<\/p>\n<p style=\"padding-left: 30px\">$proxy.GetHolidaysAvailable(&#8220;UnitedStates&#8221;)<\/p>\n<p>The neat thing about the service returning objects is that I can pipe the returned objects to other Windows PowerShell cmdlets. For example, looking through a random listing of holidays may not be too illuminating, but if I sort them alphabetically, it makes things easier to see. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">$proxy.GetHolidaysAvailable(&#8220;UnitedStates&#8221;) | sort code<\/p>\n<p>The command and associated output appear here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0880.HSG-2-18-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0880.HSG-2-18-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Hey, want to know the difference between holidays celebrated in Northern Ireland and the holidays celebrated in the Republic of Ireland? I call the <strong>GetHolidaysAvailable<\/strong> method twice (once for each region), store the results in variables, and pass the resultant objects to the <strong>Compare-Object<\/strong> cmdlet. Piece of cake. The three commands are shown here.<\/p>\n<p style=\"padding-left: 30px\">$in = $proxy.GetHolidaysAvailable(&#8220;IrelandNorthern&#8221;) | sort code<\/p>\n<p style=\"padding-left: 30px\">$ir = $proxy.GetHolidaysAvailable(&#8220;IrelandRepublicOf&#8221;) | sort code<\/p>\n<p style=\"padding-left: 30px\">Compare-Object -Property code -ReferenceObject $in -DifferenceObject $ir<\/p>\n<p>The commands and the associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2630.HSG-2-18-13-04.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2630.HSG-2-18-13-04.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Get the holiday date<\/h2>\n<p>Now, I can figure it out. It might take a bit of time. A few of my attempts are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4073.HSG-2-18-13-05.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4073.HSG-2-18-13-05.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Rather than cluttering up my Windows PowerShell console with red, how about if I go back to the HolidayService2 web page and look at the parameters for the <strong>GetHolidayDate<\/strong> method. In fact, there is even a little form I can use to see if what I am thinking about using will actually work. The web page with the form and the SOAP definition are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0383.HSG-2-18-13-06.png\"><img decoding=\"async\" title=\"Image of HolidayService2 web service\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0383.HSG-2-18-13-06.png\" alt=\"Image of HolidayService2 web service\" \/><\/a><\/p>\n<p>So, the correct way to call the <strong>GetHolidayDate<\/strong> method appears here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $proxy.GetHolidayDate(&#8220;UnitedStates&#8221;,&#8221;LABOR-DAY&#8221;,2013)<\/p>\n<p style=\"padding-left: 30px\">Monday, September 2, 2013 12:00:00 AM<\/p>\n<p>Notice that the returned date looks amazingly like the good old-fashioned <strong>System.DateTime<\/strong> object we PowerShellers know and love. Once again, because this web service returns .NET objects, I can do some really cool Windows PowerShell stuff. For example, if I want to know how long it is until Labor Day in the United States, I can use the <strong>New-TimeSpan<\/strong> cmdlet, as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; New-TimeSpan -Start (get-date) -End ($proxy.GetHolidayDate(&#8220;UnitedStates&#8221;,&#8221;LA<\/p>\n<p style=\"padding-left: 30px\">BOR-DAY&#8221;,2013))<\/p>\n<p style=\"padding-left: 30px\">Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 196<\/p>\n<p style=\"padding-left: 30px\">Hours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 11<\/p>\n<p style=\"padding-left: 30px\">Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 19<\/p>\n<p style=\"padding-left: 30px\">Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 45<\/p>\n<p style=\"padding-left: 30px\">Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 909<\/p>\n<p style=\"padding-left: 30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 171479859090373<\/p>\n<p style=\"padding-left: 30px\">TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 198.472059132376<\/p>\n<p style=\"padding-left: 30px\">TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 4763.32941917703<\/p>\n<p style=\"padding-left: 30px\">TotalMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 285799.765150622<\/p>\n<p style=\"padding-left: 30px\">TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 17147985.9090373<\/p>\n<p style=\"padding-left: 30px\">TotalMilliseconds : 17147985909.0373<\/p>\n<p>GG, that is all there is to using a web service to work with holidays. It should be much easier for you than attempting to parse your Outlook calendar. Join me tomorrow when I will talk about a cool function I wrote to determine if a command exists or not in Windows PowerShell.<\/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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to find the date for a specific holiday by using a web service. &nbsp;Hey, Scripting Guy! &nbsp;I have a problem. I guess you hear that a lot. Anyway, my boss has given me what I think is an impossible task&mdash;to find the holidays [&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-4149","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, shows how to use Windows PowerShell to find the date for a specific holiday by using a web service. &nbsp;Hey, Scripting Guy! &nbsp;I have a problem. I guess you hear that a lot. Anyway, my boss has given me what I think is an impossible task&mdash;to find the holidays [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4149","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=4149"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4149\/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=4149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}