Skip to content

Commit be025c8

Browse files
committed
Added localhost exceptions for HardCodedComputerName Rule
1 parent 3b3f0ed commit be025c8

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Rules/AvoidUsingComputerNameHardcoded.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
1616
using System.ComponentModel.Composition;
1717
using System.Globalization;
18+
using System.Collections.Generic;
1819

1920
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2021
{
@@ -48,7 +49,12 @@ public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeA
4849

4950
if (String.Equals(cmdParamAst.ParameterName, "computername", StringComparison.OrdinalIgnoreCase))
5051
{
51-
return cmdParamAst.Argument is ConstantExpressionAst || GetComputerNameArg(CmdAst, cmdParamAst.Extent.StartOffset) is ConstantExpressionAst;
52+
Ast computerNameArgument = GetComputerNameArg(CmdAst, cmdParamAst.Extent.StartOffset);
53+
List<string> localhostExceptions = new List<string>{ "localhost", ".", "::1", "127.0.0.1" };
54+
if ((null != computerNameArgument) && (!localhostExceptions.Contains(computerNameArgument.Extent.Text)))
55+
{
56+
return cmdParamAst.Argument is ConstantExpressionAst || computerNameArgument is ConstantExpressionAst;
57+
}
5258
}
5359
}
5460

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
Invoke-Command -ComputerName $comp
2-
Invoke-Command -ComputerName $env:COMPUTERNAME
2+
Invoke-Command -ComputerName $env:COMPUTERNAME
3+
Invoke-Command -ComputerName localhost
4+
Invoke-Command -ComputerName .
5+
Invoke-Command -ComputerName ::1
6+
Invoke-Command -ComputerName 127.0.0.1

0 commit comments

Comments
 (0)