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

Incorrect explanation of ShouldProcess #23

Closed
gwpelletier opened this issue Jan 25, 2022 · 0 comments · Fixed by #46
Closed

Incorrect explanation of ShouldProcess #23

gwpelletier opened this issue Jan 25, 2022 · 0 comments · Fixed by #46
Assignees
Labels
area-scriptanalyzer Area - ScriptAnalyzer module

Comments

@gwpelletier
Copy link

The "Correct" version of this isn't quite right.

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

        [Parameter(Mandatory=$true)]
        [string]$Content
    )

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

Update reference documentation to include this one:
https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-shouldprocess


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-scriptanalyzer Area - ScriptAnalyzer module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants