Embedding PowerShell PSProvider * in * PowerShell

I want to implement PowerShell Provider in PowerShell.

I keep thinking that if I just define types and then import them into my session (import-module), I should have them available.

For example, this one does not work , but its path is along the path that I would like to implement.

I am clearly missing quite ... does 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 the import module, I try to create a drive "environment":

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

with:

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

Assuming the provider really worked, perhaps it will return a list of environments: dev, qa, staging, production.

Then I would like to be able to reuse 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]
+5
source share
2 answers

, Oisin, , , , How-To. , , ?;) codeplex: http://psprovider.codeplex.com/

+6

, , , . , msdn - , , , , :

, , Import-Module ( , add-type). :

1: Add-Type, .dll .

2: . msdn:

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

.

Cheers,

+2

All Articles