Reputation: 167
I want to create a PowerShell Provider that will work like a directory structure. The root is a web address that returns a text file. This file has a list of items. When each of these items is appended to the end of the original web address, I get another file with another list of items. This goes on recursively until the file does not return any item. So the structure is like:
root: 1.2.3.4/test/ -> returns file0
file0: item1, item2, item3
1.2.3.4/test/item1 -> returns file1
1.2.3.4/test/item2 -> returns file2
1.2.3.4/test/item3 -> returns file3
file1: item4, item5
file2: item6
file3: <empty>
Since I want to create a navigation like structure, I extended the NavigationCmdletProvider
public class TESTProvider : NavigationCmdletProvider
I am able to create the new PSDrive as follows:
PS c:\> New-PSDrive -Name dr1 -PSProvider TestProvider -Root http://1.2.3.4/v1
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- -------------------
dr1 TestProvider http://1.2.3.4/v1
But when I 'cd' to that drive, I get an error:
PS c:\> cd dr1:
cd : Cannot find path 'dr1:\' because it does not exist.
At line:1 char:1
+ cd dr1:
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (dr1:\:String) [Set-Location], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
What method do I have to implement/override to show the prompt as PS dr1:> when I do cd dr1:?
(After this I understand that I will have to override GetChildItems(string path, bool recurse)
to get item1, item2, item3 listed.)
Upvotes: 4
Views: 783
Reputation: 38077
I have found that implementing IsValidPath
, ItemExists
, IsItemContainer
, and GetChildren
get you to a working state. This is what I usually start with when I am implementing a navigation provider:
[CmdletProvider("MyPowerShellProvider", ProviderCapabilities.None)]
public class MyPowerShellProvider : NavigationCmdletProvider
{
protected override bool IsValidPath(string path)
{
return true;
}
protected override Collection<PSDriveInfo> InitializeDefaultDrives()
{
PSDriveInfo drive = new PSDriveInfo("MyDrive", this.ProviderInfo, "", "", null);
Collection<PSDriveInfo> drives = new Collection<PSDriveInfo>() {drive};
return drives;
}
protected override bool ItemExists(string path)
{
return true;
}
protected override bool IsItemContainer(string path)
{
return true;
}
protected override void GetChildItems(string path, bool recurse)
{
WriteItemObject("Hello", "Hello", true);
}
}
Upvotes: 3