PSProvider development: Get-ChildItem calls GetItem

63 Views Asked by At

I have a class that extends ContainerCmdletProvider, in which I've overridden the following methods: NewDrive, IsValidPath, ItemExists, HasChildItems, GetItem and GetChildItems.

I can create a new drive (New-PSDrive -Name test ...), and successfully the child items from the root (Get-ChildItem test:). But when I try it on a sub-path that I know exists and has children (Get-ChildItem test:path) I get the same result as if I had run Get-Item test:path.

Using a debugger and setting breakpoints I can see which methods are called.
For Get-ChildItem test: it's, ItemExists, GetChildItems.
For Get-ChildItem test:path it's, ItemExists, GetItem.

Is this the expected behaviour? If so, how can I know the user's intention (viewing the item or its children)?
I'm surprised HasChildItems isn't called.

To output the test:path item, from both GetChildItems and GetItem, I use WriteItemObject(item, itemPath, true) passing the isContainer flag as true.

I've been following the How to Create a Windows PowerShell Provider guide from Microsoft. I'm targeting PowerShell Core (I'm on version 7.3.7), but this is the only guide I've found.

1

There are 1 best solutions below

0
mklement0 On BEST ANSWER

Per your feedback, implementing a navigation provider rather than a container provider solved your problem, based on the following distinction:

Implementing PowerShell providers that support hierarchies comes in two flavors:

  • Container providers:

    • Container providers only support a two-level hierarchy: containers at the root level whose children must be non-container items.
  • Navigation providers:

    • Navigation providers support arbitrarily deep hierarchies, where every level may have a mix of container and non-container children.
      (An example of such a provider is the file-system provider, whose container items are directories, and whose non-container items are files).