Windows PowerShell 提供者快速入門
本主題說明如何建立具有建立新磁碟驅動器基本功能的 Windows PowerShell 提供者。 如需提供者的一般資訊,請參閱 Windows PowerShell 提供者概觀。 如需具有更完整功能的提供者範例,請參閱 提供者範例。
撰寫基本提供者
Windows PowerShell 提供者最基本的功能是建立和移除磁碟驅動器。 在此範例中,我們會實作 System.Management.Automation.Provider.DriveCmdletProvider.NewDrive* 和 System.Management.Automation.Provider.DriveCmdletProvider.RemoveDrive*System.Management.Automation.Provider.DriveCmdletProvider 類別的方法。 您也會瞭解如何宣告提供者類別。
當您撰寫提供者時,可以指定提供者可用時自動建立的預設磁碟驅動器。 您也會定義方法來建立使用該提供者的新磁碟驅動器。
本主題中提供的範例是以 AccessDBProviderSample02 範例為基礎,此範例是代表 Access 資料庫做為 Windows PowerShell 磁碟驅動器之較大範例的一部分。
設定專案
在 Visual Studio 中,建立名為 AccessDBProviderSample 的類別庫專案。 完成下列步驟來設定專案,讓 Windows PowerShell 在建置和啟動專案時,會將提供者載入工作階段中。
設定提供者專案
將 System.Management.Automation 元件新增為項目的參考。
按兩下 [Project > AccessDBProviderSample 屬性] > [偵錯]。 在 Start 專案中,按兩下 [啟動外部程式],然後流覽至 Windows PowerShell 可執行檔(通常是 C:\Windows\System32\WindowsPowerShell\v1.0\.powershell.exe)。
在 [[開始選項]下,在 [命令行 自變數] 方塊中輸入下列專案:
-NoExit -Command "[Reflection.Assembly]::LoadFrom(AccessDBProviderSample.dll' ) | Import-Module"
宣告提供者類別
我們的提供者衍生自 System.Management.Automation.Provider.DriveCmdletProvider 類別。 大部分提供實際功能的提供者(存取和作專案、流覽數據存放區,以及取得和設定專案的內容)衍生自 System.Management.Automation.Provider.NavigationCmdletProvider 類別。
除了指定類別衍生自 System.Management.Automation.Provider.DriveCmdletProvider之外,您必須使用 System.Management.Automation.Provider.CmdletProviderAttribute 來裝飾它,如範例所示。
namespace Microsoft.Samples.PowerShell.Providers
{
using System;
using System.Data;
using System.Data.Odbc;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Provider;
#region AccessDBProvider
[CmdletProvider("AccessDB", ProviderCapabilities.None)]
public class AccessDBProvider : DriveCmdletProvider
{
}
}
實作 NewDrive
當使用者呼叫 Microsoft.PowerShell.Commands.NewPSDriveCommand Cmdlet 時,Windows PowerShell 引擎會呼叫 System.Management.Automation.Provider.DriveCmdletProvider.NewDrive* 方法。 PSDriveInfo 參數是由 Windows PowerShell 引擎傳遞,方法會將新磁碟驅動器傳回至 Windows PowerShell 引擎。 這個方法必須在上面建立的 類別內宣告。
方法會先檢查以確定傳入的磁碟驅動器物件和磁碟驅動器根目錄都存在,如果其中一個不存在,則會傳回 null
。 然後,它會使用內部類別 AccessDBPSDriveInfo 的建構函式來建立新的磁碟驅動器,以及該磁碟驅動器所代表之 Access 資料庫的連線。
protected override PSDriveInfo NewDrive(PSDriveInfo drive)
{
// Check if the drive object is null.
if (drive == null)
{
WriteError(new ErrorRecord(
new ArgumentNullException("drive"),
"NullDrive",
ErrorCategory.InvalidArgument,
null));
return null;
}
// Check if the drive root is not null or empty
// and if it is 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;
}
以下是 AccessDBPSDriveInfo 內部類別,其中包含用來建立新磁碟驅動器的建構函式,並包含磁碟驅動器的狀態資訊。
internal class AccessDBPSDriveInfo : PSDriveInfo
{
/// <summary>
/// A reference to the connection to the database.
/// </summary>
private OdbcConnection connection;
/// <summary>
/// Initializes a new instance of the AccessDBPSDriveInfo class.
/// The constructor takes a single argument.
/// </summary>
/// <param name="driveInfo">Drive defined by this provider</param>
public AccessDBPSDriveInfo(PSDriveInfo driveInfo)
: base(driveInfo)
{
}
/// <summary>
/// Gets or sets the ODBC connection information.
/// </summary>
public OdbcConnection Connection
{
get { return this.connection; }
set { this.connection = value; }
}
}
實作 RemoveDrive
當使用者呼叫 Microsoft.PowerShell.Commands.RemovePSDriveCommand Cmdlet 時,Windows PowerShell 引擎會呼叫 System.Management.Automation.Provider.DriveCmdletProvider.RemoveDrive* 方法。 這個提供者中的方法會關閉 Access 資料庫的連線。
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 the ODBC connection to the drive.
AccessDBPSDriveInfo accessDBPSDriveInfo = drive as AccessDBPSDriveInfo;
if (accessDBPSDriveInfo == null)
{
return null;
}
accessDBPSDriveInfo.Connection.Close();
return accessDBPSDriveInfo;
}