From 2dc14b3b66e934903cad764c7b9f681ff1c654f3 Mon Sep 17 00:00:00 2001
From: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
Date: Wed, 22 Oct 2025 16:13:43 +0100
Subject: [PATCH 1/8] Refactor character validation to use char.IsAsciiLetter
and char.IsAsciiDigit methods
---
.../commands/management/Computer.cs | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
index 0ea1c91aae6..92f0011de55 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
@@ -2026,17 +2026,16 @@ internal static bool IsComputerNameValid(string computerName)
foreach (char t in computerName)
{
- if (t >= 'A' && t <= 'Z' ||
- t >= 'a' && t <= 'z')
+ if (char.IsAsciiLetter(t))
{
allDigits = false;
continue;
}
- else if (t >= '0' && t <= '9')
+ else if (char.IsAsciiDigit(t))
{
continue;
}
- else if (t == '-')
+ else if (t is '-')
{
allDigits = false;
continue;
From 34c193fb82424fcbbd945c7f42c80f551efc842e Mon Sep 17 00:00:00 2001
From: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
Date: Wed, 22 Oct 2025 16:15:31 +0100
Subject: [PATCH 2/8] Avoid negation for clarity
---
.../commands/management/Computer.cs | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
index 92f0011de55..bdccb9887c2 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
@@ -2019,7 +2019,7 @@ internal static void WriteNonTerminatingError(int errorcode, PSCmdlet cmdlet, st
///
internal static bool IsComputerNameValid(string computerName)
{
- bool allDigits = true;
+ bool hasNonDigit = false;
if (computerName.Length >= 64)
return false;
@@ -2028,7 +2028,7 @@ internal static bool IsComputerNameValid(string computerName)
{
if (char.IsAsciiLetter(t))
{
- allDigits = false;
+ hasNonDigit = true;
continue;
}
else if (char.IsAsciiDigit(t))
@@ -2037,7 +2037,7 @@ internal static bool IsComputerNameValid(string computerName)
}
else if (t is '-')
{
- allDigits = false;
+ hasNonDigit = true;
continue;
}
else
@@ -2046,7 +2046,7 @@ internal static bool IsComputerNameValid(string computerName)
}
}
- return !allDigits;
+ return hasNonDigit;
}
///
From 278f2038506d40e8be5ba9d4991198e4cb476830 Mon Sep 17 00:00:00 2001
From: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
Date: Wed, 22 Oct 2025 16:16:42 +0100
Subject: [PATCH 3/8] Merge conditional
---
.../commands/management/Computer.cs | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
index bdccb9887c2..6fbcbb97ad9 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
@@ -2026,7 +2026,7 @@ internal static bool IsComputerNameValid(string computerName)
foreach (char t in computerName)
{
- if (char.IsAsciiLetter(t))
+ if (t is '-' || char.IsAsciiLetter(t))
{
hasNonDigit = true;
continue;
@@ -2035,11 +2035,6 @@ internal static bool IsComputerNameValid(string computerName)
{
continue;
}
- else if (t is '-')
- {
- hasNonDigit = true;
- continue;
- }
else
{
return false;
From d242b9414d86857eb71622c2b8417b4d0709a465 Mon Sep 17 00:00:00 2001
From: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
Date: Wed, 22 Oct 2025 16:17:45 +0100
Subject: [PATCH 4/8] Simplify branching
---
.../commands/management/Computer.cs | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
index 6fbcbb97ad9..2829bcde8d8 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
@@ -2031,11 +2031,7 @@ internal static bool IsComputerNameValid(string computerName)
hasNonDigit = true;
continue;
}
- else if (char.IsAsciiDigit(t))
- {
- continue;
- }
- else
+ else if (!char.IsAsciiDigit(t))
{
return false;
}
From 66c78ca9229f1c9b8908f6b4984ef4300e29f7ca Mon Sep 17 00:00:00 2001
From: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
Date: Sun, 26 Oct 2025 14:53:04 +0000
Subject: [PATCH 5/8] Check for letter first for performance
---
.../commands/management/Computer.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
index 2829bcde8d8..59a0f8aab89 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
@@ -2026,7 +2026,7 @@ internal static bool IsComputerNameValid(string computerName)
foreach (char t in computerName)
{
- if (t is '-' || char.IsAsciiLetter(t))
+ if (char.IsAsciiLetter(t) || t is '-' )
{
hasNonDigit = true;
continue;
From f59082240e12910ad75c89f848b76ae34837d487 Mon Sep 17 00:00:00 2001
From: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
Date: Mon, 27 Oct 2025 03:48:16 +0000
Subject: [PATCH 6/8] remove redundant continue statement
---
.../commands/management/Computer.cs | 1 -
1 file changed, 1 deletion(-)
diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
index 59a0f8aab89..b2cc4718e0c 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
@@ -2029,7 +2029,6 @@ internal static bool IsComputerNameValid(string computerName)
if (char.IsAsciiLetter(t) || t is '-' )
{
hasNonDigit = true;
- continue;
}
else if (!char.IsAsciiDigit(t))
{
From 72bb77f4dcdfbcfa990b0dc288c1d2a06e2c8f24 Mon Sep 17 00:00:00 2001
From: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
Date: Sun, 2 Nov 2025 02:20:41 +0000
Subject: [PATCH 7/8] Rename hasNonDigit
---
.../commands/management/Computer.cs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
index b2cc4718e0c..7f0df62841c 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
@@ -2019,7 +2019,7 @@ internal static void WriteNonTerminatingError(int errorcode, PSCmdlet cmdlet, st
///
internal static bool IsComputerNameValid(string computerName)
{
- bool hasNonDigit = false;
+ bool hasAsciiLetterOrHyphen = false;
if (computerName.Length >= 64)
return false;
@@ -2028,7 +2028,7 @@ internal static bool IsComputerNameValid(string computerName)
{
if (char.IsAsciiLetter(t) || t is '-' )
{
- hasNonDigit = true;
+ hasAsciiLetterOrHyphen = true;
}
else if (!char.IsAsciiDigit(t))
{
@@ -2036,7 +2036,7 @@ internal static bool IsComputerNameValid(string computerName)
}
}
- return hasNonDigit;
+ return hasAsciiLetterOrHyphen;
}
///
From c94d1ce47aa6dd0df9d0dd1113a081769e80f186 Mon Sep 17 00:00:00 2001
From: xtqqczze <45661989+xtqqczze@users.noreply.github.com>
Date: Thu, 13 Nov 2025 13:40:23 +0000
Subject: [PATCH 8/8] fix extra space
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---
.../commands/management/Computer.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
index 7f0df62841c..ea8c111531c 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Computer.cs
@@ -2026,7 +2026,7 @@ internal static bool IsComputerNameValid(string computerName)
foreach (char t in computerName)
{
- if (char.IsAsciiLetter(t) || t is '-' )
+ if (char.IsAsciiLetter(t) || t is '-')
{
hasAsciiLetterOrHyphen = true;
}