{"id":7071,"date":"2007-11-08T08:47:06","date_gmt":"2007-11-08T08:47:06","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2007\/11\/08\/ctp-get-aliassuggestion-rewritten-to-us-ctp-functions\/"},"modified":"2019-02-18T13:16:25","modified_gmt":"2019-02-18T20:16:25","slug":"ctp-get-aliassuggestion-rewritten-to-us-ctp-functions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/ctp-get-aliassuggestion-rewritten-to-us-ctp-functions\/","title":{"rendered":"CTP:  Get-AliasSuggestion Rewritten to us CTP functions"},"content":{"rendered":"<p>I&#8217;m really enjoying the book <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2007\/10\/30\/powershell-cookbook-now-available.aspx\">Windows PowerShell Cookbook<\/a> by Lee Holmes.  It has lots of neat scripts.  I decided to grab one and re-write it to highlight some of the features in the PowerShell V2 CTP.\n<\/p>\n<p>Below is the content for Get-AliasSuggestion.PS1\n\t<\/p>\n<p style=\"margin-left: 36pt\"><span style=\"font-family:Courier New;font-size:10pt\">#requires -Version 2<br \/># Name:  Get-AliasSuggestion<br \/># Author:  Jeffrey Snover <br \/># Derived From:  Lee Holmes<br \/># \u00a0\u00a0\u00a0\u00a0This uses the CTP features of PowerShell V2 which may change<br \/>#\u00a0\u00a0\u00a0\u00a0which would cause this script to change.  <br \/>cmdlet<br \/>param(<br \/># Version 2 (CTP) supports attributes on parameters.<br \/># These attributes cause the engine to do work for us<br \/>    [Mandatory]<br \/>    [Position(0)]<br \/>    [Alias(&#8220;commandline&#8221;,&#8221;Cmd&#8221;,&#8221;Line&#8221;)]<br \/>    $lastCommand<br \/>)<br \/># Version 2 (CTP) supports a DATA language which can be used for<br \/># globalizing your script<br \/>data msgs {<br \/>   # Replace this with your own culture\/string<br \/>   if ($UICulture -eq &#8220;en-US&#8221;)<br \/>   {<br \/>        @{<br \/>            Suggestion = &#8220;Suggestion:  An alias for [{0}] is [{1}]&#8221;<br \/>        }<br \/>   }else<br \/>   {<br \/>        @{<br \/>            Suggestion = &#8220;Suggestion:  An alias for [{0}] is [{1}]&#8221;<br \/>        }<br \/>   }<br \/>}<\/p>\n<p># Version 2 (CTP) supports a tokenize API so we can take an arbitrary string and<br \/># Tokenize it to extract the COMMAND tokens<br \/>$commands = [System.Management.Automation.PSParser]::Tokenize($lastCommand, [ref]$null) | <br \/>    where {$_.Type -eq &#8220;Command&#8221;} | foreach {$_.Content}<br \/>foreach ($alias in Get-Alias)<br \/>{<br \/>    # Version 2 (CTP) built in aliases now include the SNAPIN name for robustness<br \/>    if ($alias.Definition.contains(&#8220;\\&#8221;))<br \/>    {   $definition = @($alias.Definition -split &#8220;\\\\&#8221;)[1]<br \/>    }else<br \/>    {   $definition = $alias.Definition<br \/>    }<br \/>    if ($commands -contains $definition)<br \/>    {   $msgs.Suggestion -f $definition.PadRight(15), $alias.Name.padRight(7)<br \/>    }<br \/>}<br \/>\n\t\t<\/span><\/p>\n<p>\n\u00a0<\/p>\n<p>If you like this one, check out Lee&#8217;s book \u2013 he has tons of these.\n<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows Management Partner Architect<br \/>Visit the Windows PowerShell Team blog at:    <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:  <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a>\n\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m really enjoying the book Windows PowerShell Cookbook by Lee Holmes. It has lots of neat scripts. I decided to grab one and re-write it to highlight some of the features in the PowerShell V2 CTP. Below is the content for Get-AliasSuggestion.PS1 #requires -Version 2# Name: Get-AliasSuggestion# Author: Jeffrey Snover # Derived From: Lee Holmes# [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7071","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>I&#8217;m really enjoying the book Windows PowerShell Cookbook by Lee Holmes. It has lots of neat scripts. I decided to grab one and re-write it to highlight some of the features in the PowerShell V2 CTP. Below is the content for Get-AliasSuggestion.PS1 #requires -Version 2# Name: Get-AliasSuggestion# Author: Jeffrey Snover # Derived From: Lee Holmes# [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/7071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=7071"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/7071\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=7071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=7071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=7071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}