| external help file | Microsoft.Windows.PowerShell.ScriptAnalyzer.dll-Help.xml |
|---|---|
| Module Name | PSScriptAnalyzer |
| ms.date | 12/12/2024 |
| online version | https://learn.microsoft.com/powershell/module/psscriptanalyzer/get-scriptanalyzerrule?view=ps-modules&wt.mc_id=ps-gethelp |
| schema | 2.0.0 |
Gets the script analyzer rules on the local computer.
Get-ScriptAnalyzerRule [[-Name] <string[]>] [-CustomRulePath <string[]>] [-RecurseCustomRulePath]
[-Severity <string[]>] [<CommonParameters>]
Gets the script analyzer rules on the local computer. You can select rules by Name, Severity, Source, or SourceType, or even particular words in the rule description.
Use this cmdlet to create collections of rules to include and exclude when running the
Invoke-ScriptAnalyzer cmdlet.
To get information about the rules, see the value of the Description property of each rule.
The PSScriptAnalyzer module tests the PowerShell code in a script, module, or DSC resource to determine if it fulfils best practice standards.
Get-ScriptAnalyzerRuleGet-ScriptAnalyzerRule -Severity ErrorThis example runs only the DSC rules with the Error severity on the files in the MyDSCModule module.
$DSCError = Get-ScriptAnalyzerRule -Severity Error | Where-Object SourceName -eq PSDSC
$Path = "$home\Documents\WindowsPowerShell\Modules\MyDSCModule\*"
Invoke-ScriptAnalyzerRule -Path $Path -IncludeRule $DSCError -RecurseUsing the IncludeRule parameter of Invoke-ScriptAnalyzerRule is more efficient than using its
Severity parameter, which is applied only after using all rules to analyze all module files.
This example gets rules with "Parameter" or "Alias" in the name that generate an Error or Warning. You can use this set of rules to test the parameters of your script or module.
$TestParameters = Get-ScriptAnalyzerRule -Severity Error, Warning -Name *Parameter*, *Alias*This example gets the standard rules and the rules in the VeryStrictRules and ExtremelyStrictRules modules. The command uses the RecurseCustomRulePath parameter to get rules defined in subdirectories of the matching paths.
Get-ScriptAnalyzerRule -CustomRulePath $home\Documents\WindowsPowerShell\Modules\*StrictRules -RecurseCustomRulePathBy default, PSScriptAnalyzer gets only the standard rules specified in the
Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll file in the module. Use this
parameter to get the custom Script Analyzer rules in the specified path and the standard Script
Analyzer rules.
Enter the path to a .NET assembly or module that contains Script Analyzer rules. You can enter only one value, but wildcards are supported. To get rules in subdirectories of the path, use the RecurseCustomRulePath parameter.
You can create custom rules using a .NET assembly or a PowerShell module, such as the Community Analyzer Rules in the GitHub repository.
Type: String[]
Parameter Sets: (All)
Aliases: CustomizedRulePath
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: TrueGets only rules with the specified names or name patterns. Wildcards are supported. If you list multiple names or patterns, it gets all rules that match any of the name patterns.
Type: String[]
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: All rules
Accept pipeline input: False
Accept wildcard characters: TrueSearches the CustomRulePath location recursively to add rules defined in files in subdirectories
of the path. By default, Get-ScriptAnalyzerRule adds only the custom rules in the specified path.
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: FalseGets only rules with the specified severity values. Valid values are:
- Information
- Warning
- Error
Type: String[]
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: All rules
Accept pipeline input: False
Accept wildcard characters: FalseThis cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
You can't pipe input to this cmdlet.
The RuleInfo object is a custom object created specifically for Script Analyzer.