PowerShell guidelines for -Confirm, -Force, and -WhatIf

后端 未结 3 448
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 16:19

Are there any official guidelines from Microsoft about when to add -Confirm, -Force, and -WhatIf parameters to custom PowerShell cmdlets?

3条回答
  •  感情败类
    2021-02-07 17:02

    If you want to validate that your implementation of these common parameters is compliant to the guidelines (for example, Set-Xxx cmdlets should have -Confirm and -WhatIf), then you can use the excellent PsScriptAnalyzer module (which is based on code analysis).

    Make sure the module is installed:

    PS E:\> Install-Module -Name 'PsScriptAnalyzer'
    

    Then run PowerShell Code Analysis as follows:

    PS E:\> Invoke-ScriptAnalyzer -Path . | FL
    
    RuleName : PSUseShouldProcessForStateChangingFunctions
    Severity : Warning 
    Line     : 78 
    Column   : 10 
    Message  : Function 'Update-something' has verb that could change system state. 
               Therefore, the function has to support 'ShouldProcess'.
    

    Documentation (and sources) can be found on GitHub: https://github.com/PowerShell/PSScriptAnalyzer

提交回复
热议问题