diff --git a/Engine/Commands/GetScriptAnalyzerRuleCommand.cs b/Engine/Commands/GetScriptAnalyzerRuleCommand.cs index 21883302e..33ec3f1a6 100644 --- a/Engine/Commands/GetScriptAnalyzerRuleCommand.cs +++ b/Engine/Commands/GetScriptAnalyzerRuleCommand.cs @@ -10,17 +10,14 @@ // THE SOFTWARE. // +using Microsoft.PowerShell.Commands; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System; using System.Collections.Generic; -using System.ComponentModel.Composition; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Management.Automation; -using System.Resources; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Commands { @@ -56,6 +53,21 @@ public string[] Name set { name = value; } } private string[] name; + + /// + /// Severity: Array of the severity types to be enabled. + /// + /// + [ValidateSet("Warning", "Error", "Information", IgnoreCase = true)] + [Parameter(Mandatory = false)] + [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] + public string[] Severity + { + get { return severity; } + set { severity = value; } + } + private string[] severity; + #endregion Parameters #region Private Members @@ -128,9 +140,16 @@ protected override void ProcessRecord() } else { + if (severity != null) + { + var ruleSeverity = severity.Select(item => Enum.Parse(typeof (RuleSeverity), item)); + rules = rules.Where(item => ruleSeverity.Contains(item.GetSeverity())).ToList(); + } + foreach (IRule rule in rules) { - WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(), rule.GetSourceType(), rule.GetSourceName())); + WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(), + rule.GetSourceType(), rule.GetSourceName(), rule.GetSeverity())); } } } diff --git a/Engine/Generic/AvoidCmdletGeneric.cs b/Engine/Generic/AvoidCmdletGeneric.cs index 7946f7895..1ae87fa31 100644 --- a/Engine/Generic/AvoidCmdletGeneric.cs +++ b/Engine/Generic/AvoidCmdletGeneric.cs @@ -93,5 +93,11 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// /// The source type of the rule. public abstract SourceType GetSourceType(); + + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public abstract RuleSeverity GetSeverity(); } } diff --git a/Engine/Generic/AvoidParameterGeneric.cs b/Engine/Generic/AvoidParameterGeneric.cs index 7f6a0bf96..89eb433fc 100644 --- a/Engine/Generic/AvoidParameterGeneric.cs +++ b/Engine/Generic/AvoidParameterGeneric.cs @@ -104,5 +104,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) /// /// The source type of the rule. public abstract SourceType GetSourceType(); + + public abstract RuleSeverity GetSeverity(); } } diff --git a/Engine/Generic/ExternalRule.cs b/Engine/Generic/ExternalRule.cs index 43b849401..934bbc831 100644 --- a/Engine/Generic/ExternalRule.cs +++ b/Engine/Generic/ExternalRule.cs @@ -55,6 +55,12 @@ public SourceType GetSourceType() return SourceType.Module; } + //Set the community rule level as warning as the current implementation does not require user to specify rule severity when defining their functions in PS scripts + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + public string GetSourceName() { return this.srcName; diff --git a/Engine/Generic/IRule.cs b/Engine/Generic/IRule.cs index e4c3f3bc6..e9ddbbf8e 100644 --- a/Engine/Generic/IRule.cs +++ b/Engine/Generic/IRule.cs @@ -10,12 +10,6 @@ // THE SOFTWARE. // -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic { /// @@ -52,5 +46,12 @@ public interface IRule /// /// The source type of the rule. SourceType GetSourceType(); + + /// + /// GetSeverity: Retrieves severity of the rule. + /// + /// + RuleSeverity GetSeverity(); + } } diff --git a/Engine/Generic/RuleInfo.cs b/Engine/Generic/RuleInfo.cs index 1848599e5..b4c6e4369 100644 --- a/Engine/Generic/RuleInfo.cs +++ b/Engine/Generic/RuleInfo.cs @@ -29,6 +29,7 @@ public class RuleInfo private string description; private SourceType sourceType; private string sourceName; + private RuleSeverity ruleSeverity; /// /// Name: The name of the rule. @@ -81,6 +82,16 @@ public string SourceName private set { sourceName = value; } } + /// + /// Severity : The severity of the rule violation. + /// + [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + public RuleSeverity Severity + { + get { return ruleSeverity; } + private set { ruleSeverity = value; } + } + /// /// Constructor for a RuleInfo. /// @@ -89,13 +100,14 @@ public string SourceName /// Description of the rule. /// Source type of the rule. /// Source name of the rule. - public RuleInfo(string name, string commonName, string description, SourceType sourceType, string sourceName) + public RuleInfo(string name, string commonName, string description, SourceType sourceType, string sourceName, RuleSeverity severity) { Name = name; CommonName = commonName; Description = description; SourceType = sourceType; SourceName = sourceName; + Severity = severity; } } } diff --git a/Engine/Generic/RuleSeverity.cs b/Engine/Generic/RuleSeverity.cs new file mode 100644 index 000000000..75496ed43 --- /dev/null +++ b/Engine/Generic/RuleSeverity.cs @@ -0,0 +1,35 @@ +// +// Copyright (c) Microsoft Corporation. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic +{ + /// + /// Represents the severity of a PSScriptAnalyzer rule + /// + public enum RuleSeverity : uint + { + /// + /// Information: This warning is trivial, but may be useful. They are recommended by PowerShell best practice. + /// + Information = 0, + + /// + /// WARNING: This warning may cause a problem or does not follow PowerShell's recommended guidelines. + /// + Warning = 1, + + /// + /// ERROR: This warning is likely to cause a problem or does not follow PowerShell's required guidelines. + /// + Error = 2, + }; +} diff --git a/Engine/ScriptAnalyzer.cs b/Engine/ScriptAnalyzer.cs index 1dacd3d63..5f995abce 100644 --- a/Engine/ScriptAnalyzer.cs +++ b/Engine/ScriptAnalyzer.cs @@ -33,7 +33,6 @@ internal class ScriptAnalyzer #region Private memebers private CompositionContainer container; - private const string baseName = "Microsoft.Windows.Powershell.ScriptAnalyzer.Properties.Strings"; #endregion @@ -91,7 +90,7 @@ public static ScriptAnalyzer Instance public void Initialize() { // Clear external rules for each invoke. - this.ExternalRules = new List(); + ExternalRules = new List(); // Initialize helper Helper.Instance.Initialize(); @@ -129,7 +128,7 @@ public void Initilaize(Dictionary> result) List paths = new List(); // Clear external rules for each invoke. - this.ExternalRules = new List(); + ExternalRules = new List(); // Initialize helper Helper.Instance.Initialize(); diff --git a/Engine/ScriptAnalyzer.format.ps1xml b/Engine/ScriptAnalyzer.format.ps1xml index d13b8f71c..6e9448d18 100644 --- a/Engine/ScriptAnalyzer.format.ps1xml +++ b/Engine/ScriptAnalyzer.format.ps1xml @@ -13,7 +13,7 @@ - 10 + 12 @@ -62,19 +62,19 @@ - 30 - + 35 + - 33 - + 15 + - 45 + 60 - 8 + 10 @@ -86,7 +86,7 @@ Name - CommonName + Severity Description diff --git a/Engine/ScriptAnalyzer.types.ps1xml b/Engine/ScriptAnalyzer.types.ps1xml index 47bf4b417..c6f12268d 100644 --- a/Engine/ScriptAnalyzer.types.ps1xml +++ b/Engine/ScriptAnalyzer.types.ps1xml @@ -42,7 +42,7 @@ DefaultDisplayPropertySet Name - CommonName + Severity Description SourceName diff --git a/Rules/AvoidAlias.cs b/Rules/AvoidAlias.cs index 3d012e782..76c963e5c 100644 --- a/Rules/AvoidAlias.cs +++ b/Rules/AvoidAlias.cs @@ -11,19 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -97,6 +89,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the name of the module/assembly the rule is from. /// diff --git a/Rules/AvoidDefaultTrueValueSwitchParameter.cs b/Rules/AvoidDefaultTrueValueSwitchParameter.cs index 29feba72f..de4a44637 100644 --- a/Rules/AvoidDefaultTrueValueSwitchParameter.cs +++ b/Rules/AvoidDefaultTrueValueSwitchParameter.cs @@ -11,19 +11,12 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -91,6 +84,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidEmptyCatchBlock.cs b/Rules/AvoidEmptyCatchBlock.cs index 34db3c9c4..1c9c57b76 100644 --- a/Rules/AvoidEmptyCatchBlock.cs +++ b/Rules/AvoidEmptyCatchBlock.cs @@ -11,19 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -91,6 +83,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// Method: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidGlobalVars.cs b/Rules/AvoidGlobalVars.cs index b7f983854..9158beb62 100644 --- a/Rules/AvoidGlobalVars.cs +++ b/Rules/AvoidGlobalVars.cs @@ -12,16 +12,10 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -93,6 +87,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// Method: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidInvokingEmptyMembers.cs b/Rules/AvoidInvokingEmptyMembers.cs index 5331b67ca..562f335fd 100644 --- a/Rules/AvoidInvokingEmptyMembers.cs +++ b/Rules/AvoidInvokingEmptyMembers.cs @@ -11,19 +11,12 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -105,6 +98,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the name of the module/assembly the rule is from. /// diff --git a/Rules/AvoidPositionalParameters.cs b/Rules/AvoidPositionalParameters.cs index db39e021f..cc82105ab 100644 --- a/Rules/AvoidPositionalParameters.cs +++ b/Rules/AvoidPositionalParameters.cs @@ -11,20 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; -using Microsoft.Windows.Powershell.ScriptAnalyzer; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -97,6 +88,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// Method: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidReservedCharInCmdlet.cs b/Rules/AvoidReservedCharInCmdlet.cs index 0a1ad4a66..c19c0e980 100644 --- a/Rules/AvoidReservedCharInCmdlet.cs +++ b/Rules/AvoidReservedCharInCmdlet.cs @@ -11,19 +11,13 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -85,6 +79,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidReservedParams.cs b/Rules/AvoidReservedParams.cs index 106a37984..b0211f4f9 100644 --- a/Rules/AvoidReservedParams.cs +++ b/Rules/AvoidReservedParams.cs @@ -13,15 +13,10 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; using System.Management.Automation; using System.Management.Automation.Internal; @@ -106,6 +101,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// Method: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidShouldContinueWithoutForce.cs b/Rules/AvoidShouldContinueWithoutForce.cs index e6e622d56..ca91b62b0 100644 --- a/Rules/AvoidShouldContinueWithoutForce.cs +++ b/Rules/AvoidShouldContinueWithoutForce.cs @@ -11,19 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -119,6 +111,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidTrapStatement.cs b/Rules/AvoidTrapStatement.cs index 5fdd8196a..0dc9b43b1 100644 --- a/Rules/AvoidTrapStatement.cs +++ b/Rules/AvoidTrapStatement.cs @@ -11,19 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -89,6 +81,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidUnitializedVariable.cs b/Rules/AvoidUnitializedVariable.cs index 337a7d955..8c1a0a440 100644 --- a/Rules/AvoidUnitializedVariable.cs +++ b/Rules/AvoidUnitializedVariable.cs @@ -11,20 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; -using Microsoft.Windows.Powershell.ScriptAnalyzer; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -112,6 +103,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// Method: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidUserNameAndPasswordParams.cs b/Rules/AvoidUserNameAndPasswordParams.cs index c9cceef18..a593b1ae7 100644 --- a/Rules/AvoidUserNameAndPasswordParams.cs +++ b/Rules/AvoidUserNameAndPasswordParams.cs @@ -11,18 +11,12 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules @@ -129,6 +123,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Error; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidUsingComputerNameHardcoded.cs b/Rules/AvoidUsingComputerNameHardcoded.cs index 3563e864a..dab94246a 100644 --- a/Rules/AvoidUsingComputerNameHardcoded.cs +++ b/Rules/AvoidUsingComputerNameHardcoded.cs @@ -11,19 +11,10 @@ // using System; -using System.Collections.ObjectModel; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -131,6 +122,15 @@ public override SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public override RuleSeverity GetSeverity() + { + return RuleSeverity.Error; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs b/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs index e7a884b6b..090dcb059 100644 --- a/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs +++ b/Rules/AvoidUsingConvertToSecureStringWithPlainText.cs @@ -11,19 +11,12 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -44,7 +37,7 @@ public override bool CommandCondition(CommandAst CmdAst) { if (CTSTCmdlet == null) { - CTSTCmdlet = Microsoft.Windows.Powershell.ScriptAnalyzer.Helper.Instance.CmdletNameAndAliases("convertto-securestring"); + CTSTCmdlet = Helper.Instance.CmdletNameAndAliases("convertto-securestring"); } return CmdAst != null && CmdAst.GetCommandName() != null && CTSTCmdlet.Contains(CmdAst.GetCommandName(), StringComparer.OrdinalIgnoreCase); @@ -107,6 +100,15 @@ public override SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public override RuleSeverity GetSeverity() + { + return RuleSeverity.Error; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidUsingInternalURLs.cs b/Rules/AvoidUsingInternalURLs.cs index 1a5db9ef3..90d8243cb 100644 --- a/Rules/AvoidUsingInternalURLs.cs +++ b/Rules/AvoidUsingInternalURLs.cs @@ -170,6 +170,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// Method: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidUsingInvokeExpression.cs b/Rules/AvoidUsingInvokeExpression.cs index 99c51f799..81db6dc7a 100644 --- a/Rules/AvoidUsingInvokeExpression.cs +++ b/Rules/AvoidUsingInvokeExpression.cs @@ -11,17 +11,9 @@ // using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -84,6 +76,15 @@ public override SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity:Retrieves the severity of the rule: error, warning of information. + /// + /// + public override RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// Method: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidUsingPlainTextForPassword.cs b/Rules/AvoidUsingPlainTextForPassword.cs index 7933ff47f..a4f6e0d9d 100644 --- a/Rules/AvoidUsingPlainTextForPassword.cs +++ b/Rules/AvoidUsingPlainTextForPassword.cs @@ -11,18 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules @@ -107,6 +100,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidUsingWMIObjectCmdlet.cs b/Rules/AvoidUsingWMIObjectCmdlet.cs index 07d9a7982..6c71f1646 100644 --- a/Rules/AvoidUsingWMIObjectCmdlet.cs +++ b/Rules/AvoidUsingWMIObjectCmdlet.cs @@ -116,6 +116,17 @@ public SourceType GetSourceType() return SourceType.Builtin; } + + /// + /// GetSeverity:Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/AvoidUsingWriteHost.cs b/Rules/AvoidUsingWriteHost.cs index b5126a608..597d7cf4a 100644 --- a/Rules/AvoidUsingWriteHost.cs +++ b/Rules/AvoidUsingWriteHost.cs @@ -11,19 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -105,6 +97,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/MissingModuleManifestField.cs b/Rules/MissingModuleManifestField.cs index 6816878f3..8caa7e3ff 100644 --- a/Rules/MissingModuleManifestField.cs +++ b/Rules/MissingModuleManifestField.cs @@ -12,18 +12,11 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management.Automation.Language; using System.Management.Automation; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; -using System.IO; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -110,6 +103,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// Method: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/PossibleIncorrectComparisonWithNull.cs b/Rules/PossibleIncorrectComparisonWithNull.cs index 149deebe8..507832322 100644 --- a/Rules/PossibleIncorrectComparisonWithNull.cs +++ b/Rules/PossibleIncorrectComparisonWithNull.cs @@ -11,19 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -87,6 +79,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/ProvideCommentHelp.cs b/Rules/ProvideCommentHelp.cs index b6f0e4c02..fbb03b2b9 100644 --- a/Rules/ProvideCommentHelp.cs +++ b/Rules/ProvideCommentHelp.cs @@ -13,15 +13,10 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; using System.Management.Automation; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules @@ -235,6 +230,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Information; + } + /// /// Method: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/ProvideVerboseMessage.cs b/Rules/ProvideVerboseMessage.cs index 5f695966d..a69447c0e 100644 --- a/Rules/ProvideVerboseMessage.cs +++ b/Rules/ProvideVerboseMessage.cs @@ -12,16 +12,10 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -117,6 +111,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Information; + } + /// /// Method: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/ReturnCorrectTypesForDSCFunctions.cs b/Rules/ReturnCorrectTypesForDSCFunctions.cs index 3879e8aea..2890660fb 100644 --- a/Rules/ReturnCorrectTypesForDSCFunctions.cs +++ b/Rules/ReturnCorrectTypesForDSCFunctions.cs @@ -11,20 +11,12 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; -using Microsoft.Windows.Powershell.ScriptAnalyzer; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -217,6 +209,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Information; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/UseApprovedVerbs.cs b/Rules/UseApprovedVerbs.cs index 9df853532..e59d57dee 100644 --- a/Rules/UseApprovedVerbs.cs +++ b/Rules/UseApprovedVerbs.cs @@ -11,18 +11,13 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules @@ -102,6 +97,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/UseCmdletCorrectly.cs b/Rules/UseCmdletCorrectly.cs index f1de7b4cd..a5e854b0b 100644 --- a/Rules/UseCmdletCorrectly.cs +++ b/Rules/UseCmdletCorrectly.cs @@ -11,21 +11,13 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation.Runspaces; using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using Microsoft.Windows.Powershell.ScriptAnalyzer; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -197,6 +189,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/UseDeclaredVarsMoreThanAssignments.cs b/Rules/UseDeclaredVarsMoreThanAssignments.cs index 6cbd7d883..69e3f0bc3 100644 --- a/Rules/UseDeclaredVarsMoreThanAssignments.cs +++ b/Rules/UseDeclaredVarsMoreThanAssignments.cs @@ -11,19 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -95,7 +87,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) assignments.Remove(varKey); } //Check if variable belongs to PowerShell builtin variables - if (Microsoft.Windows.Powershell.ScriptAnalyzer.Helper.Instance.HasSpecialVars(varKey)) + if (Helper.Instance.HasSpecialVars(varKey)) { assignments.Remove(varKey); } @@ -144,6 +136,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/UseIdenticalMandatoryParametersDSC.cs b/Rules/UseIdenticalMandatoryParametersDSC.cs index fb0ac20f4..93309e30d 100644 --- a/Rules/UseIdenticalMandatoryParametersDSC.cs +++ b/Rules/UseIdenticalMandatoryParametersDSC.cs @@ -153,6 +153,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Information; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/UseIdenticalParametersDSC.cs b/Rules/UseIdenticalParametersDSC.cs index 5f69f6c68..ff33d03c7 100644 --- a/Rules/UseIdenticalParametersDSC.cs +++ b/Rules/UseIdenticalParametersDSC.cs @@ -11,19 +11,12 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -167,6 +160,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/UsePSCredentialType.cs b/Rules/UsePSCredentialType.cs index 070910e2d..a932686d4 100644 --- a/Rules/UsePSCredentialType.cs +++ b/Rules/UsePSCredentialType.cs @@ -11,19 +11,12 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -126,6 +119,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/UseShouldProcessCorrectly.cs b/Rules/UseShouldProcessCorrectly.cs index f1cb785e4..de40351cc 100644 --- a/Rules/UseShouldProcessCorrectly.cs +++ b/Rules/UseShouldProcessCorrectly.cs @@ -11,19 +11,11 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -114,6 +106,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/UseShouldProcessForStateChangingFunctions.cs b/Rules/UseShouldProcessForStateChangingFunctions.cs index cca236a4a..28c50aca4 100644 --- a/Rules/UseShouldProcessForStateChangingFunctions.cs +++ b/Rules/UseShouldProcessForStateChangingFunctions.cs @@ -118,6 +118,16 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/UseSingularNouns.cs b/Rules/UseSingularNouns.cs index 162a36437..570cadebb 100644 --- a/Rules/UseSingularNouns.cs +++ b/Rules/UseSingularNouns.cs @@ -11,19 +11,13 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -91,6 +85,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Warning; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Rules/UseStandardDSCFunctionsInResource.cs b/Rules/UseStandardDSCFunctionsInResource.cs index 548eac8fa..60e729cde 100644 --- a/Rules/UseStandardDSCFunctionsInResource.cs +++ b/Rules/UseStandardDSCFunctionsInResource.cs @@ -11,19 +11,12 @@ // using System; -using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Management.Automation; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; -using System.Resources; using System.Globalization; -using System.Threading; -using System.Reflection; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -133,6 +126,15 @@ public SourceType GetSourceType() return SourceType.Builtin; } + /// + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. + /// + /// + public RuleSeverity GetSeverity() + { + return RuleSeverity.Error; + } + /// /// GetSourceName: Retrieves the module/assembly name the rule is from. /// diff --git a/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 b/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 index dbbc97f08..8c32e9b4b 100644 --- a/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 +++ b/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 @@ -108,4 +108,16 @@ Describe "Test RuleExtension" { } } +} + +Describe "TestSeverity" { + It "filters rules based on the specified rule severity" { + $rules = Get-ScriptAnalyzerRule -Severity Error + $rules.Count | Should be 4 + } + + It "filters rules based on multiple severity inputs"{ + $rules = Get-ScriptAnalyzerRule -Severity Error,Information + $rules.Count | Should be 8 + } } \ No newline at end of file