8
votes

Où sont les alias par défaut définis dans PowerShell?

Ceci peut être une question stupide, mais sont les noix d'alias par défaut (E.G. CD) sont correctes en PowerShell ou définies dans un script "profil" caché quelque part?

Je n'ai pas de profils de profils (par utilisateur ou à l'échelle du système), je me demande simplement où les par défaut proviennent.


0 commentaires

6 Réponses :


1
votes

Bien que je ne connaisse pas les détails techniques, je dirais qu'ils sont codés en papier et ils ne sont pas configurables. Ils peuvent être redéfinis ou supprimés, mais l'ensemble initial n'est pas sous notre contrôle.


0 commentaires

10
votes

Ils sont "intégrés" mais pas immuables. Remarque:

xxx

Comme vous pouvez le constater, il existe une partitionnement des alias par l'option loadonly . Les lisonly sont idiomatiques dans PowerShell, tandis que les personnes mutables sont pour les personnes familières avec d'autres coquilles. J'ai vu des gens modifier dir pour ajouter plus de fonctionnalités, tout en conservant gci comme alias droit sur get-chillitem .

Pour une plus grande compatibilité, je n'utilise que les alias lisonly dans mes scripts.

aussi, car dir dans cmd, < Code> LS dans UNIX et GCI dans PowerShell Chaque fonctionne à sa manière, je m'entraîne pour utiliser la commande natif et non un alias. dir a tendance à fonctionner partout, mais dir -recurse n'est pas!

comme exercice de formation et pour tester mes scripts pour la compatibilité, je supprime parfois le non- lisonly alias: xxx

Il y a une approche plus douce où vous remplacez chaque alias avec une nouvelle commande qui vous avertit que vous utilisez l'un des alias de compatibilité, mais vous permet de continuer à fonctionner.

Aussi, vous pouvez modifier les alias lisonly si vous voulez vraiment, mais pour les raisons ci-dessus que je recommanderais Il:

xxx


3 commentaires

Merci (et d'autres aussi) pour une excellente explication!


En fait, la partie que vous avez qualifiée de # mauvais! est exactement ce que je frappais ma tête contre le mur en essayant de comprendre comment faire - remplacer un alias lisonly. J'avais effectivement essayé -force et -Option AllScope séparément, et bien sûr, ni l'un ni l'autre n'a travaillé. Pour une raison quelconque, cela ne m'a pas rencontré que chacun s'adresse à un obstacle différent, qui s'appliquent tous deux aux alias réadonnés, ils doivent donc être utilisés ensemble ... Duh!


Si vous devez savoir, je voulais alias SL à Push-Location pour que je puisse modifier le comportement sans changer mes habitudes. Il peut également être utile d'avoir une référence d'alias intégrée une commande proxy de la commande qu'elle références normalement. Encore une fois, pour changer le comportement de la commande sans changer vos habitudes. Alors ... ce n'est pas nécessairement toujours # mauvais! . ;)



0
votes

Ils sont codés en couleur.


0 commentaires

7
votes

codé en dur, mais récupérable (comme la plupart des choses "cachées" dans PowerShell) xxx

; -)


1 commentaires

Get-Alias ​​travaille aussi bien sûr, mais le code ci-dessus est définitif, quels que soient les autres modules / matanniques ajoutant leur propre au mélange.



2
votes

Une autre façon de trouver les alias intégrés en utilisant le code source (C #) de Github:

https://github.com/PowerShell/PowerShell/blob/98cf44cdecd72ffc328c3b7ca216fae20a08f1e4/src/System.Management.Automation/engine/InitialSessionState.cs# L4549-L4714 P>

internal static SessionStateAliasEntry[] BuiltInAliases {
    get {
            // Too many AllScope entries hurts performance because an entry is
            // created in each new scope, so we limit the use of AllScope to the
            // most commonly used commands - primarily so command lookup is faster,
            // though if we speed up command lookup significantly, then removing
            // AllScope for all of these aliases makes sense.

            const ScopedItemOptions AllScope = ScopedItemOptions.AllScope;
            const ScopedItemOptions ReadOnly_AllScope = ScopedItemOptions.ReadOnly | ScopedItemOptions.AllScope;
            const ScopedItemOptions ReadOnly = ScopedItemOptions.ReadOnly;

            return new SessionStateAliasEntry[] {
                new SessionStateAliasEntry("foreach", "ForEach-Object", string.Empty, ReadOnly_AllScope),
                new SessionStateAliasEntry("%", "ForEach-Object", string.Empty, ReadOnly_AllScope),
                new SessionStateAliasEntry("where", "Where-Object", string.Empty, ReadOnly_AllScope),
                new SessionStateAliasEntry("?", "Where-Object", string.Empty, ReadOnly_AllScope),
                new SessionStateAliasEntry("clc", "Clear-Content", string.Empty, ReadOnly),
                new SessionStateAliasEntry("cli", "Clear-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("clp", "Clear-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("clv", "Clear-Variable", string.Empty, ReadOnly),
                new SessionStateAliasEntry("cpi", "Copy-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("cvpa", "Convert-Path", string.Empty, ReadOnly),
                new SessionStateAliasEntry("dbp", "Disable-PSBreakpoint", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ebp", "Enable-PSBreakpoint", string.Empty, ReadOnly),
                new SessionStateAliasEntry("epal", "Export-Alias", string.Empty, ReadOnly),
                new SessionStateAliasEntry("epcsv", "Export-Csv", string.Empty, ReadOnly),
                new SessionStateAliasEntry("fl", "Format-List", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ft", "Format-Table", string.Empty, ReadOnly),
                new SessionStateAliasEntry("fw", "Format-Wide", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gal", "Get-Alias", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gbp", "Get-PSBreakpoint", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gc", "Get-Content", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gci", "Get-ChildItem", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gcm", "Get-Command", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gdr", "Get-PSDrive", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gcs", "Get-PSCallStack", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ghy", "Get-History", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gi", "Get-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gl", "Get-Location", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gm", "Get-Member", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gmo", "Get-Module", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gp", "Get-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gpv", "Get-ItemPropertyValue", string.Empty,ReadOnly),
                new SessionStateAliasEntry("gps", "Get-Process", string.Empty, ReadOnly),
                new SessionStateAliasEntry("group", "Group-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gu", "Get-Unique", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gv", "Get-Variable", string.Empty, ReadOnly),
                new SessionStateAliasEntry("iex", "Invoke-Expression", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ihy", "Invoke-History", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ii", "Invoke-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ipmo", "Import-Module", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ipal", "Import-Alias", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ipcsv", "Import-Csv", string.Empty, ReadOnly),
                new SessionStateAliasEntry("measure", "Measure-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("mi", "Move-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("mp", "Move-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("nal", "New-Alias", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ndr", "New-PSDrive", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ni", "New-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("nv", "New-Variable", string.Empty, ReadOnly),
                new SessionStateAliasEntry("nmo", "New-Module", string.Empty, ReadOnly),
                new SessionStateAliasEntry("oh", "Out-Host", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rbp", "Remove-PSBreakpoint", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rdr", "Remove-PSDrive", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ri", "Remove-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rni", "Rename-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rnp", "Rename-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rp", "Remove-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rmo", "Remove-Module", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rv", "Remove-Variable", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rvpa", "Resolve-Path", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sal", "Set-Alias", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sbp", "Set-PSBreakpoint", string.Empty, ReadOnly),
                new SessionStateAliasEntry("select", "Select-Object", string.Empty, ReadOnly_AllScope),
                new SessionStateAliasEntry("si", "Set-Item", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sl", "Set-Location", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sp", "Set-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("saps", "Start-Process", string.Empty, ReadOnly),
                new SessionStateAliasEntry("spps", "Stop-Process", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sv", "Set-Variable", string.Empty, ReadOnly),
                // Web cmdlets aliases
                new SessionStateAliasEntry("irm", "Invoke-RestMethod", string.Empty, ReadOnly),
                new SessionStateAliasEntry("iwr", "Invoke-WebRequest", string.Empty, ReadOnly),
// Porting note: #if !UNIX is used to disable aliases for cmdlets which conflict with Linux / macOS
#if !UNIX
                // ac is a native command on macOS
                new SessionStateAliasEntry("ac", "Add-Content", string.Empty, ReadOnly),
                new SessionStateAliasEntry("compare", "Compare-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("cpp", "Copy-ItemProperty", string.Empty, ReadOnly),
                new SessionStateAliasEntry("diff", "Compare-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("gsv", "Get-Service", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sleep", "Start-Sleep", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sort", "Sort-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("start", "Start-Process", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sasv", "Start-Service", string.Empty, ReadOnly),
                new SessionStateAliasEntry("spsv", "Stop-Service", string.Empty, ReadOnly),
                new SessionStateAliasEntry("tee", "Tee-Object", string.Empty, ReadOnly),
                new SessionStateAliasEntry("write", "Write-Output", string.Empty, ReadOnly),
                // These were transferred from the "transferred from the profile" section
                new SessionStateAliasEntry("cat", "Get-Content"),
                new SessionStateAliasEntry("cp", "Copy-Item", string.Empty, AllScope),
                new SessionStateAliasEntry("ls", "Get-ChildItem"),
                new SessionStateAliasEntry("man", "help"),
                new SessionStateAliasEntry("mount", "New-PSDrive"),
                new SessionStateAliasEntry("mv", "Move-Item"),
                new SessionStateAliasEntry("ps", "Get-Process"),
                new SessionStateAliasEntry("rm", "Remove-Item"),
                new SessionStateAliasEntry("rmdir", "Remove-Item"),
                new SessionStateAliasEntry("cnsn", "Connect-PSSession", string.Empty, ReadOnly),
                new SessionStateAliasEntry("dnsn", "Disconnect-PSSession", string.Empty, ReadOnly),
#endif
                // Bash built-ins we purposefully keep even if they override native commands
                new SessionStateAliasEntry("cd", "Set-Location", string.Empty, AllScope),
                new SessionStateAliasEntry("dir", "Get-ChildItem", string.Empty, AllScope),
                new SessionStateAliasEntry("echo", "Write-Output", string.Empty, AllScope),
                new SessionStateAliasEntry("fc", "Format-Custom", string.Empty, ReadOnly),
                new SessionStateAliasEntry("kill", "Stop-Process"),
                new SessionStateAliasEntry("pwd", "Get-Location"),
                new SessionStateAliasEntry("type", "Get-Content"),
                // Native commands we keep because the functions act correctly on Linux
                new SessionStateAliasEntry("clear", "Clear-Host"),
// #if !CORECLR is used to disable aliases for cmdlets which are not available on OneCore or not appropriate for PSCore6 due to conflicts
#if !CORECLR
                new SessionStateAliasEntry("gwmi", "Get-WmiObject", string.Empty, ReadOnly),
                new SessionStateAliasEntry("iwmi", "Invoke-WMIMethod", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ogv", "Out-GridView", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ise", "powershell_ise.exe", string.Empty, ReadOnly),
                new SessionStateAliasEntry("rwmi", "Remove-WMIObject", string.Empty, ReadOnly),
                new SessionStateAliasEntry("sc", "Set-Content", string.Empty, ReadOnly),
                new SessionStateAliasEntry("swmi", "Set-WMIInstance", string.Empty, ReadOnly),
                new SessionStateAliasEntry("shcm", "Show-Command", string.Empty, ReadOnly),
                new SessionStateAliasEntry("trcm", "Trace-Command", string.Empty, ReadOnly),
                new SessionStateAliasEntry("lp", "Out-Printer"),
#endif
                // Aliases transferred from the profile
                new SessionStateAliasEntry("h", "Get-History"),
                new SessionStateAliasEntry("history", "Get-History"),
                new SessionStateAliasEntry("md", "mkdir", string.Empty, AllScope),
                new SessionStateAliasEntry("popd", "Pop-Location", string.Empty, AllScope),
                new SessionStateAliasEntry("pushd", "Push-Location", string.Empty, AllScope),
                new SessionStateAliasEntry("r", "Invoke-History"),
                new SessionStateAliasEntry("cls", "Clear-Host"),
                new SessionStateAliasEntry("chdir", "Set-Location"),
                new SessionStateAliasEntry("copy", "Copy-Item", string.Empty, AllScope),
                new SessionStateAliasEntry("del", "Remove-Item", string.Empty, AllScope),
                new SessionStateAliasEntry("erase", "Remove-Item"),
                new SessionStateAliasEntry("move", "Move-Item", string.Empty, AllScope),
                new SessionStateAliasEntry("rd", "Remove-Item"),
                new SessionStateAliasEntry("ren", "Rename-Item"),
                new SessionStateAliasEntry("set", "Set-Variable"),
                new SessionStateAliasEntry("icm", "Invoke-Command"),
                new SessionStateAliasEntry("clhy", "Clear-History", string.Empty, ReadOnly),
                // Job Specific aliases
                new SessionStateAliasEntry("gjb", "Get-Job"),
                new SessionStateAliasEntry("rcjb", "Receive-Job"),
                new SessionStateAliasEntry("rjb", "Remove-Job"),
                new SessionStateAliasEntry("sajb", "Start-Job"),
                new SessionStateAliasEntry("spjb", "Stop-Job"),
                new SessionStateAliasEntry("wjb", "Wait-Job"),
#if !CORECLR
                new SessionStateAliasEntry("sujb", "Suspend-Job"),
                new SessionStateAliasEntry("rujb", "Resume-Job"),
                // Remoting Cmdlets Specific aliases
                new SessionStateAliasEntry("npssc", "New-PSSessionConfigurationFile", string.Empty, ReadOnly),
                new SessionStateAliasEntry("ipsn", "Import-PSSession"),
                new SessionStateAliasEntry("epsn", "Export-PSSession"),
#endif
                new SessionStateAliasEntry("nsn", "New-PSSession"),
                new SessionStateAliasEntry("gsn", "Get-PSSession"),
                new SessionStateAliasEntry("rsn", "Remove-PSSession"),
                new SessionStateAliasEntry("etsn", "Enter-PSSession"),
                new SessionStateAliasEntry("rcsn", "Receive-PSSession", string.Empty, ReadOnly),
                new SessionStateAliasEntry("exsn", "Exit-PSSession"),
                // Win8: 121662/169179 Add "sls" alias for Select-String cmdlet
                //   - do not use AllScope - this causes errors in profiles that set this somewhat commonly used alias.
                new SessionStateAliasEntry("sls", "Select-String"),
            };
        }
}


0 commentaires

0
votes

Aussi de note, il y a deux endroits à prendre conscience des alias. Les globaux et le paramètre cmdlet spécifique spécifiques.

# Get named aliases 
Get-Alias | 
Out-GridView -PassThru -Title 'Available aliases'

# Get cmdlet / function parameter aliases
(Get-Command Get-ADUser).Parameters.Values | 
where aliases | 
select Name, Aliases | 
Out-GridView -PassThru -Title 'Alias results for a given cmdlet or function.'


0 commentaires