MicrosoftDocs / windows-powershell-docs

This repo is used to contribute to Windows 10, Windows Server 2016, and MDOP PowerShell module documentation.
Creative Commons Attribution 4.0 International
444 stars 592 forks source link

Confirm switch is not false by default! #1395

Closed lrandall-gillsch closed 4 years ago

lrandall-gillsch commented 4 years ago

Please update the documentation to be correct. PowerShell in WinPE prompts before the disk is cleared, the documentation suggests the default is false and will not prompt the user to clear.

Thanks!


Document details

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

velkovb commented 4 years ago

Hello @lrandall-gillsch

Thank you for trying to make MicrosoftDocs a better place.

By default switch parameters are always false. unless explicitly specified - Switch Parameters. Here you are not prompted because of the -Confirm parameter but because the impact of the cmdlet is High. You could read more about cmdlet confirm impact and preference - $ConfirmPreference, ConfirmImpact

Let me know if you have any questions.

o0nj commented 4 years ago

Hello @lrandall-gillsch,

The issue is being closed since your question seems to be answered. Feel free to re-open this issue if you feel that it hasn't been answered or that there are further suggestions to improve the documentation itself.

Thanks.

lrandall-gillsch commented 4 years ago

Hi

Hello @lrandall-gillsch,

The issue is being closed since your question seems to be answered. Feel free to re-open this issue if you feel that it hasn't been answered or that there are further suggestions to improve the documentation itself.

Thanks.

Hi @e0i,

I've just used the cmdlet on Windows 10 Pro Version 1903 and when running the cmdlet with these parameters: "Clear-Disk -Number 1", it prompts for confirmation, which is the opposite to what the documentation says for it, thus "-Confirm" here is not false by default, it is true by default.

image

Many thanks,

Luke

velkovb commented 4 years ago

@lrandall-gillsch Hello,

How do you know that "-Confirm" is true?

I do not think you went through the provided articles to understand how the prompt for confirmation actually works. Confirm is a Switch parameter. Here is the definition of a switch parameter:

Windows PowerShell provides a System.Management.Automation.SwitchParameter type that allows you to define a parameter whose value is automatically set to false if the parameter is not specified when the cmdlet is called. Whenever possible, use switch parameters in place of Boolean parameters.

In the example that you have provided "-Confirm" is not specified so by definition it is false. Here is an example: image

You can see that the second one does not prompt and both times I have not specified "-Confirm"

Furthermore, you could check the following article to understand confirmation requests - https://docs.microsoft.com/powershell/developer/cmdlet/users-requesting-confirmation

Let me know if that help you understand better how switch parameters works.

o0nj commented 4 years ago

@lrandall-gillsch Let us know whether the answer above clarifies your follow-up question. Thanks.

lrandall-gillsch commented 4 years ago

@lrandall-gillsch Hello,

How do you know that "-Confirm" is true?

I do not think you went through the provided articles to understand how the prompt for confirmation actually works. Confirm is a Switch parameter. Here is the definition of a switch parameter:

Windows PowerShell provides a System.Management.Automation.SwitchParameter type that allows you to define a parameter whose value is automatically set to false if the parameter is not specified when the cmdlet is called. Whenever possible, use switch parameters in place of Boolean parameters.

In the example that you have provided "-Confirm" is not specified so by definition it is false. Here is an example: image

You can see that the second one does not prompt and both times I have not specified "-Confirm"

Furthermore, you could check the following article to understand confirmation requests - https://docs.microsoft.com/powershell/developer/cmdlet/users-requesting-confirmation

Let me know if that help you understand better how switch parameters works.

Hi,

In your example you set "$ConfirmPreference" to "None", which appears to allow the command to run the "Clear-Disk" cmdlet without confirmation, which would suggest "-confirm $false" is default, however whenever I run the cmdlet in Windows 10 and WinPE Version 1903 (On and off a domain if that makes any difference), as per my example, without setting this variable (I've never set this variable, so whatever value is currently set is default), it appears that for me "-confirm $true" is default.

In my case, on my personal machine (In the example I posted), I've tested that the default for "-confirm" for this cmdlet is true, if it was false it shouldn't prompt for confirmation should it?

In the PowerShell scripts I've created, I've not had to use the "$ConfirmPreference" and set it to "None" to ensure that a cmdlet doesn't prompt for confirmation or to enforce false as default, so in all cases that variable hasn't been set, in my experience it depends on the cmdlet you're using as to what the default is.

Many thanks,

Luke

lrandall-gillsch commented 4 years ago

In the article that you linked it says this:

The default setting of $ConfirmPreference is High. Therefore, in the default environment, only cmdlets and providers that specify a high-impact action request confirmation.

So I'm led to believe that the "-confirm" parameter for "Clear-Disk" is true as I would say it is a high-impact action request because it could cause users to lose data if used incorrectly?

velkovb commented 4 years ago

@lrandall-gillsch Hi Luke,

Lets get back to the definition of a switch parameter(-Confirm is switch parameter):

Windows PowerShell provides a System.Management.Automation.SwitchParameter type that allows you to define a parameter whose value is automatically set to false if the parameter is not specified when the cmdlet is called. Whenever possible, use switch parameters in place of Boolean parameters.

This means that unless "-Confirm" is explicitly specified it is false so it is default value (the value when it is not specified) is false.

In you case you are not prompted because "-Confirm" is true but because the cmdlet implements ShouldProcess and its impacts is High. It will always prompt for confirmation with our without the "-Confirm" parameter. You can override this by setting the ConfirmPreference variable to none or by passing "-Confirm:$false".

For example if a command has an impact of Medium, you will not be prompted for confirmation by default and if you specify the "-Confirm" parameter it will prompt you. For example: image

lrandall-gillsch commented 4 years ago

@lrandall-gillsch Hi Luke,

Lets get back to the definition of a switch parameter(-Confirm is switch parameter):

Windows PowerShell provides a System.Management.Automation.SwitchParameter type that allows you to define a parameter whose value is automatically set to false if the parameter is not specified when the cmdlet is called. Whenever possible, use switch parameters in place of Boolean parameters.

This means that unless "-Confirm" is explicitly specified it is false so it is default value (the value when it is not specified) is false.

In you case you are not prompted because "-Confirm" is true but because the cmdlet implements ShouldProcess and its impacts is High. It will always prompt for confirmation with our without the "-Confirm" parameter. You can override this by setting the ConfirmPreference variable to none or by passing "-Confirm:$false".

For example if a command has an impact of Medium, you will not be prompted for confirmation by default and if you specify the "-Confirm" parameter it will prompt you. For example: image

Hi,

I understand what your saying, totally, however I am conflicted between this documentation, your explanation, the outcome of my testing above, and the documentation for confirmations with powershell commands here: https://docs.microsoft.com/powershell/developer/cmdlet/users-requesting-confirmation

You're saying the default here is false, however, in my tests I don't specify the parameter and I don't specify the $ConfirmPreference variable either, which means in a default case scenario the default for "Clear-Disk" is true wouldn't it, seeing as though the $ConfirmPreference default value is high, thus meaning "Clear-Disk" high-impact request?

In your example you're proving that providing the "-Confirm" parameter with the "New-File" cmdlet prompts you for confirmation and as the default for $ConfirmPreference, it the same cmdlet doesn't prompt you for confirmation.

Please see my testing below with the "New-Item" cmdlet and the $ConfirmPreference variable:

image

First I set the location to C:\, then I run "New-Item test.txt" which doesn't prompt for confirmation, then I set $ConfirmPreference to "Medium" and then run "New-Item test2.txt" which does prompt for confirmation as it's impact is medium.

If you run "Get-Command Clear-Disk | Fl" which reveals the cmdlets powershell code, it reveals that its ConfirmImpact is High, so with a default value of High for $ConfirmPreference, Clear-Disk will prompt for confirmation so, the default has to be true for -Confirm?

lrandall-gillsch commented 4 years ago

Apologies, didn't mean to close this.

lrandall-gillsch commented 4 years ago

And thank you for the useful PowerShell information!

Luke

velkovb commented 4 years ago

Hi Luke,

The Confirm parameter and prompting for confirmation are two separate things. A cmdlet can prompt for confirmation without implementing the Confirm parameter. Confirm is a switch parameter which is false unless explicitly specified. Verbose, Debug, WhatIf have the same behaviour.

$ConfirmPreference is a global variable that controls how you are prompted for confirmation. For example, if a command has Medium impact and your $ConfirmPreference is set to Medium you will be prompted for that command but not prompted for a command that has Low impact. You will need to specify -Confirm to get a prompt for the Low impact command.

I guess you can say that you need to explicitly specify -Confirm to be prompted for confirmation for commands that have lower impact than your $ConfirmPreference variable.

lrandall-gillsch commented 4 years ago

Hi,

OK, thank you for the information.

I have just run "Clear-Disk -Number 1 -Confirm" thus running it with the default value of -Confirm, as I have not specified it? Which results in a confirm prompt, which means that -Confirm is true here? In a completely default scenario, this means -Confirm is default, surely?

image

Many thanks,

Luke

velkovb commented 4 years ago

Hi Luke,

I don't think you get the switch parameters :) You probably confuse them with boolean. You cannot pass a value to the switch parameter. A switch parameter is false if not specified and true only if it is specified (if you include -Confirm in the command line) . So when you specify it in the command that makes it true. You can only override this behaviour with a slightly different syntax - "-Confirm:$false".

o0nj commented 4 years ago

Hello @lrandall-gillsch ,

The issue is being closed since your question seems to be answered. Feel free to re-open this issue if you feel that it hasn't been answered or that there are further suggestions to improve the documentation.

Thanks.

lrandall-gillsch commented 4 years ago

OK, this documentation is very confusing! To me if you specify "-Confirm" with the cmdlet and as it confirm prompts that would say to me that the value of confirm is $true, which contradicts what you said.

In any case, is it possible to include somewhere in the documentation that this cmdlet confirm prompts by default unless you specify -Confirm:$false as when you run the cmdlet with -WhatIf it won't confirm prompt, so some sceptics like me, may think it won't confirm prompt when you run the cmdlet normally (When in fact it does! Without specifying the -Confirm parameter as you say that is effectively irrelevant) as it doesn't when you add the parameter -WhatIf

All this seems very contradictory to me, -Confirm switches are always false yet this has nothing to do a cmdlet confirm prompting without even specifying the -Confirm parameter as this relates to the impact of the cmdlet and the value of $ConfirmPreference.

Many thanks,

Luke

lrandall-gillsch commented 4 years ago

Hi,

I think I have a better description of the -Confirm parameter that I found on another Microsoft documentation page for the "Set-UserPhoto" cmdlet, please see below:

image

For me it explains clearly about the -Confirm parameter, which makes more sense than what is currently present on this page, in my opinion.

Many thanks,

Luke