English 中文(简体)
实施 PowerShell Psprovider * in * PowerShell 执行 PowerShell 驱动器 * in * PowerShell 执行 PowerShell 驱动器
原标题:Implement PowerShell PSProvider *in* PowerShell

我期待在 " /强 " 电动 Shell 中执行一个 " 强 " 电动 Shell 供应商。

我不断想,如果我只是定义类型, 然后把它们输入我的会话(进口模块), 我应该可以让它们可用。

例如,这个“强”并不起作用

我显然错过了相当多... 有人知道这是否可能吗?

# 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

进口模块后, 我尝试创建驱动器“ 环境 ” :

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

存在错误 :

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

假设提供商实际工作,也许可以让它返回一份环境清单:dev、qa、中转、生产。

那么我想通过下列方式重新使用这个:

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

environments:>ls
dev
qa
staging
production

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

environmentsproduction:> cd dev
environmentsdev:> [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. Or maybe what to avoid? ;) You can find it on codeplex: http://psprovider.codeplex.com/

问题回答

我知道你问了这个问题已有一段时间了,但我一直在自己寻找同样的答案。 正因为如此,重读Mssdn的样本最终得到了我的答案,而且考虑到我所抱有的挫折性商数,我想我同意:

含有供应商的组件需要使用进口模块(而不仅仅是包含添加类型声明的模块)进口。

Option 1: Use the parameter of Add-Type that builds the runtime assembly as a .dll file and import the file.

Option 2: Import the runtime assembly from memory. This is how I did that with the standard msdn samples:

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

在过滤器中用您自己的过滤器替换“ 提供者” 名称 。

Cheers, Fred





相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签