[玩转系统] 创建 Windows PowerShell 驱动器提供程序
作者:精品下载站 日期:2024-12-14 20:28:58 浏览:13 分类:玩电脑
创建 Windows PowerShell 驱动器提供程序
本主题介绍如何创建 Windows PowerShell 驱动器提供程序,该提供程序提供通过 Windows PowerShell 驱动器访问数据存储的方法。这种类型的提供程序也称为 Windows PowerShell 驱动器提供程序。提供商使用的 Windows PowerShell 驱动器提供连接到数据存储的方法。
此处描述的 Windows PowerShell 驱动提供程序提供对 Microsoft Access 数据库的访问。对于此提供程序,Windows PowerShell 驱动器代表数据库(可以向驱动器提供程序添加任意数量的驱动器),驱动器的顶级容器代表数据库中的表,容器的项目代表数据库中的表。表中的行。
定义 Windows PowerShell 提供程序类
您的驱动器提供程序必须定义一个派生自 System.Management.Automation.Provider.Drivecmdletprovider 基类的 .NET 类。以下是该驱动器提供程序的类定义:
[CmdletProvider("AccessDB", ProviderCapabilities.None)]
public class AccessDBProvider : DriveCmdletProvider
请注意,在此示例中,System.Management.Automation.Provider.Cmdletproviderattribute 属性指定提供程序的用户友好名称以及提供程序在命令处理期间向 Windows PowerShell 运行时公开的 Windows PowerShell 特定功能。提供程序功能的可能值由 System.Management.Automation.Provider.Providercapability 枚举定义。该驱动器提供商不支持任何这些功能。
定义基本功能
如设计您的 Windows PowerShell 提供程序中所述,System.Management.Automation.Provider.Drivecmdletprovider 类派生自 System.Management.Automation.Provider.Cmdletprovider 基类,该基类定义初始化和取消初始化提供程序所需的方法。要实现添加特定于会话的初始化信息和释放提供程序使用的资源的功能,请参阅创建基本 Windows PowerShell 提供程序。但是,大多数提供程序(包括此处描述的提供程序)可以使用 Windows PowerShell 提供的此功能的默认实现。
创建驱动器状态信息
所有 Windows PowerShell 提供程序都被视为无状态,这意味着您的驱动器提供程序需要创建 Windows PowerShell 运行时调用您的提供程序时所需的任何状态信息。
对于此驱动器提供程序,状态信息包括与数据库的连接,该连接作为驱动器信息的一部分保留。以下代码显示了如何将这些信息存储在描述驱动器的 System.Management.Automation.PSDriveinfo 对象中:
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
创建驱动器
要允许 Windows PowerShell 运行时创建驱动器,驱动器提供程序必须实现 System.Management.Automation.Provider.Drivecmdletprovider.Newdrive* 方法。以下代码显示了此驱动器提供程序的 System.Management.Automation.Provider.Drivecmdletprovider.Newdrive* 方法的实现:
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
您对此方法的重写应该执行以下操作:
验证 System.Management.Automation.PSDriveinfo.Root* 成员是否存在并且可以建立与数据存储的连接。
创建驱动器并填充连接成员,以支持
New-PSDrive
cmdlet。验证建议驱动器的 System.Management.Automation.PSDriveinfo 对象。
修改 System.Management.Automation.PSDriveinfo 对象,该对象用任何所需的性能或可靠性信息来描述驱动器,或者为使用驱动器的调用者提供额外的数据。
使用 System.Management.Automation.Provider.Cmdletprovider.WriteError 方法处理失败,然后返回
null
。此方法返回传递给该方法的驱动器信息或其特定于提供程序的版本。
将动态参数附加到 NewDrive
您的驱动器提供商支持的 New-PSDrive
cmdlet 可能需要其他参数。要将这些动态参数附加到 cmdlet,提供程序将实现 System.Management.Automation.Provider.Drivecmdletprovider.Newdrivedynamicparameters* 方法。此方法返回一个对象,该对象具有与 cmdlet 类或 System.Management.Automation.Runtimedefineparameterdictionary 对象类似的解析属性的属性和字段。
此驱动器提供程序不会重写此方法。但是,以下代码显示了此方法的默认实现:
移除驱动器
要关闭数据库连接,驱动器提供程序必须实现 System.Management.Automation.Provider.Drivecmdletprovider.Removedrive* 方法。此方法在清除任何特定于提供程序的信息后关闭与驱动器的连接。
以下代码显示了此驱动器提供程序的 System.Management.Automation.Provider.Drivecmdletprovider.Removedrive* 方法的实现:
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
如果可以删除驱动器,则该方法应返回通过 drive
参数传递给该方法的信息。如果无法删除驱动器,该方法应写入异常,然后返回 null
。如果您的提供程序不重写此方法,则此方法的默认实现仅返回作为输入传递的驱动器信息。
初始化默认驱动器
您的驱动器提供程序实现 System.Management.Automation.Provider.Drivecmdletprovider.Initializedefaultdrives* 方法来安装驱动器。例如,如果计算机加入域,Active Directory 提供程序可能会安装默认命名上下文的驱动器。
此方法返回有关已初始化驱动器的驱动器信息的集合,或者返回空集合。在 Windows PowerShell 运行时调用 System.Management.Automation.Provider.Cmdletprovider.Start* 方法来初始化提供程序之后,将调用此方法。
此驱动器提供程序不会重写 System.Management.Automation.Provider.Drivecmdletprovider.Initializedefaultdrives* 方法。但是,以下代码显示了默认实现,它返回一个空驱动器集合:
关于实施 InitializeDefaultDrives 的注意事项
所有驱动器提供商都应安装根驱动器以帮助用户发现。根驱动器可能会列出充当其他已安装驱动器的根的位置。例如,Active Directory 提供程序可能会创建一个驱动器,其中列出在根分布式系统环境 (DSE) 上的 namingContext
属性中找到的命名上下文。这有助于用户发现其他驱动器的安装点。
代码示例
有关完整的示例代码,请参阅 AccessDbProviderSample02 代码示例。
测试 Windows PowerShell 驱动器提供程序
当您的 Windows PowerShell 提供程序已向 Windows PowerShell 注册后,您可以通过在命令行上运行受支持的 cmdlet(包括通过派生提供的任何 cmdlet)来测试它。让我们测试示例驱动器提供程序。
运行
Get-PSProvider
cmdlet 检索提供程序列表,以确保 AccessDB 驱动提供程序存在:PS>
获取 PSProvider
将出现以下输出:
Name Capabilities Drives ---- ------------ ------ AccessDB None {} Alias ShouldProcess {Alias} Environment ShouldProcess {Env} FileSystem Filter, ShouldProcess {C, Z} Function ShouldProcess {function} Registry ShouldProcess {HKLM, HKCU}
通过访问操作系统管理工具的数据源部分,确保数据库存在数据库服务器名称(DSN)。在用户 DSN 表中,双击MS Access 数据库并添加驱动器路径
C:\ps\northwind.mdb
。使用示例驱动器提供程序创建新驱动器:
new-psdrive -name mydb -root c:\ps\northwind.mdb -psprovider AccessDb`
将出现以下输出:
Name Provider Root CurrentLocation ---- -------- ---- --------------- mydb AccessDB c:\ps\northwind.mdb
验证连接。由于连接被定义为驱动器的成员,因此您可以使用 Get-PDDrive cmdlet 检查它。
笔记
用户还不能作为驱动器与提供者进行交互,因为提供者需要容器功能来进行该交互。有关详细信息,请参阅创建 Windows PowerShell 容器提供程序。
PS> (get-psdrive mydb).connection
将出现以下输出:
ConnectionString : Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\ps\northwind.mdb ConnectionTimeout : 15 Database : c:\ps\northwind DataSource : ACCESS ServerVersion : 04.00.0000 Driver : odbcjt32.dll State : Open Site : Container :
删除驱动器并退出外壳:
PS> remove-psdrive mydb PS> exit
参见
创建 Windows PowerShell 提供程序
设计您的 Windows PowerShell 提供程序
创建基本 Windows PowerShell 提供程序
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag