이 샘플에서는 system.Management.Automation.Provider.DriveCmdletProvider.NewDrive* 및 System.Management.Automation.Provider.DriveCmdletProvider.RemoveDrive* 메서드를 덮어쓰고 New-PSDrive
및 Remove-PSDrive
cmdlet에 대한 호출을 지원하는 방법을 보여 줍니다. 이 샘플의 공급자 클래스는 System.Management.Automation.Provider.DriveCmdletProvider 클래스에서 파생됩니다.
입증합니다
중요합니다
공급자 클래스는 다음 클래스 중 하나에서 파생되고 다른 공급자 인터페이스를 구현할 가능성이 높습니다.
- system.Management.Automation.Provider.ItemCmdletProvider 클래스를. AccessDBProviderSample03 참조하세요.
- system.Management.Automation.Provider.ContainerCmdletProvider 클래스를. AccessDBProviderSample04 참조하세요.
- system.Management.Automation.Provider.NavigationCmdletProvider 클래스를. AccessDBProviderSample05 참조하세요.
공급자 기능을 기반으로 파생할 공급자 클래스를 선택하는 방법에 대한 자세한 내용은 windows PowerShell 공급자 디자인참조하세요.
이 샘플에서는 다음을 보여 줍니다.
CmdletProvider
특성을 선언합니다.System.Management.Automation.Provider.DriveCmdletProvider 클래스에서 구동하는 공급자 클래스를 정의합니다.
새 드라이브 만들기를 지원하기 위해 System.Management.Automation.Provider.DriveCmdletProvider.NewDrive* 메서드를 덮어씁니다. (이 샘플에서는
New-PSDrive
cmdlet에 동적 매개 변수를 추가하는 방법을 보여주지 않습니다.)기존 드라이브 제거를 지원하기 위해 System.Management.Automation.Provider.DriveCmdletProvider.RemoveDrive* 메서드를 덮어씁니다.
예시
이 샘플에서는 System.Management.Automation.Provider.DriveCmdletProvider.NewDrive* 및 System.Management.Automation.Provider.DriveCmdletProvider.RemoveDrive* 메서드를 덮어쓰는 방법을 보여 줍니다. 이 샘플 공급자의 경우 드라이브가 만들어지면 연결 정보가 AccessDBPsDriveInfo
개체에 저장됩니다.
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
}
또한 참조하십시오
System.Management.Automation.Provider.ItemCmdletProvider
System.Management.Automation.Provider.ContainerCmdletProvider
System.Management.Automation.Provider.NavigationCmdletProvider
Windows PowerShell 공급자 디자인하는
PowerShell