AccessDBProviderSample02AccessDBProviderSample02
En este ejemplo se muestra cómo sobrescribir los métodos System. Management. Automation. Provider. Drivecmdletprovider. newdrive * y System. Management. Automation. Provider. Drivecmdletprovider. Removedrive * para admitir llamadas a los New-PSDrive
Remove-PSDrive
cmdlets y.This sample shows how to overwrite the System.Management.Automation.Provider.Drivecmdletprovider.Newdrive* and System.Management.Automation.Provider.Drivecmdletprovider.Removedrive* methods to support calls to the New-PSDrive
and Remove-PSDrive
cmdlets. La clase de proveedor de este ejemplo se deriva de la clase System. Management. Automation. Provider. Drivecmdletprovider .The provider class in this sample derives from the System.Management.Automation.Provider.Drivecmdletprovider class.
MuestraDemonstrates
Importante
Lo más probable es que la clase de proveedor derive de una de las siguientes clases y, posiblemente, implemente otras interfaces de proveedor:Your provider class will most likely derive from one of the following classes and possibly implement other provider interfaces:
- Clase System. Management. Automation. Provider. Itemcmdletprovider .System.Management.Automation.Provider.Itemcmdletprovider class. Vea AccessDBProviderSample03.See AccessDBProviderSample03.
- Clase System. Management. Automation. Provider. Containercmdletprovider .System.Management.Automation.Provider.Containercmdletprovider class. Vea AccessDBProviderSample04.See AccessDBProviderSample04.
- Clase System. Management. Automation. Provider. Navigationcmdletprovider .System.Management.Automation.Provider.Navigationcmdletprovider class. Vea AccessDBProviderSample05.See AccessDBProviderSample05.
Para obtener más información sobre cómo elegir la clase de proveedor que se va a derivar en función de las características del proveedor, vea diseñar un proveedor de Windows PowerShell.For more information about choosing which provider class to derive from based on provider features, see Designing Your Windows PowerShell Provider.
En este ejemplo se muestra lo siguiente:This sample demonstrates the following:
Declarar el
CmdletProvider
atributo.Declaring theCmdletProvider
attribute.Definir una clase de proveedor que controla desde la clase System. Management. Automation. Provider. Drivecmdletprovider .Defining a provider class that drives from the System.Management.Automation.Provider.Drivecmdletprovider class.
Sobrescribir el método System. Management. Automation. Provider. Drivecmdletprovider. newdrive * para permitir la creación de nuevas unidades.Overwriting the System.Management.Automation.Provider.Drivecmdletprovider.Newdrive* method to support creating new drives. (En este ejemplo no se muestra cómo agregar parámetros dinámicos al
New-PSDrive
cmdlet).(This sample does not show how to add dynamic parameters to theNew-PSDrive
cmdlet.)Sobrescribir el método System. Management. Automation. Provider. Drivecmdletprovider. Removedrive * para admitir la eliminación de unidades existentes.Overwriting the System.Management.Automation.Provider.Drivecmdletprovider.Removedrive* method to support removing existing drives.
EjemploExample
En este ejemplo se muestra cómo sobrescribir los métodos System. Management. Automation. Provider. Drivecmdletprovider. newdrive * y System. Management. Automation. Provider. Drivecmdletprovider. Removedrive * .This sample shows how to overwrite the System.Management.Automation.Provider.Drivecmdletprovider.Newdrive* and System.Management.Automation.Provider.Drivecmdletprovider.Removedrive* methods. Para este proveedor de ejemplo, cuando se crea una unidad, la información de conexión se almacena en un AccessDBPsDriveInfo
objeto.For this sample provider, when a drive is created its connection information is stored in an AccessDBPsDriveInfo
object.
using System;
using System.IO;
using System.Data;
using System.Data.Odbc;
using System.Management.Automation;
using System.Management.Automation.Provider;
using System.ComponentModel;
namespace Microsoft.Samples.PowerShell.Providers
{
#region AccessDBProvider
/// <summary>
/// A PowerShell Provider which acts upon a access data store.
/// </summary>
/// <remarks>
/// This example only demonstrates the drive overrides
/// </remarks>
[CmdletProvider("AccessDB", ProviderCapabilities.None)]
public class AccessDBProvider : DriveCmdletProvider
{
#region Drive Manipulation
/// <summary>
/// Create a new drive. Create a connection to the database file and set
/// the Connection property in the PSDriveInfo.
/// </summary>
/// <param name="drive">
/// Information describing the drive to add.
/// </param>
/// <returns>The added drive.</returns>
protected override PSDriveInfo NewDrive(PSDriveInfo drive)
{
// check if drive object is null
if (drive == null)
{
WriteError(new ErrorRecord(
new ArgumentNullException("drive"),
"NullDrive",
ErrorCategory.InvalidArgument,
null)
);
return null;
}
// check if drive root is not null or empty
// and if its an existing file
if (String.IsNullOrEmpty(drive.Root) || (File.Exists(drive.Root) == false))
{
WriteError(new ErrorRecord(
new ArgumentException("drive.Root"),
"NoRoot",
ErrorCategory.InvalidArgument,
drive)
);
return null;
}
// create a new drive and create an ODBC connection to the new drive
AccessDBPSDriveInfo accessDBPSDriveInfo = new AccessDBPSDriveInfo(drive);
OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
builder.Driver = "Microsoft Access Driver (*.mdb)";
builder.Add("DBQ", drive.Root);
OdbcConnection conn = new OdbcConnection(builder.ConnectionString);
conn.Open();
accessDBPSDriveInfo.Connection = conn;
return accessDBPSDriveInfo;
} // NewDrive
/// <summary>
/// Removes a drive from the provider.
/// </summary>
/// <param name="drive">The drive to remove.</param>
/// <returns>The drive removed.</returns>
protected override PSDriveInfo RemoveDrive(PSDriveInfo drive)
{
// check if drive object is null
if (drive == null)
{
WriteError(new ErrorRecord(
new ArgumentNullException("drive"),
"NullDrive",
ErrorCategory.InvalidArgument,
drive)
);
return null;
}
// close ODBC connection to the drive
AccessDBPSDriveInfo accessDBPSDriveInfo = drive as AccessDBPSDriveInfo;
if (accessDBPSDriveInfo == null)
{
return null;
}
accessDBPSDriveInfo.Connection.Close();
return accessDBPSDriveInfo;
} // RemoveDrive
#endregion Drive Manipulation
} // AccessDBProvider
#endregion AccessDBProvider
#region AccessDBPSDriveInfo
/// <summary>
/// Any state associated with the drive should be held here.
/// In this case, it's the connection to the database.
/// </summary>
internal class AccessDBPSDriveInfo : PSDriveInfo
{
private OdbcConnection connection;
/// <summary>
/// ODBC connection information.
/// </summary>
public OdbcConnection Connection
{
get { return connection; }
set { connection = value; }
}
/// <summary>
/// Constructor that takes one argument
/// </summary>
/// <param name="driveInfo">Drive provided by this provider</param>
public AccessDBPSDriveInfo(PSDriveInfo driveInfo)
: base(driveInfo)
{ }
} // class AccessDBPSDriveInfo
#endregion AccessDBPSDriveInfo
}
Consulte tambiénSee Also
Diseño del proveedor de Windows PowerShellDesigning Your Windows PowerShell Provider