Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors in ShouldProcess rule document #1766

Merged
merged 7 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The PSScriptAnalyzer contains the following rule definitions.
| [ReservedCmdletChar](./ReservedCmdletChar.md) | Error | Yes | |
| [ReservedParams](./ReservedParams.md) | Error | Yes | |
| [ReviewUnusedParameter](./ReviewUnusedParameter.md) | Warning | Yes | |
| [ShouldProcess](./ShouldProcess.md) | Error | Yes | |
| [ShouldProcess](./ShouldProcess.md) | Warning | Yes | |
masaru-iritani marked this conversation as resolved.
Show resolved Hide resolved
| [UseApprovedVerbs](./UseApprovedVerbs.md) | Warning | Yes | |
| [UseBOMForUnicodeEncodedFile](./UseBOMForUnicodeEncodedFile.md) | Warning | Yes | |
| [UseCmdletCorrectly](./UseCmdletCorrectly.md) | Warning | Yes | |
Expand Down
74 changes: 41 additions & 33 deletions docs/Rules/ShouldProcess.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
description: Should Process
ms.custom: PSSA v1.20.0
ms.date: 10/18/2021
ms.date: 01/23/2022
ms.topic: reference
title: ShouldProcess
---
Expand All @@ -14,55 +14,63 @@ title: ShouldProcess
If a cmdlet declares the `SupportsShouldProcess` attribute, then it should also call
`ShouldProcess`. A violation is any function which either declares `SupportsShouldProcess` attribute
but makes no calls to `ShouldProcess` or it calls `ShouldProcess` but does not declare
`SupportsShouldProcess`
`SupportsShouldProcess`.

For more information, please refer to `about_Functions_Advanced_Methods` and
`about_Functions_CmdletBindingAttribute`
For more information, please refer to [about_Functions_Advanced_Methods][1] and
[about_Functions_CmdletBindingAttribute][2].

[1]: /powershell/module/Microsoft.PowerShell.Core/About/about_Functions_Advanced_Methods
[2]: /powershell/module/Microsoft.PowerShell.Core/About/about_Functions_CmdletBindingAttribute

## How

To fix a violation of this rule, please call `ShouldProcess` method when a cmdlet declares
`SupportsShouldProcess` attribute. Or please add `SupportsShouldProcess` attribute argument when
calling `ShouldProcess`
calling `ShouldProcess`.

## Example

### Wrong

```powershell
function Set-File
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param
(
# Path to file
[Parameter(Mandatory=$true)]
$Path
)
"String" | Out-File -FilePath $FilePath
}
function Set-File
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param
(
# Path to file
[Parameter(Mandatory=$true)]
$Path
)

"String" | Out-File -FilePath $Path
}
```

### Correct

```powershell
function Set-File
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param
(
# Path to file
[Parameter(Mandatory=$true)]
$Path
)
function Set-File
{
[CmdletBinding(SupportsShouldProcess=$true)]
Param
(
# Path to file
[Parameter(Mandatory=$true)]
$Path,

if ($PSCmdlet.ShouldProcess("Target", "Operation"))
{
"String" | Out-File -FilePath $FilePath
}
else
{
Write-Host ('Write "String" to file {0}' -f $FilePath)
}
[Parameter(Mandatory=$true)]
[string]$Content
)

if ($PSCmdlet.ShouldProcess($Path, ("Setting content to '{0}'" -f $Content)))
{
$Content | Out-File -FilePath $Path
}
else
sdwheeler marked this conversation as resolved.
Show resolved Hide resolved
{
# Code that should be processed if doing a WhatIf operation
# Must NOT change anything outside of the function / script
}
}
```