このトピックでは、新しいドライブを作成するための基本的な機能を備える Windows PowerShell プロバイダーを作成する方法について説明します。 プロバイダーの一般的な情報については、「Windows PowerShell プロバイダーの概要」を参照してください。 より完全な機能を備えたプロバイダーの例については、「プロバイダー サンプル」を参照してください。
基本的なプロバイダーの記述
Windows PowerShell プロバイダーの最も基本的な機能は、ドライブの作成と削除です。 この例では、System.Management.Automation.Provider.DriveCmdletProvider クラスの system.Management.Automation.Provider.DriveCmdletProvider.NewDrive* および System.Management.Automation.Provider.DriveCmdletProvider.RemoveDrive* メソッドを実装します。 プロバイダー クラスを宣言する方法についても説明します。
プロバイダーを作成するときに、プロバイダーが使用可能になったときに自動的に作成される既定のドライブドライブを指定できます。 また、そのプロバイダーを使用する新しいドライブを作成するメソッドも定義します。
このトピックの例は、Access データベースを Windows PowerShell ドライブとして表す大規模なサンプルの一部である、AccessDBProviderSample02 サンプルに基づいています。
プロジェクトの設定
Visual Studio で、AccessDBProviderSample という名前のクラス ライブラリ プロジェクトを作成します。 プロジェクトをビルドして開始するときに、Windows PowerShell が起動し、プロバイダーがセッションに読み込まれるようにプロジェクトを構成するには、次の手順を実行します。
プロバイダー プロジェクトを構成する
System.Management.Automation アセンブリをプロジェクトへの参照として追加します。
[プロジェクト > AccessDBProviderSample プロパティ] > [デバッグ] をクリックします。 スタート プロジェクトで、[外部プログラム の開始]クリックし、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 の実装
System.Management.Automation.Provider.DriveCmdletProvider.NewDrive* メソッドは、ユーザーがプロバイダーの名前を指定する Microsoft.PowerShell.Commands.NewPSDriveCommand コマンドレットを呼び出すと、Windows PowerShell エンジンによって呼び出されます。 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 の実装
System.Management.Automation.Provider.DriveCmdletProvider.RemoveDrive* メソッドは、ユーザーが Microsoft.PowerShell.Commands.RemovePSDriveCommand コマンドレットを呼び出すと、Windows PowerShell エンジンによって呼び出されます。 このプロバイダーのメソッドは、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;
}
PowerShell