การเริ่มต้นอย่างรวดเร็วของผู้ให้บริการ 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 ให้สร้างโครงการ Class Library ชื่อ AccessDBProviderSample ทำตามขั้นตอนต่อไปนี้เพื่อกำหนดค่าโปรเจ็กต์ของคุณเพื่อให้ Windows PowerShell เริ่มทำงาน และผู้ให้บริการจะถูกโหลดเข้าสู่เซสชันเมื่อคุณสร้างและเริ่มโปรเจ็กต์ของคุณ
กำหนดค่าโครงการผู้ให้บริการ
-
เพิ่มแอสเซมบลี System.Management.Automation เป็นการอ้างอิงถึงโครงการของคุณ
คลิก โครงการ > คุณสมบัติ AccessDBProviderSample > ดีบัก ใน เริ่มโครงการ คลิก เริ่มโปรแกรมภายนอก และไปที่ไฟล์ปฏิบัติการ Windows PowerShell (โดยทั่วไปคือ c:\Windows\System32\WindowsPowerShell\v1.0\.powershell.exe) .
ใต้ ตัวเลือกการเริ่มต้น ให้ป้อนข้อมูลต่อไปนี้ลงในช่อง อาร์กิวเมนต์บรรทัดคำสั่ง:
-noexit -command "[reflection.assembly]::loadFrom(AccessDBProviderSample.dll' ) |. นำเข้าโมดูล"
ประกาศคลาสผู้ให้บริการ
ผู้ให้บริการของเรามาจากคลาส 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* ถูกเรียกโดยกลไก Windows PowerShell เมื่อผู้ใช้เรียก Microsoft.PowerShell.Commands.NewPSDriveCommand cmdlet โดยระบุชื่อของผู้ให้บริการของคุณ พารามิเตอร์ 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* ถูกเรียกโดยกลไกจัดการ Windows PowerShell เมื่อผู้ใช้เรียก Microsoft.PowerShell.Commands.RemovePSDriveCommand cmdlet วิธีการในตัวให้บริการนี้จะปิดการเชื่อมต่อกับฐานข้อมูล 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;
}