我正在实现一个自定义 PowerShell 提供程序。我现在正在研究remove-item cmdlet 实现。
RemoveItem 方法具有以下签名:
protected override void RemoveItem(string path, bool recurse)
当我键入:Remove-Item .\Myobject -recurse
时,PowerShell 基础结构会在 recurse< 中为我提供值 true
RemoveItem 方法的 /code> 参数。
但是,当我输入:Remove-Item .\MyObject' 时,我收到一个问题:
MyObject 中的项目有子项,并且未指定 Recurse 参数。如果你继续,
所有儿童都将与该物品一起被带走。您确定要继续吗?
[Y] 是 [A] 全部是 [N] 否 [L] 全部否 [S] 暂停 [?] 帮助(默认为“Y”):
我想这个问题是由 PowerShell 基础设施向我提出的。这完全没问题,因为我要删除的对象是一个容器。如果我对上述问题的回答是“是”,则 PowerShell 基础结构不会设置递归参数。事实上,当调用我的 RemoveItem
方法时,它是错误的。我希望参数为 true,因为我对这个问题的回答是“是”。
我的问题是:
-
为什么 PowerShell 没有将 bool recurse 参数设置为正确的值?
-
我需要以其他方式获取值(问题的答案)吗?如何?
-
如果上述不可能,是否有办法抑制该问题?
I'm implementing a custom PowerShell provider. I'm now working on the remove-item cmdlet implementation.
The RemoveItem method has the following signature:
protected override void RemoveItem(string path, bool recurse)
When I type: Remove-Item .\Myobject -recurse
the PowerShell infrastructure provides me with the value true
in the recurse
parameter of the RemoveItem method.
However when I type: Remove-Item .\MyObject' I get a question:
The item at MyObject has children and the Recurse parameter was not specified. If you continue,
all children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
I guess this question is presented to my by the PowerShell infrastructure. This is perfectly fine because the object that I want to remove is a container. If I answer yes to the above question the PowerShell infrastructure does not set the recurse parameter. In fact it is false when my RemoveItem
method is called. I would except the parameter to be true because I answered yes to the question.
My questions are:
-
Why does PowerShell not set the bool recurse parameter to the correct value?
-
Do I need to get the value (answer to the question) in some other way? How?
-
If above is not possible is there a way to suppress the question?
发布评论
评论(2)
最终,如果您被要求删除一个容器,那么它本质上是递归的,除非该容器不包含其他容器。我相信 PowerShell 会进行提示,因为该操作的影响超出了用户最初可能意识到的范围(所有容器的内容),并且需要确认。所以在这种情况下,我认为 -recurse 用于告诉 PowerShell“我知道我在做什么”。
-recurse 更有意义的是,如果您要执行这样的操作:
在这种情况下,您想要递归搜索要删除的文件。不幸的是,Remove-Item 上的 Recurse 参数对于这种用法已损坏 - 来自文档:
所以目前的做法是:
Ultimately if you're asked to remove a container then it is inherently recursive unless the container doesn't contain other containers. I believe PowerShell prompts because the action affects more than the user might initially be aware of (all the container's contents) and warrants confirmation. So in this case, I think the -recurse is used to tell PowerShell "I know what I'm doing".
Where -recurse makes more sense is if you were to execute something like this:
In this case, you want to recursively search for files to delete. Unfortunately the Recurse parameter on Remove-Item is broken for this usage - from the docs:
So the way to do this currently is:
抑制问题
您可以通过设置
$ConfirmPreference="None"
http://blogs.msdn.com/b/powershell/archive/2006/12/15/confirmpreference.aspxYou can suppress the question by setting
$ConfirmPreference="None"
http://blogs.msdn.com/b/powershell/archive/2006/12/15/confirmpreference.aspx