简体   繁体   English

在* PowerShell中实现PowerShell PSProvider *

[英]Implement PowerShell PSProvider *in* PowerShell

I'm looking to implement a PowerShell Provider in PowerShell. 我想 PowerShell中实现PowerShell提供程序。

I keep thinking that if I just define the types, then import them into my session (import-module), I should be able to have them available. 我一直在想,如果我只是定义类型,然后将它们导入我的会话(import-module),我应该可以使它们可用。

For example, this does not work but its along the path of what I'd like to implement. 例如,这不起作用,但它沿着我想要实现的路径。

I'm obviously missing quite a bit...anyone know if this is possible? 我显然很想念......有人知道这是否可能?

# EnvironmentProvider.ps1
    $reference_assemblies = (

      "System.Management.Automation, Version=1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
    #  "System.Configuration.Install, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    )

    $source = @"

    namespace Providers
    {

    using System.Management.Automation;
    using System.Management.Automation.Provider;


        [CmdletProvider("Environments", ProviderCapabilities.None)]
        public class EnvironmentProvider : DriveCmdletProvider
        {
            protected override PSDriveInfo NewDrive(PSDriveInfo drive)
            {
                return new EnvironmentDriveInfo(drive);
            }

            protected override object NewDriveDynamicParameters()
            {
                return base.NewDriveDynamicParameters();
            }

        }

         public class EnvironmentDriveInfo : PSDriveInfo
        {
            public EnvironmentDriveInfo(PSDriveInfo driveInfo) : base(driveInfo)
            {
            }
        }


    }
    "@

    # -ea silentlycontinue in case its already loaded
    #
    add-type -referencedassemblies $referenced_assemblies -typedefinition $source -language CSharp -erroraction silentlycontinue

After import-module, I try to create the drive "environments": 在import-module之后,我尝试创建驱动器“环境”:

new-psdrive -psprovider Environments -name "Environments" -root ""

errors with: 错误:

New-PSDrive : Cannot find a provider with the name 'Environments'.

Assuming the provider actually worked, maybe have it return a list of environments: dev, qa, staging, production. 假设提供者实际工作,可能让它返回一个环境列表:dev,qa,staging,production。

Then I'd like to be able to re-use this through: 然后我希望能够通过以下方式重复使用:

c:\adminlib>import-module .\EnvironmentProvider.ps1
c:\adminlib>environments:

environments:>ls
dev
qa
staging
production

environments:> cd production
environments\production> [execute actions against production]

environments\production:> cd dev
environments\dev:> [execute actions against dev, etc]

I would strongly recommend looking at the stuff Oisin wrote, suspect for people like you, who can grab their head around it, that could be very good reference on how-to. 我强烈建议你看看Oisin所写的东西,怀疑像你这样的人,他们可以抓住它,这可能是非常好的参考方法。 Or maybe what to avoid? 或者可能要避免什么? ;) You can find it on codeplex: http://psprovider.codeplex.com/ ;)你可以在codeplex上找到它: http//psprovider.codeplex.com/

I know it's been some time since you asked the question, but I've been searching for that same answer myself. 我知道你问这个问题已经有一段时间了,但我一直在寻找同样的答案。 As it happens, re-reading the Samples in msdn finally got me my answer, and given the frustration quotient I thought I'd share: 事实上,重新阅读msdn中的样本终于得到了我的答案,并且考虑到我认为我会分享的沮丧商数:

The assembly containing the provider needs to be imported using Import-Module (not merely the module containing the add-type declaration). 需要使用Import-Module导入包含提供程序的程序集(不仅仅是包含add-type声明的模块)。 This can be done using two ways: 这可以通过两种方式完成:

Option 1: Use the parameter of Add-Type that builds the runtime assembly as a .dll file and import the file. 选项1:使用Add-Type参数将构建运行时程序集作为.dll文件并导入文件。

Option 2: Import the runtime assembly from memory. 选项2:从内存中导入运行时程序集。 This is how I did that with the standard msdn samples: 这就是我使用标准msdn样本的方法:

[appdomain]::CurrentDomain.GetAssemblies() | Where {$_.ExportedTypes -ne $null} | Where {($_.ExportedTypes | Select -ExpandProperty "Name") -contains "AccessDBProvider"} | Import-Module

Replace the Provider name in the where filter with your own. 将where过滤器中的Provider名称替换为您自己的名称。

Cheers, Fred 干杯,弗雷德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM