简体   繁体   中英

PowerShell alias - No restrictions on name

I have written a simple SetDifference function, by using the Compare-Object function:

Function SetDifference {
    param(
    [Parameter(Mandatory=$true)]$ReferenceObject,
    [Parameter(Mandatory=$true)]$DifferenceObject
    )

    Compare-Object $ReferenceObject $DifferenceObject | 
    Where-Object { $_.SideIndicator -eq '<=' } | 
    ForEach-Object { $_.InputObject }
}

I've noticed that ANY alias can be used without PowerShell complaining:

Set-Alias '\' 'SetDifference'
Set-Alias '.' 'SetDifference'
Set-Alias '+' 'SetDifference'

Shouldn't there be restrictions on what the alias name can be - to stop you from using symbols that are already part of PowerShell syntax?

You should get an error, when using ie the '+'.

See here , you should follow this: "You can use any alphanumeric characters in an alias, but the first character cannot be a number."

Also resuing an already defined alias will throw an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM