{"id":51243,"date":"2010-02-20T00:01:00","date_gmt":"2010-02-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/02\/20\/hey-scripting-guy-weekend-scripter-conversion-module-part-5\/"},"modified":"2010-02-20T00:01:00","modified_gmt":"2010-02-20T00:01:00","slug":"hey-scripting-guy-weekend-scripter-conversion-module-part-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-conversion-module-part-5\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Conversion Module, Part 5"},"content":{"rendered":"<p><span><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><em><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/em><\/a><\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. What a wonderful day Saturday is. I took the occasion to upgrade from the current beta of Microsoft Office 2010 to the release candidate. I love Microsoft Office 2010, and Outlook in particular has received a very nice upgrade. This week, I was tweeting with Megan (@OrlandoTechNuts on Twitter), and she introduced me to a band called&nbsp;<a href=\"http:\/\/en.wikipedia.org\/wiki\/The_Script_(band)\"><font face=\"Segoe\">The Script<\/font><\/a>. I figured as a Scripting Guy I had to listen to them. They are great, and I have been listening to them ever since. Because Megan is from San Francisco, I&rsquo;m including the following picture, which I took in San Francisco when I was teaching a WMI workshop to a group of Microsoft customers.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of one of San Francisco's most famous buildings\" alt=\"Image of one of San Francisco's most famous buildings\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/february\/hey0220\/wes-02-20-10-01.jpg\" width=\"480\" height=\"640\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">Today&rsquo;s article is part five of the conversion module we began during the <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/02\/06\/hey-scripting-guy-february-6-2010.aspx\">inaugural Weekend Scripter article<\/a>. The <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/02\/07\/hey-scripting-guy-february-7-2010.aspx\">second article<\/a> added additional help information to the functions to better integrate with the <b>Get-Help<\/b> cmdlet. The <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/02\/13\/hey-scripting-guy-february-13-2010.aspx\">third article<\/a> added a function that converts several different types of input into pounds. The <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2010\/02\/14\/hey-scripting-guy-february-14-2010.aspx\">fourth article<\/a> added a function that converts input into liters. <\/p>\n<p class=\"MsoNormal\">In today&rsquo;s article, we add an additional function, but we also change how the data is returned from each of the functions. In all the previous versions of the conversion module, the data that is returned is a string. This means that if you wanted to use the results of the module in a computation, you would need to parse a string. This could be done, of course, but it is a lot of extra work.<\/p>\n<p class=\"MsoNormal\">Because we are already familiar with the object-oriented nature of Windows PowerShell<span>&mdash;<\/span>the cmdlets return objects<span>&mdash;<\/span>and not merely text, it makes sense to restructure our module to also return an object. The new function, <b>ConvertTo-MetersPerSecond<\/b>, is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>ConvertTo-MetersPerSecond<\/p>\n<p><\/strong><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">Function ConvertTo-MetersPerSecond<br \/>{<br \/><span>&nbsp;<\/span>&lt;#<br \/><span>&nbsp; <\/span>.Synopsis<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Converts Miles per Hour into Meters per second.<span>&nbsp; <\/span><br \/><span>&nbsp;&nbsp; <\/span>.Example<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>ConvertTo-MetersPerSecond -MilesPerHour 55<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Converts 55 miles per hour into meters per second<br \/><span>&nbsp;&nbsp; <\/span>.Parameter MilesPerHour<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>The Miles per hour to be converted to Meters per Second<br \/><span>&nbsp;&nbsp; <\/span>.Inputs<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>[double]<br \/><span>&nbsp;&nbsp; <\/span>.Outputs<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>[psobject]<br \/><span>&nbsp;&nbsp; <\/span>.Notes<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>NAME:<span>&nbsp; <\/span>ConvertTo-MetersPerSecond<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>AUTHOR: Ed Wilson<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>AUTHOR BOOK: Windows PowerShell 2.0 Best Practices, Microsoft Press 2010<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>LASTEDIT: 1\/31\/2010<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>KEYWORDS: WeekEnd Scripter, Modules, Getting Started<br \/><span>&nbsp;&nbsp; <\/span>.Link<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Http:\/\/www.ScriptingGuys.com<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Http:\/\/www.bit.ly\/HSGBlog<br \/><span>&nbsp;<\/span>#&gt;<br \/><span>&nbsp;<\/span>#Requires -Version 2.0<br \/>[CmdletBinding()]<br \/><span>&nbsp;<\/span>param(<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[Parameter(Mandatory = $True,valueFromPipeline=$true)]<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[Double]<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$MilesPerHour<br \/>) #end param<\/p>\n<p>if($MilesPerHour)<br \/><span>&nbsp; <\/span>{<br \/><span>&nbsp;&nbsp; <\/span>$outObject = New-Object psobject -property @{<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>value = ($milesPerHour * 0.44704) ;<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>units = &#8220;Meters per sec&#8221;<br \/><span>&nbsp;&nbsp; <\/span>}<br \/><span>&nbsp;&nbsp; <\/span>$outObject<br \/><span>&nbsp; <\/span>}<br \/>} #end function ConvertTo-MetersPerSecond<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">The use of the help tags and parameters have already been discussed in our previous articles. The critical change is the creation of a new <b>psobject<\/b> by using the <b>New-Object<\/b> cmdlet, and adding the properties for that object. The properties of the new object are added via a hash table, <b>@{}<\/b>. A semicolon separates each property. It is then important to return the entire object back to the calling code. This portion of the function is seen here:<\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">$outObject\n = New-Object psobject -property @{<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>value = ($milesPerHour * 0.44704) ;<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>units = &#8220;Meters per sec&#8221;<br \/><span>&nbsp;&nbsp; <\/span>}<br \/><span>&nbsp;&nbsp; <\/span>$outObject<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">After installing the module, you can import the module and use the functions in the following manner:<\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">PS C:&gt; Import-Module conv*<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">PS C:&gt; $a = ConvertTo-MetersPerSecond -MilesPerHour 55<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">PS C:&gt; $a.value<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">24.5872<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">PS C:&gt; $a.units<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">Meters per sec<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">PS C:&gt; &#8220;the result is $($a.value) $($a.units)&#8221;<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">the result is 24.5872 Meters per sec<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">PS C:&gt;<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">Testing the various functions is seen in the following image.<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of testing various functions\" alt=\"Image of testing various functions\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/february\/hey0220\/wes-02-20-10-02.jpg\" width=\"600\" height=\"352\"><\/p>\n<p class=\"Fig-Graphic\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">Rather than overload this blog post with the rapidly lengthening conversion module, I have uploaded the conversion module version 5 to the <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/7b436a7d-68a8-4101-a8ac-d8a5f243100c\"><font face=\"Segoe\">Script Center Repository<\/font><\/a>.<\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <span><a title=\"Facebook\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a><\/span>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/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 class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. What a wonderful day Saturday is. I took the occasion to upgrade from the current beta of Microsoft Office 2010 to the release candidate. I love Microsoft Office 2010, and Outlook in particular has received a very nice upgrade. This week, I was tweeting with Megan (@OrlandoTechNuts on [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,52,3,4,61,45],"class_list":["post-51243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-modules","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. What a wonderful day Saturday is. I took the occasion to upgrade from the current beta of Microsoft Office 2010 to the release candidate. I love Microsoft Office 2010, and Outlook in particular has received a very nice upgrade. This week, I was tweeting with Megan (@OrlandoTechNuts on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51243","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51243"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51243\/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=51243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}