当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 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,并将提供程序加载到会话中。

配置提供者项目
  1. 添加 System.Management.Automation 程序集作为对项目的引用。

  2. 单击项目 > AccessDBProviderSample 属性 > 调试。在启动项目中,单击启动外部程序,然后导航到 Windows PowerShell 可执行文件(通常为 c:\Windows\System32\WindowsPowerShell\v1.0\.powershell.exe) 。

  3. 启动选项下,在命令行参数框中输入以下内容:-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;
    }

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯