Monday, March 25, 2013

To alias or not to alias.

To alias, or not to alias, or to imply an alias (or Function)

The difference between Help and Get-Help was raised in the discussion portion of this months Saturday PowerShell SIG.  I had always assumed that Help was just an alias for Get-Help, and was happily ignorant.

Help Vs. Get-Help


This isn't really an alias, it is a function that can be seen with:
get-item function:Help | select -expand ScriptBlock

For example a PS3.0 install with the PSCX  module installed gives:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
[CmdletBinding(DefaultParameterSetName='AllUsersView', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113316')]
param(
[Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
[System.String]
${Name},

[System.String]
${Path},

[ValidateSet('Alias','Cmdlet','Provider','General','FAQ','Glossary','HelpFile','ScriptCommand','Function','F
ilter'
,'ExternalScript','All','DefaultHelp','Workflow')]
[System.String[]]
${Category},

[System.String[]]
${Component},

[System.String[]]
${Functionality},

[System.String[]]
${Role},

[Parameter(ParameterSetName='DetailedView', Mandatory=$true)]
[Switch]
${Detailed},

[Parameter(ParameterSetName='AllUsersView')]
[Switch]
${Full},

[Parameter(ParameterSetName='Examples', Mandatory=$true)]
[Switch]
${Examples},

[Parameter(ParameterSetName='Parameters', Mandatory=$true)]
[System.String]
${Parameter},

[Parameter(ParameterSetName='Online', Mandatory=$true)]
[switch]
${Online},

[Parameter(ParameterSetName='ShowWindow', Mandatory=$true)]
[switch]
${ShowWindow}
)

$outputEncoding=[System.Console]::OutputEncoding

if ($Pscx:Preferences["PageHelpUsingLess"])
{
    Get-Help @PSBoundParameters | less
}
else
{
    Get-Help @PSBoundParameters | more
}


Without PSCX in PowerShell 2.0 the same Command generates:
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
<# .FORWARDHELPTARGETNAME Get-Help .FORWARDHELPCATEGORY Cmdlet #>
[CmdletBinding(DefaultParameterSetName='AllUsersView')]
param(
    [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
    [System.String]
    ${Name},

    [System.String]
    ${Path},

    [System.String[]]
    ${Category},

    [System.String[]]
    ${Component},

    [System.String[]]
    ${Functionality},

    [System.String[]]
    ${Role},

    [Parameter(ParameterSetName='DetailedView')]
    [Switch]
    ${Detailed},

    [Parameter(ParameterSetName='AllUsersView')]
    [Switch]
    ${Full},

    [Parameter(ParameterSetName='Examples')]
    [Switch]
    ${Examples},

    [Parameter(ParameterSetName='Parameters')]
    [System.String]
    ${Parameter},

    [Switch]
    ${Online})
$outputEncoding=[System.Console]::OutputEncoding

      Get-Help @PSBoundParameters | more


Help is not an alias, it is a function in both installs.  People treat these as interchangeable in many online references that I have seen.  For most part they are, but they are not the 'same' and this distinction may impact learning.

Alias Ambiguity 

We also talked about where alias are helpful and a hindrance   The general idea being that alias make life easier on you, and potentially more difficult on someone else.  Specifically that someone else would be those reading your script, forum post, or forum answers.  Potentially to someone new to PowerShell this can start with confusion.

For Example:

Get-Alias -Definition Get-ChildItem

CommandType     Name                                               ModuleName                                    
-----------     ----                                               ----------                                    
Alias           dir -> Get-ChildItem                                                                              
Alias           gci -> Get-ChildItem                                                                              
Alias           ls -> Get-ChildItem



If you mix these alias with full cmdlets you can have 4 completely correct answers.  If you are trying to learn the Verb-Noun pairing of PowerShell you could completely miss it. The Verb-Noun pairs are also often referenced as key learning and exploring benefit of PowerShell.

So what could we do to completely overreact to this information?

  1. We could wipe out all of alias:\* and all function:\* that don't contain a hyphen (-).  We would probably not be able to run many other scripts though.  Not sure what this would break actually.  I could only see it as really helping in a high-wire style class room setting.
  2. Run a Function to open a PS1, or ISE and expand aliases.  Something like: http://PoshCode.org/embed/2980 
  3. Improve visibility into what you write and echo an expanded version of the last thing you typed in expanded form.  While at it, expanding any ordered or trimmed argument names would be good too (not show below).
001
002
003
004
005
006
007
008
009
# Generic append to existing Prompt Line
Copy-item Function:Prompt Function:Prompt_Backup

Function Prompt {
Write-host `n
# Expand Alias from http://PoshCode.org/embed/2980
Expand-Alias -script (get-history -count 1 ) | write-host
Function:Prompt_Backup
}

3 comments:

  1. Sering Kalah Bermain Poker atau Permainan Lain?? Butuh Konsultasi Seputar Poker atau Permainan Lain Agar Menang Terus??
    Dapatkan Trik dan Tips Jitu dari Donaco Poker...
    Cukup Daftar menjadi Member saja..!!
    Pendaftaran Gratis!!
    Klik Cara Daftar Poker

    Dapatkan Langsung Tips Jitu Poker

    Nikmati juga kemudahan dalam bertransaksi menggunakan OVO Pay Donaco Poker...

    Dapatkan Juga
    - Bonus Deposit 15% New Member Weekend.
    - Bonus Deposit 10% Next Deposit Weekend.
    - BONUS DEPOSIT HARIAN 5%
    - BONUS ROLLINGAN MINGGUAN 0.5%
    - BONUS KEJUTAN LAINNYA

    Hubungi Kami Secepatnya Di :
    YM : Cs.Donacopoker
    BBM : DC31E2B0
    LINE : Donaco.poker
    WHATSAPP : +6281333555662
    WECHAT : Donacopoker

    ReplyDelete
  2. MENANG BERAPAPUN, PASTI KAMI BAYAR !!! *


    * Melayani LiveChat 7 x 24 Jam Nonstop :

    - WA : 08125522303
    - BBM : CSID303



    Sabung Ayam Online Thailand

    Agen Deposit Sbobet

    www.gorengayam.live

    Situs Poker Online Uang Asli

    ReplyDelete
  3. Ingin deposit tapi rekening sedang bermasalah? Jangan khawatir..kami ada solusinya...
    Deposit via PULSA dan Aplikasi OVO / DANA kini bisa anda nikmati..Proses cepat dan tidak ribet...

    Dapatkan Deposit dengan rate potongan terendah untuk Telkomsel, AXIS dan XL..

    DEPOSIT VIA TELKOMSEL

    DEPOSIT VIA XL

    DEPOSIT VIA AXIS

    Mudah bukan??? Tunggu apa lagi..Segera bergabung dengan kami sekarang juga..

    Daftar Poker Deposit PULSA ==>> Disini Gratiss <<==

    Hubungi Kami Secepatnya Di :
    WHATSAPP : +6281333555662

    ReplyDelete