Invoke-DscResource

Desired State Configuration (DSC) 资源运行指定 PowerShell 的方法。

语法

Invoke-DscResource
      [-Name] <String>
      [[-ModuleName] <ModuleSpecification>]
      [-Method] <String>
      [-Property] <Hashtable>
      [<CommonParameters>]

说明

Invoke-DscResource cmdlet 运行指定的 PowerShell 所需状态配置 (DSC) 资源的方法。

此 cmdlet 直接调用 DSC 资源,而无需创建配置文档。 使用此 cmdlet,配置管理产品可以使用 DSC 资源管理 Windows 或 Linux。

注意

在 PSDesiredStateConfiguration 2.0.6 之前,在 PowerShell 7 中使用 Invoke-DscResource 需要启用 PowerShell 实验性功能。 若要在版本 2.0.0 到 2.0.5 中使用 cmdlet,必须使用以下命令启用它。

Enable-ExperimentalFeature PSDesiredStateConfiguration.InvokeDscResource

示例

示例 1:通过指定资源的必需属性来调用其 Set 方法

此示例调用名为 WindowsProcess 的资源的 Set 方法,并提供必需的 PathArguments 属性来启动指定的 Windows 进程。

Invoke-DscResource -Name WindowsProcess -Method Set -ModuleName PSDesiredStateConfiguration -Property @{
  Path      = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
  Arguments = ''
}

示例 2:为指定模块调用资源的 Test 方法

此示例调用名为 WindowsProcess 的资源的 Test 方法,该资源位于名为 PSDesiredStateConfiguration 的模块中。

$SplatParam = @{
    Name       = 'WindowsProcess'
    ModuleName = 'PSDesiredStateConfiguration'
    Method     = 'Test'
    Property   = @{
        Path      = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
        Arguments = ''
    }
}

Invoke-DscResource @SplatParam

参数

-Method

指定此 cmdlet 调用的资源的方法。 此参数的可接受值为: GetSetTest

Type:String
Accepted values:Get, Set, Test
Position:2
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-ModuleName

指定提供要调用的指定 DSC 资源的模块的名称。

Type:ModuleSpecification
Position:1
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Name

指定要调用的 DSC 资源的名称。

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Property

分别在哈希表中将资源属性名称及其值指定为键和值。

Type:Hashtable
Position:3
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

输入

String

ModuleSpecification

输出

Object

备注

  • 在 Windows PowerShell 5.1 资源在系统上下文下运行,除非使用 PsDscRunAsCredential 键通过用户上下文指定。 在 PowerShell 7.0 中,资源在用户的上下文中运行,并且不再支持 PsDscRunAsCredential 。 使用此键会导致 cmdlet 引发异常。

  • 从 PowerShell 7 起, Invoke-DscResource 不再支持调用 WMI DSC 资源。 这包括 PSDesiredStateConfiguration 中的文件和日志资源 。