使用 Import-DSCResource

Import-DSCResource 是一个动态关键字,只能在配置脚本块中使用,以导入配置中所需的任何资源。 下的 $PSHOME 资源会自动导入,但显式导入 配置中使用的所有资源仍被视为最佳实践。

Import-DSCResource 语法如下所示。 按名称指定模块时,需要在新行中列出每个模块。

Import-DscResource [-Name <ResourceName(s)>] [-ModuleName <ModuleName>] [-ModuleVersion <ModuleVersion>]

参数

  • -Name - 必须导入的 DSC 资源名称。 如果指定了模块名称,则命令将在此模块中搜索这些 DSC 资源;否则,该命令将在所有 DSC 资源路径中搜索 DSC 资源。 支持通配符。
  • -ModuleName - 模块名称或模块规范。 如果指定要从模块导入的资源,则该命令将尝试仅导入这些资源。 如果仅指定模块,则该命令将导入模块中的所有 DSC 资源。
  • -ModuleVersion - 从 PowerShell 5.0 开始,可以指定配置应使用的模块版本。 有关详细信息,请参阅 导入已安装资源的特定版本
Import-DscResource -ModuleName xActiveDirectory

示例:在配置中使用 Import-DSCResource

Configuration MSDSCConfiguration
{
    # Search for and imports Service, File, and Registry from the module PSDesiredStateConfiguration.
    Import-DSCResource -ModuleName PSDesiredStateConfiguration -Name Service, File, Registry

    # Search for and import Resource1 from the module that defines it.
    # If only –Name parameter is used then resources can belong to different PowerShell modules as well.
    # TimeZone resource is from the ComputerManagementDSC module which is not installed by default.
    # As a best practice, list each requirement on a different line if possible.  This makes reviewing
    # multiple changes in source control a bit easier.
    Import-DSCResource -Name File
    Import-DSCResource -Name TimeZone

    # Search for and import all DSC resources inside the module PSDesiredStateConfiguration.
    # When specifying the modulename parameter, it is a requirement to list each on a new line.
    Import-DSCResource -ModuleName PSDesiredStateConfiguration
    # In PowerShell 5.0 and later, you can specify a ModuleVersion parameter
    Import-DSCResource -ModuleName ComputerManagementDsc -ModuleVersion 6.0.0.0
...

注释

不支持在同一命令中为资源名称和模块名称指定多个值。 如果多个模块中存在相同的资源,它可以对从哪个模块加载哪个资源具有不确定的行为。 以下命令在编译过程中会导致错误。

Import-DscResource -Name UserConfigProvider*,TestLogger1 -ModuleName UserConfigProv,PsModuleForTestLogger

仅使用 Name 参数时要考虑的事项:

  • 这是一项资源密集型作,具体取决于机器上安装的模块数量。
  • 它将加载找到的第一个具有给定名称的资源。 如果安装了多个同名资源,则可能会加载错误的资源。

建议的用法是使用参数指定–ModuleName-Name,如下所述。

这种用法具有以下好处:

  • 它通过限制指定资源的搜索范围来减少对性能的影响。
  • 它显式定义了定义资源的模块,确保加载正确的资源。

注释

在 PowerShell 5.0 中,DSC 资源可以有多个版本,并且版本可以并排安装在计算机上。 这是通过在同一模块文件夹中包含多个版本的资源模块来实现的。 有关详细信息,请参阅使用具有多个版本的资源。

具有 Import-DSCResource 的智能感知

在 ISE 中创作 DSC 配置时,PowerShell 为资源和资源属性提供 IntelliSense。 模块路径下的 $pshome 资源定义会自动加载。 使用关键字导入 Import-DSCResource 资源时,将添加指定的资源定义,并展开 Intellisense 以包含导入资源的架构。

ISE中DSC资源的Intellisense

注释

从 PowerShell 5.0 开始,选项卡补全已添加到 DSC 资源及其属性的 ISE 中。 有关详细信息,请参阅资源

编译配置时,PowerShell 使用导入的资源定义来验证配置中的所有资源块。 使用资源的架构定义验证每个资源块,以符合以下规则。

  • 仅使用架构中定义的属性。
  • 每个属性的数据类型都是正确的。
  • 键属性被指定。
  • 不使用只读属性。
  • 值映射类型的验证。

请考虑以下配置:

Configuration SchemaValidationInCorrectEnumValue
{
    # It is best practice to explicitly import all resources used in your Configuration.
    # This includes resources that are imported automatically, like WindowsFeature.
    Import-DSCResource -Name WindowsFeature
    Node localhost
    {
        WindowsFeature ROLE1
        {
            Name = "Telnet-Client"
            Ensure = "Invalid"
        }
    }
}

编译此配置会导致错误。

PSDesiredStateConfiguration\WindowsFeature: At least one of the values 'Invalid' is not supported or
valid for property 'Ensure' on class 'WindowsFeature'. Please specify only supported values:
Present, Absent.

Intellisense 和架构验证允许在分析和编译期间捕获更多错误,从而避免运行时出现复杂情况。

注释

每个 DSC 资源都可以有一个名称,以及资源架构定义的 FriendlyName 。 以下是“MSFT_ServiceResource.shema.mof”的前两行。

[ClassVersion("1.0.0"),FriendlyName("Service")]
class MSFT_ServiceResource : OMI_BaseResource

在配置中使用此资源时,可以指定 MSFT_ServiceResource服务

PowerShell v4 和 v5 差异

在 PowerShell 4.0 与 PowerShell 5.0 及更高版本中创作配置时,会看到多个差异。 本节将重点介绍您看到的与本文相关的差异。

多个资源版本

PowerShell 4.0 不支持并排安装和使用多个版本的资源。 如果您发现将资源导入配置时出现问题,请确保您只安装了一个版本的资源。

在下图中,安装了两个版本的 xPSDesiredStateConfiguration 模块。

文件夹中安装了多个资源版本

将所需模块版本的内容复制到模块目录的顶层。

将所需版本复制到顶级模块目录

资源位置

创作和编译配置时,资源可以存储在 PSModulePath 指定的任何目录中。 在 PowerShell 4.0 中,LCM 要求将所有 DSC 资源模块存储在“Program Files\WindowsPowerShell\Modules”或 $pshome\Modules. 从 PowerShell 5.0 开始,此要求已被删除,并且资源模块可以存储在 指定的任何目录 PSModulePath中。

添加了 ModuleVersion

从 PowerShell 5.0 开始,该 -ModuleVersion 参数允许指定要在配置中使用的模块版本。

另请参阅