此範例示範如何覆寫 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-PSDriveCmdlet。覆寫 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