Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Management.Automation;
using System.Management.Automation.Internal;

Expand Down Expand Up @@ -58,6 +59,8 @@ protected override void ProcessRecord()
}
}

ValidateAliasName();

// Create the alias info

AliasInfo newAlias =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Management.Automation;
using System.Management.Automation.Internal;

Expand Down Expand Up @@ -30,6 +31,7 @@ protected override void ProcessRecord()
Option);

aliasToSet.Description = Description;
ValidateAliasName();

string action = AliasCommandStrings.SetAliasAction;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Management.Automation;
using System.Management.Automation.Internal;

namespace Microsoft.PowerShell.Commands
{
Expand Down Expand Up @@ -84,5 +86,19 @@ public SwitchParameter Force

private bool _force;
#endregion Parameters

/// <summary>
/// Validates the alias name is not the same as the value.
/// </summary>
/// <exception cref="ArgumentException">Thrown when the alias name is the same as the value.</exception>
protected internal void ValidateAliasName()
{
if (string.Equals(Name, Value, StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException(
StringUtil.Format(AliasCommandStrings.InvalidAliasName, Name),
nameof(Name));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,7 @@
<data name="NoAliasFound" xml:space="preserve">
<value>This command cannot find a matching alias because an alias with the {0} '{1}' does not exist.</value>
</data>
<data name="InvalidAliasName" xml:space="preserve">
<value>The alias name '{0}' is not valid. Alias names must be unique and cannot be the same as existing commands.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ Describe "New-Alias DRT Unit Tests" -Tags "CI" {
$result.Description | Should -BeExactly "test description"
$result.Options | Should -BeExactly "None"
}

It "New-Alias Should throw an error when the alias name and value are the same" {
{ New-Alias -Name ABCD -Value ABCD -Scope "0" -ErrorAction Stop } |
Should -Throw -ErrorId "System.ArgumentException,Microsoft.PowerShell.Commands.NewAliasCommand"
}
}

Describe "New-Alias" -Tags "CI" {
Expand Down