CTP: Get-AliasSuggestion Rewritten to us CTP functions

CTP: Get-AliasSuggestion Rewritten to us CTP functions

  • Comments 1

I'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
#     This uses the CTP features of PowerShell V2 which may change
#    which would cause this script to change.
cmdlet
param(
# Version 2 (CTP) supports attributes on parameters.
# These attributes cause the engine to do work for us
[Mandatory]
[Position(0)]
[Alias("commandline","Cmd","Line")]
$lastCommand
)
# Version 2 (CTP) supports a DATA language which can be used for
# globalizing your script
data msgs {
# Replace this with your own culture/string
if ($UICulture -eq "en-US")
{
@{
Suggestion = "Suggestion: An alias for [{0}] is [{1}]"
}
}else
{
@{
Suggestion = "Suggestion: An alias for [{0}] is [{1}]"
}
}
}

# Version 2 (CTP) supports a tokenize API so we can take an arbitrary string and
# Tokenize it to extract the COMMAND tokens
$commands = [System.Management.Automation.PSParser]::Tokenize($lastCommand, [ref]$null) |
where {$_.Type -eq "Command"} | foreach {$_.Content}
foreach ($alias in Get-Alias)
{
# Version 2 (CTP) built in aliases now include the SNAPIN name for robustness
if ($alias.Definition.contains("\"))
{ $definition = @($alias.Definition -split "\\")[1]
}else
{ $definition = $alias.Definition
}
if ($commands -contains $definition)
{ $msgs.Suggestion -f $definition.PadRight(15), $alias.Name.padRight(7)
}
}

 

If you like this one, check out Lee's book – he has tons of these.

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 4 and 7 and type the answer here:
  • Post