开发者

Should I implement the IPropertyCmdletProvider interface for a PowerShell Cmdlet?

I'm writing a NavigationCmdletProvider for PowerShell. Through the GetItem and GetChildItems overrides, there are various types of objects that are written to the pipeline.

The docs for IPropertyCmdletProvider interface tell us the following:

Developers should implement this interface under the following conditions:

  • When users must use cmdlets such as the Get-Property and Set-Property cmdlets.
  • For a providers that derive from the ItemCmdletProvider, ContainerCmdl开发者_开发问答etProvider, or NavigationCmdletProvider classes.

Confusion:

Not a lot of useful information in my opinion because how would a user know if they must use the Get-Property and Set-Property cmdlet's? I would imagine that's up to the Cmdlet author. The big confusion (for me at least) is if the Cmdlet writes the objects to the pipeline; and those objects have properties exposed that are callable (i.e. get/set); what benefits does calling Get-Property/Set-Property have over manipulating the object(s) directly?

Question:

Under what circumstances should the IPropertyCmdletProvider interface be implemented?

I know I'm missing something here! Any insight would be greatly appreciated.


Wow those docs are a bit old. There are no Get/Set-Property cmdlets. This must be referring to the Get/Set-ItemProperty cmdlets. In the case of the RegistryProvider, these cmdlets are essential because it is the only way to access registry values. That is, the Get-Item/ChildItem cmdlets only return RegistryKey objects and never a registry value object (they don't exist in .NET). You have to use Get/Set-ItemProperty to get/set specific regvals under a regkey.

OTOH the FileSystem provider allows you to directly access containers (dirs) and leafs (files). You can get the content of a file directly. Still, you can use Get-ItemProperty if you want to get the LastWriteTime of a file:

PS> Get-ItemProperty -Path .\DotNetTypes.format.ps1xml -Name LastWriteTime


PSPath        : Microsoft.PowerShell.Core\FileSystem::C:\Win
                dows\System32\WindowsPowerShell\v1.0\DotNetT
                ypes.format.ps1xml
PSParentPath  : Microsoft.PowerShell.Core\FileSystem::C:\Win
                dows\System32\WindowsPowerShell\v1.0
PSChildName   : DotNetTypes.format.ps1xml
PSDrive       : C
PSProvider    : Microsoft.PowerShell.Core\FileSystem
LastWriteTime : 4/24/2009 11:21:46 AM

However, I wouldn't normally access this property in this fashion. I find the output is way to verbose. I would do this:

PS> (Get-Item .\DotNetTypes.format.ps1xml).LastWriteTime

Friday, April 24, 2009 11:21:46 AM

As for guidance, I would say that you really need to implement this interface if you take the RegistryProvider approach but it is less important if you go the route the FileSystem provider went because you can easly access the properties directly of the objects returned by Get-Item/ChildItem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜