-Confirm、-Force 和 -WhatIf 的 PowerShell 指南 [英] PowerShell guidelines for -Confirm, -Force, and -WhatIf

查看:67
本文介绍了-Confirm、-Force 和 -WhatIf 的 PowerShell 指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft 是否有任何关于何时将 -Confirm-Force-WhatIf 参数添加到自定义 PowerShell cmdlet 的官方指南?关于何时/如何使用这些参数似乎没有明确的共识.例如这个问题.

Are there any official guidelines from Microsoft about when to add -Confirm, -Force, and -WhatIf parameters to custom PowerShell cmdlets? There doesn't seem to be a clear consensus about when/how to use these parameters. For example this issue.

在没有正式指南的情况下,是否有最佳实践或经验法则可供使用?以下是更多背景,以我目前的(可能有缺陷的)理解:

In the absence of formal guidelines, is there a best practice or rule of thumb to use? Here is some more background, with my current (possibly flawed) understanding:

-WhatIf 标志显示 cmdlet 将执行的操作,而无需实际执行任何操作.这对于潜在不稳定操作的试运行很有用,以查看实际结果.如果 cmdlet 的 Cmdlet 属性具有 SupportsShouldProcess 属性设置为 true.

The -WhatIf flag displays what the cmdlet would do without actually performing any action. This is useful for a dry run of a potentially destabilizing operation, to see what the actual results would be. The parameter is automatically added if the cmdlet's Cmdlet attribute has the SupportsShouldProcess property set to true.

如果您要添加或删除资源,似乎(但我很想在这里看到更多官方指南)您应该添加 -WhatIf.(例如删除文件.)更新现有资源的操作可能不会从中受益.对吗?

It seems like (but I'd love to see more official guidance here) that you should add -WhatIf if you are ever adding or removing resources. (e.g. deleing files.) Operations that update existing resources probably wouldn't benefit from it. Right?

-Force 开关用于声明我知道我在做什么,而且我确定我想这样做".例如,复制文件时 (Copy-File) -Force 参数表示:

The -Force switch is used to declare "I know what I'm doing, and I'm sure I want to do this". For example, when copying a file (Copy-File) the -Force parameter means:

允许 cmdlet 复制不能以其他方式更改的项目,例如复制只读文件或别名.

Allows the cmdlet to copy items that cannot otherwise be changed, such as copying over a read-only file or alias.

所以对我来说,当您遇到 cmdlet 否则会失败的情况时,您似乎(同样,我希望这里有一些官方指导)您应该添加一个可选的 -Force 参数,但是可以被说服完成动作.

So to me it seems like (again, I'd love some official guidance here) that you should add an optional -Force parameter when you have a situation where the cmdlet would otherwise fail, but can be convinced to complete the action.

例如,如果您正在创建一个新资源,该资源将破坏具有相同名称的现有资源.cmdlet 的默认行为将报告错误并失败.但是如果您添加 -Force 它将继续(并覆盖现有资源).对吗?

For example, if you are creating a new resource that will clobber an existing one with the same name. The default behavior of the cmdlet would report an error and fail. But if you add -Force it will continue (and overwrite the existing resource). Right?

如果 cmdlet 将 SupportsShouldProcess 设置为 true,则会自动添加 -Confirm 标志,如 -WhatIf.在 cmdlet 中,如果您调用 ShouldProcess 然后将提示用户执行操作.并且如果添加了-Confirm 标志,则不会有提示.(即通过 cmdlet 调用添加确认.)

The -Confirm flag gets automatically added like -WhatIf if the cmdlet has SupportsShouldProcess set to true. In a cmdlet if you call ShouldProcess then the user will be prompted to perform the action. And if the -Confirm flag is added, there will be no prompt. (i.e. the confirmation is added via the cmdlet invocation.)

所以 -Confirm 应该在 cmdlet 对系统有重大影响时可用.就像 -WhatIf 一样,这应该在添加或删除资源时添加.

So -Confirm should be available whenever a cmdlet has a big impact on the system. Just like -WhatIf this should be added whenever a resource is added or removed.

考虑到我可能不正确的理解,以下是一些我想得到具体答案的问题:

With my potentially incorrect understanding in mind, here are some of the questions I'd like a concrete answer to:

  • 什么时候需要添加-WhatIf/-Confirm?
  • 什么时候需要添加-Force?
  • 同时支持 -Confirm-Force 是否有意义?
  • When should it be necessary to add -WhatIf/-Confirm?
  • When should it be necessary to add -Force?
  • Does it ever make sense to support both -Confirm and -Force?

推荐答案

我没有研究文档是否如此详细,但以下是基于我的观察:

I haven't researched whether the documentation is this detailed, but the following are based on my observations:

  1. 您应该将 -WhatIf 用于进行更改的任何内容.更新是可以从 -WhatIf 中受益的更改(例如,如果您想要进行大量更新怎么办?).

  1. You should use -WhatIf for anything that makes a change. Updates are changes that can benefit from -WhatIf (e.g., what if you want to make a lot of updates?).

-Force 表示强制覆盖现有项目"或覆盖只读文件系统属性".无论哪种情况,操作的成功都取决于用户是否拥有权限.

-Force means "force overwrite of an existing item" or "override a read-only file system attribute". In either case the success of the action depends on the user having permission.

-Confirm-Force 不是相互排斥的.例如,您可以确认写入文件的操作,但该文件可能受到只读属性的保护.在这种情况下,除非您还指定 -Force,否则操作将失败.

-Confirm and -Force are not mutually exclusive. For example, you can confirm an action to write a file, but the file might be protected with the read-only attribute. In this case the action would fail unless you also specify -Force.

这篇关于-Confirm、-Force 和 -WhatIf 的 PowerShell 指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆