2014-09-02 115 views
4

我想创建一个PowerShell提供程序,它将像目录结构一样工作。 根是一个返回文本文件的网址。这个文件有一个项目列表。当这些项目中的每一个都附加到原始Web地址的末尾时,我会得到另一个包含另一个项目列表的文件。这将递归直到文件不返回任何项目。所以结构是这样的:自定义PowerShell提供程序实现

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> 

因为我想创建一个类似结构的导航,我延长了NavigationCmdletProvider

public class TESTProvider : NavigationCmdletProvider 

我能够创建新PSDrive来如下:

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 

但是,当我'cd'到该驱动器时,出现错误:

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 

我需要使用什么方法来实现/覆盖以显示提示信息:PS dr1:>当我执行cd dr1时:? (在此之后我明白,我将不得不重写GetChildItems(string path, bool recurse)得到项目1,项目2,项目3列出)。

+1

这不完全是您的问题的答案,但您应该查看https://github.com/beefarino/p2f/或https://github.com/beefarino/simplex - 他们都使构建提供者变得更简单。 – 2014-09-02 22:04:19

回答

3

我发现,实施IsValidPathItemExistsIsItemContainerGetChildren让你到工作状态。这是我通常从我实施导航提供商时开始的:

[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); 
    } 
} 
相关问题