PowerShell自定义提供程序RemoveItem [英] PowerShell custom provider RemoveItem

查看:261
本文介绍了PowerShell自定义提供程序RemoveItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现自定义 PowerShell 提供程序.我正在研究remove-item cmdlet的实现.

I'm implementing a custom PowerShell provider. I'm now working on the remove-item cmdlet implementation.

RemoveItem方法具有以下签名:

The RemoveItem method has the following signature:

protected override void RemoveItem(string path, bool recurse)

当我键入:Remove-Item .\Myobject -recurse时,PowerShell基础结构会在RemoveItem方法的recurse参数中为我提供值true.

When I type: Remove-Item .\Myobject -recurse the PowerShell infrastructure provides me with the value true in the recurse parameter of the RemoveItem method.

但是当我键入:Remove-Item.\ MyObject'时,我遇到一个问题:

However when I type: Remove-Item .\MyObject' I get a question:

MyObject上的项具有子项,并且未指定Recurse参数.如果继续 所有孩子都将与物品一起被移走.你确定你要继续吗? [Y]是[A]对所有人[N]否[L]对所有人[S]暂停[?]帮助(默认为"Y"):

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"):

我想这个问题是由PowerShell基础结构提出的.这是完全可以的,因为我要删除的对象是一个容器.如果我对上述问题的回答是是",那么PowerShell基础结构不会设置recurse参数.实际上,当调用我的RemoveItem方法时,它是错误的.我会将该参数除外,因为我对这个问题的回答是肯定的.

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.

我的问题是:

  1. PowerShell为什么不将bool recurse参数设置为正确的值?

  1. 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?

推荐答案

最终,如果要求您删除一个容器,则该容器本质上是递归的,除非该容器不包含其他容器.我相信PowerShell会发出提示,因为该操作的影响程度超出了用户最初可能意识到的(所有容器的内容)并需要确认.因此,在这种情况下,我认为-recurse用于告诉PowerShell我知道我在做什么".

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".

-recurse更有意义的地方是您是否要执行以下操作:

Where -recurse makes more sense is if you were to execute something like this:

Remove-Item *.bak -recurse

在这种情况下,您要递归搜索要删除的文件.不幸的是,针对此用法,Remove-Item上的Recurse参数已损坏-来自文档:

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:

因为在此的Recurse参数 cmdlet错误,命令使用 Get-Childitem cmdlet获取 所需的文件,它使用 管道运营商将其传递给 Remove-Item cmdlet.

Because the Recurse parameter in this cmdlet is faulty, the command uses the Get-Childitem cmdlet to get the desired files, and it uses the pipeline operator to pass them to the Remove-Item cmdlet.

因此当前执行此操作的方法是:

So the way to do this currently is:

Get-ChildItem . -r *.bak | Remove-Item

这篇关于PowerShell自定义提供程序RemoveItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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