| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ae712f7 commit 28081e5
22 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | --- | |
| 2 | 2 | external help file: Microsoft.Windows.PowerShell.ScriptAnalyzer.dll-Help.xml | |
| 3 | 3 | Module Name: PSScriptAnalyzer | |
| 4 | - ms.date: 10/07/2021 | ||
| 4 | + ms.date: 12/12/2024 | ||
| 5 | 5 | online version: https://learn.microsoft.com/powershell/module/psscriptanalyzer/get-scriptanalyzerrule?view=ps-modules&wt.mc_id=ps-gethelp | |
| 6 | 6 | schema: 2.0.0 | |
| 7 | 7 | --- | |
@@ -92,7 +92,7 @@ one value, but wildcards are supported. To get rules in subdirectories of the pa | |||
| 92 | 92 | **RecurseCustomRulePath** parameter. | |
| 93 | 93 | ||
| 94 | 94 | You can create custom rules using a .NET assembly or a PowerShell module, such as the | |
| 95 | - [Community Analyzer Rules](https://github.com/PowerShell/PSScriptAnalyzer/blob/development/Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1) | ||
| 95 | + [Community Analyzer Rules](https://github.com/PowerShell/PSScriptAnalyzer/tree/main/Tests/Engine/CommunityAnalyzerRules) | ||
| 96 | 96 | in the GitHub repository. | |
| 97 | 97 | ||
| 98 | 98 | ```yaml | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -192,7 +192,7 @@ value of the **Profile** parameter is the path to the Script Analyzer profile. | |||
| 192 | 192 | ExcludeRules = '*WriteHost' | |
| 193 | 193 | } | |
| 194 | 194 | ||
| 195 | - Invoke-ScriptAnalyzer -Path $pshome\Modules\BitLocker -Profile .\ScriptAnalyzerProfile.txt | ||
| 195 | + Invoke-ScriptAnalyzer -Path $pshome\Modules\BitLocker -Settings .\ScriptAnalyzerProfile.txt | ||
| 196 | 196 | ``` | |
| 197 | 197 | ||
| 198 | 198 | If you include a conflicting parameter in the `Invoke-ScriptAnalyzer` command, such as | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,10 +21,13 @@ checks the quality of PowerShell code by running a set of rules. | |||
| 21 | 21 | ## PSScriptAnalyzer Cmdlets | |
| 22 | 22 | ||
| 23 | 23 | ### [Get-ScriptAnalyzerRule](Get-ScriptAnalyzerRule.md) | |
| 24 | + | ||
| 24 | 25 | Gets the script analyzer rules on the local computer. | |
| 25 | 26 | ||
| 26 | 27 | ### [Invoke-Formatter](Invoke-Formatter.md) | |
| 28 | + | ||
| 27 | 29 | Formats a script text based on the input settings or default settings. | |
| 28 | 30 | ||
| 29 | 31 | ### [Invoke-ScriptAnalyzer](Invoke-ScriptAnalyzer.md) | |
| 32 | + | ||
| 30 | 33 | Evaluates a script or module based on selected best practice rules | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,11 @@ only be assigned in certain special cases to achieve a certain effect as a speci | |||
| 16 | 16 | ||
| 17 | 17 | To understand more about automatic variables, see `Get-Help about_Automatic_Variables`. | |
| 18 | 18 | ||
| 19 | + <!-- TODO | ||
| 20 | + Ability to suppress was added in https://github.com/PowerShell/PSScriptAnalyzer/pull/1896 | ||
| 21 | + Need documentation for how to configure suppression of this rule. | ||
| 22 | + --> | ||
| 23 | + | ||
| 19 | 24 | ## How | |
| 20 | 25 | ||
| 21 | 26 | Use variable names in functions or their parameters that do not conflict with automatic variables. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | --- | |
| 2 | 2 | description: Switch Parameters Should Not Default To True | |
| 3 | - ms.date: 06/28/2023 | ||
| 3 | + ms.date: 12/05/2024 | ||
| 4 | 4 | ms.topic: reference | |
| 5 | 5 | title: AvoidDefaultValueSwitchParameter | |
| 6 | 6 | --- | |
@@ -10,11 +10,19 @@ title: AvoidDefaultValueSwitchParameter | |||
| 10 | 10 | ||
| 11 | 11 | ## Description | |
| 12 | 12 | ||
| 13 | - Switch parameters for commands should default to false. | ||
| 13 | + If your parameter takes only `true` and `false`, define the parameter as type `[Switch]`. PowerShell | ||
| 14 | + treats a switch parameter as `true` when it's used with a command. If the parameter isn't included | ||
| 15 | + with the command, PowerShell considers the parameter to be false. Don't define `[Boolean]` | ||
| 16 | + parameters. | ||
| 17 | + | ||
| 18 | + You shouldn't define a switch parameter with a default value of `$true` because this isn't the | ||
| 19 | + expected behavior of a switch parameter. | ||
| 14 | 20 | ||
| 15 | 21 | ## How | |
| 16 | 22 | ||
| 17 | - Change the default value of the switch parameter to be false. | ||
| 23 | + Change the default value of the switch parameter to be `$false` or don't provide a default value. | ||
| 24 | + Write the logic of the script to assume that the switch parameter default value is `$false` or not | ||
| 25 | + provided. | ||
| 18 | 26 | ||
| 19 | 27 | ## Example | |
| 20 | 28 | ||
@@ -48,8 +56,22 @@ function Test-Script | |||
| 48 | 56 | $Param1, | |
| 49 | 57 | ||
| 50 | 58 | [switch] | |
| 51 | - $Switch=$False | ||
| 59 | + $Switch | ||
| 52 | 60 | ) | |
| 61 | + | ||
| 62 | + begin { | ||
| 63 | + # Ensure that the $Switch is set to false if not provided | ||
| 64 | + if (-not $PSBoundParameters.ContainsKey('Switch')) { | ||
| 65 | + $Switch = $false | ||
| 66 | + } | ||
| 67 | + } | ||
| 53 | 68 | ... | |
| 54 | 69 | } | |
| 55 | 70 | ``` | |
| 71 | + | ||
| 72 | + ## More information | ||
| 73 | + | ||
| 74 | + - [Strongly Encouraged Development Guidelines][01] | ||
| 75 | + | ||
| 76 | + <!-- link references --> | ||
| 77 | + [01]: https://learn.microsoft.com/powershell/scripting/developer/cmdlet/strongly-encouraged-development-guidelines#parameters-that-take-true-and-false | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,6 @@ title: AvoidGlobalFunctions | |||
| 13 | 13 | Globally scoped functions override existing functions within the sessions with matching names. This | |
| 14 | 14 | name collision can cause difficult to debug issues for consumers of modules. | |
| 15 | 15 | ||
| 16 | - | ||
| 17 | 16 | To understand more about scoping, see `Get-Help about_Scopes`. | |
| 18 | 17 | ||
| 19 | 18 | ## How | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | --- | |
| 2 | 2 | description: Avoid overwriting built in cmdlets | |
| 3 | - ms.date: 06/28/2023 | ||
| 3 | + ms.date: 12/12/2024 | ||
| 4 | 4 | ms.topic: reference | |
| 5 | 5 | title: AvoidOverwritingBuiltInCmdlets | |
| 6 | 6 | --- | |
@@ -14,7 +14,7 @@ This rule flags cmdlets that are available in a given edition/version of PowerSh | |||
| 14 | 14 | operating system which are overwritten by a function declaration. It works by comparing function | |
| 15 | 15 | declarations against a set of allowlists that ship with PSScriptAnalyzer. These allowlist files are | |
| 16 | 16 | used by other PSScriptAnalyzer rules. More information can be found in the documentation for the | |
| 17 | - [UseCompatibleCmdlets](./UseCompatibleCmdlets.md) rule. | ||
| 17 | + [UseCompatibleCmdlets][01] rule. | ||
| 18 | 18 | ||
| 19 | 19 | ## Configuration | |
| 20 | 20 | ||
@@ -37,14 +37,17 @@ following your settings file. | |||
| 37 | 37 | ||
| 38 | 38 | The parameter `PowerShellVersion` is a list of allowlists that ship with PSScriptAnalyzer. | |
| 39 | 39 | ||
| 40 | - **Note**: The default value for `PowerShellVersion` is `core-6.1.0-windows` if PowerShell 6 or | ||
| 41 | - later is installed, and `desktop-5.1.14393.206-windows` if it is not. | ||
| 40 | + > [!NOTE] | ||
| 41 | + > The default value for `PowerShellVersion` is `core-6.1.0-windows` if PowerShell 6 or | ||
| 42 | + > later is installed, and `desktop-5.1.14393.206-windows` if it's not. | ||
| 42 | 43 | ||
| 43 | 44 | Usually, patched versions of PowerShell have the same cmdlet data, therefore only settings of major | |
| 44 | 45 | and minor versions of PowerShell are supplied. One can also create a custom settings file as well | |
| 45 | - with the | ||
| 46 | - [New-CommandDataFile.ps1](https://github.com/PowerShell/PSScriptAnalyzer/blob/development/Utils/New-CommandDataFile.ps1) | ||
| 47 | - script and use it by placing the created `JSON` into the `Settings` folder of the `PSScriptAnalyzer` | ||
| 48 | - module installation folder, then the `PowerShellVersion` parameter is just its file name (that can | ||
| 49 | - also be changed if desired). Note that the `core-6.0.2-*` files were removed in PSScriptAnalyzer | ||
| 50 | - 1.18 since PowerShell 6.0 reached end of life. | ||
| 46 | + with the [New-CommandDataFile.ps1][02] script and use it by placing the created `JSON` into the | ||
| 47 | + `Settings` folder of the `PSScriptAnalyzer` module installation folder, then the `PowerShellVersion` | ||
| 48 | + parameter is just its filename (that can also be changed if desired). Note that the `core-6.0.2-*` | ||
| 49 | + files were removed in PSScriptAnalyzer 1.18 since PowerShell 6.0 reached end of life. | ||
| 50 | + | ||
| 51 | + <!-- link references --> | ||
| 52 | + [01]: ./UseCompatibleCmdlets.md | ||
| 53 | + [02]: https://github.com/PowerShell/PSScriptAnalyzer/blob/main/Utils/New-CommandDataFile.ps1 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,7 @@ There are also implicit aliases. When PowerShell cannot find the cmdlet name, it | |||
| 20 | 20 | Every PowerShell author learns the actual command names, but different authors learn and use | |
| 21 | 21 | different aliases. Aliases can make code difficult to read, understand and impact availability. | |
| 22 | 22 | ||
| 23 | - Using the full command name makes it eaiser to maintain your scripts in the the future. | ||
| 23 | + Using the full command name makes it easier to maintain your scripts in the the future. | ||
| 24 | 24 | ||
| 25 | 25 | Using the full command names also allows for syntax highlighting in sites and applications like | |
| 26 | 26 | GitHub and Visual Studio Code. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | --- | |
| 2 | 2 | description: Avoid Using SecureString With Plain Text | |
| 3 | - ms.date: 06/28/2023 | ||
| 3 | + ms.date: 01/28/2025 | ||
| 4 | 4 | ms.topic: reference | |
| 5 | 5 | title: AvoidUsingConvertToSecureStringWithPlainText | |
| 6 | 6 | --- | |
@@ -37,6 +37,4 @@ $EncryptedInput = ConvertTo-SecureString -String $UserInput -AsPlainText -Force | |||
| 37 | 37 | ||
| 38 | 38 | ```powershell | |
| 39 | 39 | $SecureUserInput = Read-Host 'Please enter your secure code' -AsSecureString | |
| 40 | - $EncryptedInput = ConvertFrom-SecureString -String $SecureUserInput | ||
| 41 | - $SecureString = ConvertTo-SecureString -String $EncryptedInput | ||
| 42 | 40 | ``` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | --- | |
| 2 | 2 | description: Avoid Using Write-Host | |
| 3 | - ms.date: 06/28/2023 | ||
| 3 | + ms.date: 12/05/2024 | ||
| 4 | 4 | ms.topic: reference | |
| 5 | 5 | title: AvoidUsingWriteHost | |
| 6 | 6 | --- | |
@@ -10,10 +10,15 @@ title: AvoidUsingWriteHost | |||
| 10 | 10 | ||
| 11 | 11 | ## Description | |
| 12 | 12 | ||
| 13 | - The use of `Write-Host` is greatly discouraged unless in the use of commands with the `Show` verb. | ||
| 14 | - The `Show` verb explicitly means 'show on the screen, with no other possibilities'. | ||
| 13 | + The primary purpose of the `Write-Host` cmdlet is to produce display-only output in the host. For | ||
| 14 | + example: printing colored text or prompting the user for input when combined with `Read-Host`. | ||
| 15 | + `Write-Host` uses the `ToString()` method to write the output. The particular result depends on the | ||
| 16 | + program that's hosting PowerShell. The output from `Write-Host` isn't sent to the pipeline. To | ||
| 17 | + output data to the pipeline, use `Write-Output` or implicit output. | ||
| 15 | 18 | ||
| 16 | - Commands with the `Show` verb do not have this check applied. | ||
| 19 | + The use of `Write-Host` in a function is discouraged unless the function uses the `Show` verb. The | ||
| 20 | + `Show` verb explicitly means _display information to the user_. This rule doesn't apply to functions | ||
| 21 | + with the `Show` verb. | ||
| 17 | 22 | ||
| 18 | 23 | ## How | |
| 19 | 24 | ||
@@ -27,22 +32,22 @@ logging or returning one or more objects. | |||
| 27 | 32 | ```powershell | |
| 28 | 33 | function Get-MeaningOfLife | |
| 29 | 34 | { | |
| 30 | - ... | ||
| 31 | 35 | Write-Host 'Computing the answer to the ultimate question of life, the universe and everything' | |
| 32 | - ... | ||
| 33 | 36 | Write-Host 42 | |
| 34 | 37 | } | |
| 35 | 38 | ``` | |
| 36 | 39 | ||
| 37 | 40 | ### Correct | |
| 38 | 41 | ||
| 42 | + Use `Write-Verbose` for informational messages. The user can decide whether to see the message by | ||
| 43 | + providing the **Verbose** parameter. | ||
| 44 | + | ||
| 39 | 45 | ```powershell | |
| 40 | 46 | function Get-MeaningOfLife | |
| 41 | 47 | { | |
| 42 | - [CmdletBinding()]Param() # to make it possible to set the VerbosePreference when calling the function | ||
| 43 | - ... | ||
| 48 | + [CmdletBinding()]Param() # makes it possible to support Verbose output | ||
| 49 | + | ||
| 44 | 50 | Write-Verbose 'Computing the answer to the ultimate question of life, the universe and everything' | |
| 45 | - ... | ||
| 46 | 51 | Write-Output 42 | |
| 47 | 52 | } | |
| 48 | 53 | ||
@@ -51,3 +56,7 @@ function Show-Something | |||
| 51 | 56 | Write-Host 'show something on screen' | |
| 52 | 57 | } | |
| 53 | 58 | ``` | |
| 59 | + | ||
| 60 | + ## More information | ||
| 61 | + | ||
| 62 | + [Write-Host](xref:Microsoft.PowerShell.Utility.Write-Host) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments