获取 -Test-Set

适用于:Windows PowerShell 4.0、Windows PowerShell 5.0

PowerShell 所需状态配置是围绕 GetTestSet 进程构造的。 每个 DSC 资源 都包含用于完成其中每个作的方法。 在 配置中,定义资源块以填充键,这些键成为资源的 GetTestSet 方法的参数。

这是 服务 资源块的语法。 服务资源配置 Windows 服务。

Service [String] #ResourceName
{
    Name = [string]
    [BuiltInAccount = [string]{ LocalService | LocalSystem | NetworkService }]
    [Credential = [PSCredential]]
    [Dependencies = [string[]]]
    [DependsOn = [string[]]]
    [Description = [string]]
    [DisplayName = [string]]
    [Ensure = [string]{ Absent | Present }]
    [Path = [string]]
    [PsDscRunAsCredential = [PSCredential]]
    [StartupType = [string]{ Automatic | Disabled | Manual }]
    [State = [string]{ Running | Stopped }]
}

Service 资源的 GetTestSet 方法将具有接受这些值的参数块。

param
(
    [parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [System.String]
    $Name,

    [System.String]
    [ValidateSet("Automatic", "Manual", "Disabled")]
    $StartupType,

    [System.String]
    [ValidateSet("LocalSystem", "LocalService", "NetworkService")]
    $BuiltInAccount,

    [System.Management.Automation.PSCredential]
    [ValidateNotNull()]
    $Credential,

    [System.String]
    [ValidateSet("Running", "Stopped")]
    $State="Running",

    [System.String]
    [ValidateNotNullOrEmpty()]
    $DisplayName,

    [System.String]
    [ValidateNotNullOrEmpty()]
    $Description,

    [System.String]
    [ValidateNotNullOrEmpty()]
    $Path,

    [System.String[]]
    [ValidateNotNullOrEmpty()]
    $Dependencies,

    [System.String]
    [ValidateSet("Present", "Absent")]
    $Ensure="Present"
)

注释

用于定义资源的语言和方法决定了 如何定义 GetTestSet 方法。

由于 Service 资源只有一个必需的键 (),Name因此 Service 块资源可以像以下那样简单:

Configuration TestConfig
{
    Import-DSCResource -Name Service
    Node localhost
    {
        Service "MyService"
        {
            Name = "Spooler"
        }
    }
}

编译上述配置时,为键指定的值将存储在生成的文件中 .mof 。 有关详细信息,请参阅 MOF

instance of MSFT_ServiceResource as $MSFT_ServiceResource1ref
{
SourceInfo = "::5::1::Service";
 ModuleName = "PsDesiredStateConfiguration";
 ResourceID = "[Service]MyService";
 Name = "Spooler";

ModuleVersion = "1.0";

 ConfigurationName = "Test";

};

应用时,本地配置管理器 (LCM) 将从文件中.mof读取值“后台处理程序”,并将其传递给服务资源的“MyService”实例的 GetTestSet 方法的 Name 参数。

Get

资源的 Get 方法检索在目标节点上配置的资源状态。 此状态作为 哈希表返回。 哈希表的键将是资源接受的可配置值或参数。

Get 方法直接映射到 Get-DSCConfiguration cmdlet。 调用 Get-DSCConfiguration时,LCM 会运行当前应用配置中每个资源的 Get 方法。 LCM 使用存储在文件中的 .mof 键值作为每个相应资源实例的参数。

这是配置“后台处理程序”服务的 服务 资源的示例输出。

ConfigurationName    : Test
DependsOn            :
ModuleName           : PsDesiredStateConfiguration
ModuleVersion        : 1.1
PsDscRunAsCredential :
ResourceId           : [Service]Spooler
SourceInfo           :
BuiltInAccount       : LocalSystem
Credential           :
Dependencies         : {RPCSS, http}
Description          : This service spools print jobs and handles interaction with the printer.  If you turn off
                       this service, you won't be able to print or see your printers.
DisplayName          : Print Spooler
Ensure               :
Name                 : Spooler
Path                 : C:\WINDOWS\System32\spoolsv.exe
StartupType          : Automatic
State                : Running
Status               :
PSComputerName       :
CimClassName         : MSFT_ServiceResource

输出显示 可由服务 资源配置的当前值属性。

Service [String] #ResourceName
{
    Name = [string]
    [BuiltInAccount = [string]{ LocalService | LocalSystem | NetworkService }]
    [Credential = [PSCredential]]
    [Dependencies = [string[]]]
    [DependsOn = [string[]]]
    [Description = [string]]
    [DisplayName = [string]]
    [Ensure = [string]{ Absent | Present }]
    [Path = [string]]
    [PsDscRunAsCredential = [PSCredential]]
    [StartupType = [string]{ Automatic | Disabled | Manual }]
    [State = [string]{ Running | Stopped }]
}

测试

资源的 Test 方法确定目标节点当前是否符合资源的 所需状态Test 方法返回$true$false仅用于指示节点是否合规。 调用 Test-DSCConfiguration 时,LCM 会调用当前应用配置中每个资源的 Test 方法。 LCM 使用存储在“.mof”文件中的键值作为每个相应资源实例的参数。

如果任何单个资源的 Test 结果为 $falseTest-DSCConfiguration 则返回 $false 指示节点不合规。 如果所有资源的 Test 方法都返回 $true,则返回 $trueTest-DSCConfiguration指示节点符合要求。

Test-DSCConfiguration
True

从 PowerShell 5.0 开始,添加了 Detailed 参数。 指定 Detailed 会导致 Test-DSCConfiguration 返回一个对象,其中包含合规和不合规资源的结果集合。

Test-DSCConfiguration -Detailed
PSComputerName  ResourcesInDesiredState        ResourcesNotInDesiredState     InDesiredState
--------------  -----------------------        --------------------------     --------------
localhost       {[Service]Spooler}                                            True

有关详细信息,请参阅 Test-DSCConfiguration

设置

资源的 Set 方法尝试强制节点符合资源的 所需状态Set 方法旨在冪等,这意味着 Set 可以多次运行,并且始终获得相同的结果而不会出现错误。 运行 Start-DSCConfiguration 时,LCM 将循环浏览当前应用的配置中的每个资源。 LCM 从“.mof”文件中检索当前资源实例的键值,并将其用作 Test 方法的参数。 如果 Test 方法返回 $true,则 Node 与当前资源兼容,并且跳过 Set 方法。 如果 测试 返回 $false,则节点不合规。 LCM 将资源实例的键值作为参数传递给资源的 Set 方法,从而将节点恢复为合规性。

通过指定 VerboseWait 参数,可以监视 cmdlet 的 Start-DSCConfiguration 进度。 在此示例中,节点已符合要求。 输出指示 Verbose 跳过了 Set 方法。

PS> Start-DSCConfiguration -Verbose -Wait -UseExisting

VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' =
ApplyConfiguration,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' =
root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer SERVER01 with user sid
S-1-5-21-124525095-708259637-1543119021-1282804.
VERBOSE: [SERVER01]:                            [] Starting consistency engine.
VERBOSE: [SERVER01]:                            [] Checking consistency for current configuration.
VERBOSE: [SERVER01]:                            [DSCEngine] Importing the module
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\DscResources\MSFT_ServiceResource\MSFT
_ServiceResource.psm1 in force mode.
VERBOSE: [SERVER01]: LCM:  [ Start  Resource ]  [[Service]Spooler]
VERBOSE: [SERVER01]: LCM:  [ Start  Test     ]  [[Service]Spooler]
VERBOSE: [SERVER01]:                            [[Service]Spooler] Importing the module MSFT_ServiceResource in
force mode.
VERBOSE: [SERVER01]: LCM:  [ End    Test     ]  [[Service]Spooler]  in 0.2540 seconds.
VERBOSE: [SERVER01]: LCM:  [ Skip   Set      ]  [[Service]Spooler]
VERBOSE: [SERVER01]: LCM:  [ End    Resource ]  [[Service]Spooler]
VERBOSE: [SERVER01]:                            [] Consistency check completed.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 1.379 seconds

另请参阅