[玩转系统] Windows PowerShell 提供程序快速入门
作者:精品下载站 日期:2024-12-14 02:47:23 浏览:13 分类:玩电脑
Windows PowerShell 提供程序快速入门
本主题介绍如何创建具有创建新驱动器基本功能的 Windows PowerShell 提供程序。有关提供程序的一般信息,请参阅 Windows PowerShell 提供程序概述。有关具有更完整功能的提供程序的示例,请参阅提供程序示例。
编写一个基本的提供者
Windows PowerShell 提供程序最基本的功能是创建和删除驱动器。在此示例中,我们实现 System.Management.Automation.Provider.Drivecmdletprovider 类的 System.Management.Automation.Provider.Drivecmdletprovider.Newdrive* 和 System.Management.Automation.Provider.Drivecmdletprovider.Removedrive* 方法。您还将了解如何声明提供程序类。
当您编写提供程序时,您可以指定默认驱动器 - 当提供程序可用时自动创建的驱动器。您还可以定义一种方法来创建使用该提供程序的新驱动器。
本主题中提供的示例基于 AccessDBProviderSample02 示例,该示例是一个较大示例的一部分,该示例将 Access 数据库表示为 Windows PowerShell 驱动器。
设置项目
在 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' ) | 导入模块”
声明提供者类
我们的提供程序派生自 System.Management.Automation.Provider.Drivecmdletprovider 类。大多数提供实际功能(访问和操作项目、导航数据存储以及获取和设置项目内容)的提供程序都派生自 System.Management.Automation.Provider.Navigationcmdletprovider 类。
除了指定该类派生自 System.Management.Automation.Provider.Drivecmdletprovider 之外,还必须使用 System.Management.Automation.Provider.Cmdletprovider 属性对其进行修饰,如示例中所示。
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; }
}
}
实施删除驱动器
当用户调用 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;
}
猜你还喜欢
- 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