Marko Stanojevic
Marko Stanojevic

Reputation: 468

Powershell Provider - GetItem Path Error - Custom File as Drive

I'm trying to emulate my custom project file as new PS Drive. I am trying to create my custom Powershell Provider that is derived from NavigationCmdletProvider. I have overridden PSDriveInfo to read and contain the project from the file and filepath is in the root of PSDriveInfo.

I can't override GetItem properly. What I want to do is use GetNamesFromPath(path, out tableName, out rowNumber) method. Since my custom project is basically dataset, I would like to use tableName to get the DataTable and rowNumber for ID of DataRow.

The problem is that I get the "path doesn't exist" kind of error. It doesn't event get into the overridden method. Am I missing something to override? The filepath doesn't exist really, but I simply need to handle the path and use WriteItemObject with what I want as object(s) returned, without checking is it valid path.

Edit 1: One thing I noticed is that it never gets into GetItem and therefore into IsValidPath. When I debug and use breakpoints, first I load the drive and then Set-Location to the drive, IsItemContainer is called (it has to be overridden for Set-Location to work).

GetItem and IsValidPath are not called at all, as if it checks for valid path before calling overridden method. Can NavigationCmdletProvider work with non-existing paths (except for the file itself), just work with strings that will manually be handled as paths would?

Upvotes: 0

Views: 306

Answers (2)

John Koerner
John Koerner

Reputation: 38077

Make sure you override the IsValidPath and ItemExists methods:

protected override bool IsValidPath(string path)
{
    return true;
}

protected override bool ItemExists(string path)
{
  return true;
}

Upvotes: 1

Marek Dzikiewicz
Marek Dzikiewicz

Reputation: 2884

If you are extending NavigationCmdletProvider then you should override IsValidPath, ItemExists, GetItem, GetChildItems and possibly other methods depending on what features you want to support for your PS drives.

The best way to find out which methods are missing implementation is to override all the virtual methods and put a breakpoint in each one. Then execute a cmdlet and see in the debugger what gets called and what are the parameter values.

Unfortunately, there isn't a lot of detailed documentation about implementing custom PowerShell providers. However, you can find a quite detailed tutorial on MSDN about this topic with a lot of source code examples. Additionally, you can take a look at the PowerShell VFS project - it's a wrapper around PowerShell provider API to make it easier to build complicated providers.

Upvotes: 1

Related Questions