Home > Mobile >  Powershell - Confirmation before executing command in function
Powershell - Confirmation before executing command in function

Time:07-17

I put the following function in my powershell profile (path from echo $profile, mine is like D:\C_Drive\Hardlink\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1)

function test {
    [cmdletbinding(SupportsShouldProcess)]
    ls
}
Set-Alias ggg test

with [cmdletbinding(SupportsShouldProcess)] to prompt confirmation, but I get the following error when running ggg

At D:\C_Drive\Hardlink\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:2 char:5
      [cmdletbinding(SupportsShouldProcess)]
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected attribute 'cmdletbinding'.
At D:\C_Drive\Hardlink\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:3 char:5
      ls
      ~~
Unexpected token 'ls' in expression or statement.
      CategoryInfo          : ParserError: (:) [], ParseException
      FullyQualifiedErrorId : UnexpectedAttribute

What is the right way to confirm before executing command in function?

CodePudding user response:

You were on the right track using SupportsShouldProcess, however it's missing ConfirmImpact with ConfirmImpact set to High. This way you define that your function is a high risk function and PowerShell always asks for confirmation. This is also determined by your $ConfirmPreference preference variable which, by default, is set to High:

High: PowerShell prompts for confirmation before running cmdlets or functions with a high risk.

If, for example, the preference variable is set to None, PowerShell would no longer ask you to confirm the action.

function test {
    [cmdletbinding(SupportsShouldProcess, ConfirmImpact = 'High')]
    param([string] $myParam)

    if($PSCmdlet.ShouldProcess([string] $myParam)) {
        Get-ChildItem $myParam
    }
}

Then if we try to call the function:

PS /> test C:\

The following confirmation prompt would appear:

Are you sure you want to perform this action?
Performing the operation "test" on target "C:\".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):

Lastly, Cmdlet.ShouldProcess has many Overloads which offer a more detailed way of asking for confirmation. The one currently used on the test function is the simplest one.

As for the error you're seeing when trying to run your function, this happens because the function has a cmdletbinding attribute declaration making your function and Advanced Function but it's missing a param block even if the function does not have any parameters it should be there. In addition, a function that SupportsShouldProcess must call $PSCmdlet.ShouldProcess(...) to prompt for confirmation.

function test {
    [cmdletbinding()]
    param()

}

CodePudding user response:

Try this:

function test {
   $confirmation = Read-Host "Do you want to continue? [y to continue] "
    if ($confirmation -eq 'y') {
        ls
    }
}
Set-Alias ggg test

More info see here: https://www.delftstack.com/howto/powershell/powershell-yes-no-prompt/

  • Related