Skip to content

Add internal only support for ScopeItemOption settings in 'AliasAttribute' #3796

Description

@iSazonov

Plan:

Motivation is from #3595 (#3595 (comment))

Dongbo Wang (@daxian-dbw)

My opinion doesn't stem from the performance concern. I just think we should no longer keep stuffing unnecessary settings to InitialSessionState.cs . Keep alias declaration in individual cmdlet is good for at least 2 reasons:
1.alias declaration is together with the cmdlet definition -- easy to read/discover and the alias exists in the cmdlet as a metadata information which can be analyzed using reflection by tools (if there is any tool doing so).
2.we want to stop the pattern of declaring aliases in a centralized location, so that when multiple people working on different cmdlets at the same time, they will have less conflicts that are resulted from editing the same file.

For those existing alias declarations in InitialSessionState.cs , we cannot move them out due to the ScopeItemOption settings (BTW, I think for some of the aliases there, the authors probably just copied the existing options without knowing if they should actually be applied). But for new aliases, I think it's better to have them coupled with the cmdlet unless an option other than 'none' is really desired.

Jason Shirk (@lzybkr)

There are other benefits to the Alias attribute:

  • AllScope is terrible for performance - there is little benefit to have most of these aliases as AllScope
  • Consistency - if a non-default runspace is constructed and imports one of these modules, that runspace would not get the aliases defined by default. For example:
function DoIt($createArg)
{
    [powershell]::Create($createArg).
        AddScript('$ExecutionContext.InvokeCommand.GetCommand("clc", "Alias")').
        Invoke()
}

DoIt ([System.Management.Automation.RunspaceMode]::NewRunspace)
DoIt ([System.Management.Automation.RunspaceMode]::CurrentRunspace)

$iss = [InitialSessionState]::Create()
$iss.ImportPSModule('Microsoft.PowerShell.Management')
$iss.LanguageMode = 'FullLanguage'

DoIt $iss

This prints:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           clc -> Clear-Content
Alias           clc -> Clear-Content

Note - only 2 results instead of 3. This is a little weird because Clear-Content is in Microsoft.PowerShell.Management, so why isn't clc?

We should perhaps consider adding support for AllScope/ReadOnly in the Alias attribute, perhaps as an internal only option so that it isn't abused.

Activity

  1. added
    WG-Enginecore PowerShell engine, interpreter, and runtime
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    Issue-Enhancementthe issue is more of a feature request than a bug
    on May 17, 2017
  2. iSazonov commented on May 17, 2017

    @iSazonov
    CollaboratorAuthor

    Below full list of aliases from InitialSessionState.cs.

    Jason Shirk (@lzybkr) Could you please make a conclusion where we should remove unneeded options?
    (Tomorrow I'll push new tests for the options in #3595 - maybe it is easy to make the conclusion by means of the .ps1 file?)

    Alias Cmdlet Option1 Option2 Conclusion
    "foreach" "ForEach-Object" ReadOnly AllScope
    "%" "ForEach-Object" ReadOnly AllScope
    "where" "Where-Object" ReadOnly AllScope
    "?" "Where-Object" ReadOnly AllScope
    "clc" "Clear-Content" ReadOnly AllScope
    "cli" "Clear-Item" ReadOnly AllScope
    "clp" "Clear-ItemProperty" ReadOnly AllScope
    "clv" "Clear-Variable" ReadOnly AllScope
    "cpi" "Copy-Item" ReadOnly AllScope
    "cvpa" "Convert-Path" ReadOnly AllScope
    "dbp" "Disable-PSBreakpoint" ReadOnly AllScope
    "ebp" "Enable-PSBreakpoint" ReadOnly AllScope
    "epal" "Export-Alias" ReadOnly AllScope
    "epcsv" "Export-Csv" ReadOnly AllScope
    "fl" "Format-List" ReadOnly AllScope
    "ft" "Format-Table" ReadOnly AllScope
    "fw" "Format-Wide" ReadOnly AllScope
    "gal" "Get-Alias" ReadOnly AllScope
    "gbp" "Get-PSBreakpoint" ReadOnly AllScope
    "gc" "Get-Content" ReadOnly AllScope
    "gci" "Get-ChildItem" ReadOnly AllScope
    "gcm" "Get-Command" ReadOnly AllScope
    "gdr" "Get-PSDrive" ReadOnly AllScope
    "gcs" "Get-PSCallStack" ReadOnly AllScope
    "ghy" "Get-History" ReadOnly AllScope
    "gi" "Get-Item" ReadOnly AllScope
    "gl" "Get-Location" ReadOnly AllScope
    "gm" "Get-Member" ReadOnly AllScope
    "gmo" "Get-Module" ReadOnly AllScope
    "gp" "Get-ItemProperty" ReadOnly AllScope
    "gpv" "Get-ItemPropertyValue" ReadOnly AllScope
    "gps" "Get-Process" ReadOnly AllScope
    "group" "Group-Object" ReadOnly AllScope
    "gu" "Get-Unique" ReadOnly AllScope
    "gv" "Get-Variable" ReadOnly AllScope
    "iex" "Invoke-Expression" ReadOnly AllScope
    "ihy" "Invoke-History" ReadOnly AllScope
    "ii" "Invoke-Item" ReadOnly AllScope
    "ipmo" "Import-Module" ReadOnly AllScope
    "ipal" "Import-Alias" ReadOnly AllScope
    "ipcsv" "Import-Csv" ReadOnly AllScope
    "measure" "Measure-Object" ReadOnly AllScope
    "mi" "Move-Item" ReadOnly AllScope
    "mp" "Move-ItemProperty" ReadOnly AllScope
    "nal" "New-Alias" ReadOnly AllScope
    "ndr" "New-PSDrive" ReadOnly AllScope
    "ni" "New-Item" ReadOnly AllScope
    "nv" "New-Variable" ReadOnly AllScope
    "nmo" "New-Module" ReadOnly AllScope
    "oh" "Out-Host" ReadOnly AllScope
    "rbp" "Remove-PSBreakpoint" ReadOnly AllScope
    "rdr" "Remove-PSDrive" ReadOnly AllScope
    "ri" "Remove-Item" ReadOnly AllScope
    "rni" "Rename-Item" ReadOnly AllScope
    "rnp" "Rename-ItemProperty" ReadOnly AllScope
    "rp" "Remove-ItemProperty" ReadOnly AllScope
    "rmo" "Remove-Module" ReadOnly AllScope
    "rv" "Remove-Variable" ReadOnly AllScope
    "rvpa" "Resolve-Path" ReadOnly AllScope
    "sal" "Set-Alias" ReadOnly AllScope
    "sbp" "Set-PSBreakpoint" ReadOnly AllScope
    "sc" "Set-Content" ReadOnly AllScope
    "select" "Select-Object" ReadOnly AllScope
    "si" "Set-Item" ReadOnly AllScope
    "sl" "Set-Location" ReadOnly AllScope
    "sp" "Set-ItemProperty" ReadOnly AllScope
    "saps" "Start-Process" ReadOnly AllScope
    "spps" "Stop-Process" ReadOnly AllScope
    "sv" "Set-Variable" ReadOnly AllScope
    "irm" "Invoke-RestMethod" ReadOnly AllScope
    "iwr" "Invoke-WebRequest" ReadOnly AllScope
    "ac" "Add-Content" ReadOnly AllScope
    "compare" "Compare-Object" ReadOnly AllScope
    "cpp" "Copy-ItemProperty" ReadOnly AllScope
    "diff" "Compare-Object" ReadOnly AllScope
    "gsv" "Get-Service" ReadOnly AllScope
    "sleep" "Start-Sleep" ReadOnly AllScope
    "sort" "Sort-Object" ReadOnly AllScope
    "start" "Start-Process" ReadOnly AllScope
    "sasv" "Start-Service" ReadOnly AllScope
    "spsv" "Stop-Service" ReadOnly AllScope
    "tee" "Tee-Object" ReadOnly AllScope
    "write" "Write-Output" ReadOnly AllScope
    "cat" "Get-Content" AllScope
    "cp" "Copy-Item" AllScope
    "ls" "Get-ChildItem" AllScope
    "man" "help" AllScope
    "mount" "New-PSDrive" AllScope
    "mv" "Move-Item" AllScope
    "ps" "Get-Process" AllScope
    "rm" "Remove-Item" AllScope
    "rmdir" "Remove-Item" AllScope
    "cd" "Set-Location" AllScope
    "dir" "Get-ChildItem" AllScope
    "echo" "Write-Output" AllScope
    "fc" "Format-Custom" ReadOnly AllScope
    "kill" "Stop-Process" AllScope
    "pwd" "Get-Location" AllScope
    "type" "Get-Content" AllScope
    "clear" "Clear-Host" AllScope
    "asnp" "Add-PSSnapIn" ReadOnly AllScope
    "gsnp" "Get-PSSnapIn" ReadOnly AllScope
    "gwmi" "Get-WmiObject" ReadOnly AllScope
    "iwmi" "Invoke-WMIMethod" ReadOnly AllScope
    "ogv" "Out-GridView" ReadOnly AllScope
    "ise" "powershell_ise.exe" ReadOnly AllScope
    "rsnp" "Remove-PSSnapin" ReadOnly AllScope
    "rwmi" "Remove-WMIObject" ReadOnly AllScope
    "swmi" "Set-WMIInstance" ReadOnly AllScope
    "shcm" "Show-Command" ReadOnly AllScope
    "trcm" "Trace-Command" ReadOnly AllScope
    "wget" "Invoke-WebRequest" AllScope
    "curl" "Invoke-WebRequest" AllScope
    "lp" "Out-Printer" AllScope
    "h" "Get-History" AllScope
    "history" "Get-History" AllScope
    "md" "mkdir" AllScope
    "popd" "Pop-Location" AllScope
    "pushd" "Push-Location" AllScope
    "r" "Invoke-History" AllScope
    "cls" "Clear-Host" AllScope
    "chdir" "Set-Location" AllScope
    "copy" "Copy-Item" AllScope
    "del" "Remove-Item" AllScope
    "erase" "Remove-Item" AllScope
    "move" "Move-Item" AllScope
    "rd" "Remove-Item" AllScope
    "ren" "Rename-Item" AllScope
    "set" "Set-Variable" AllScope
    "icm" "Invoke-Command" AllScope
    "clhy" "Clear-History" ReadOnly AllScope
    "gjb" "Get-Job" AllScope
    "rcjb" "Receive-Job" AllScope
    "rjb" "Remove-Job" AllScope
    "sajb" "Start-Job" AllScope
    "spjb" "Stop-Job" AllScope
    "wjb" "Wait-Job" AllScope
    "sujb" "Suspend-Job" AllScope
    "rujb" "Resume-Job" AllScope
    "npssc" "New-PSSessionConfigurationFile" ReadOnly AllScope
    "ipsn" "Import-PSSession" AllScope
    "epsn" "Export-PSSession" AllScope
    "cnsn" "Connect-PSSession" ReadOnly AllScope
    "dnsn" "Disconnect-PSSession" ReadOnly AllScope
    "nsn" "New-PSSession" AllScope
    "gsn" "Get-PSSession" AllScope
    "rsn" "Remove-PSSession" AllScope
    "etsn" "Enter-PSSession" AllScope
    "rcsn" "Receive-PSSession" ReadOnly AllScope
    "exsn" "Exit-PSSession" AllScope
    "sls" "Select-String" None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions