Get-Test-Set

適用於:Windows PowerShell 4.0、Windows PowerShell 5.0

PowerShell 預期狀態設定是圍繞著取得測試設定流程所建構的。 每個 DSC 資源均包含可用以完成上述各項作業的方法。 在設定中,您會定義資源區塊來填滿索引鍵,以成為適用於資源之取得測試設定方法的參數。

這是適用於服務資源區塊的語法。 服務資源會設定 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 }]
}

服務資源的取得測試設定方法將具備可接受這些值的參數區塊。

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"
)

注意

用來定義資源的語言和方法會決定將如何定義取得測試設定方法。

因為服務資源只有一個必要的索引鍵 (Name),所以服務區塊資源可能如下所示般簡單:

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 檔案讀取值 "Spooler",傳遞給 Service 資源其 "MyService" 執行個體的 GetTestSet 方法的 Name 參數。

Get

資源的取得方法會在目標節點上設定資源時擷取其狀態。 此狀態會當成雜湊表傳回。 雜湊表的索引鍵將是資源接受的可設定值或參數。

取得方法會直接對應至 Get-DSCConfiguration Cmdlet。 當您呼叫 Get-DSCConfiguration 時,LCM 會在目前套用的設定中執行每個資源的取得方法。 LCM 會使用儲存在 .mof 檔案中的索引鍵值,以作為每個對應資源執行個體的參數。

這是設定 "Spooler" 服務的服務資源範例輸出。

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 }]
}

測試

資源的測試方法會判斷目標節點目前是否符合資源「期望狀態」的規範。 測試方法只會傳回 $true$false,以指出節點是否符合規範。 當您呼叫 Test-DSCConfiguration 時,LCM 會在目前套用的設定中呼叫每個資源的取得方法。 LCM 會使用儲存於 ".mof" 檔案的索引鍵值作為每個對應資源執行個體的參數。

如果任何個別資源的測試結果是 $falseTest-DSCConfiguration 會傳回 $false,指出節點不符合規範。 如果所有資源的測試方法會傳回 $trueTest-DSCConfiguration 會傳回 $true,以指出節點符合規範。

Test-DSCConfiguration
True

從 PowerShell 5.0 開始,即已新增 Detailed 參數。 指定 Detailed 會導致 Test-DSCConfiguration 傳回物件,其包含符合規範及不符合規範的資源結果集合。

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

如需詳細資訊,請參閱 Test-DSCConfiguration

設定

資源的設定方法會嘗試強制節點符合資源「期望狀態」的規範。 設定方法意味著等冪,這表示設定會多次執行,且一律取得相同結果且不會有任何錯誤。 當您執行 Start-DSCConfiguration 時,LCM 會在目前套用的設定中循環執行每個資源。 LCM 會在 ".mof" 檔案中擷取目前資源執行個體的索引鍵值,並使用它們作為測試方法的參數。 如果測試方法傳回 $true,則該節點符合目前資源的規範,且會跳過設定方法。 如果測試傳回 $false,則節點不符合規範。 LCM 會將資源執行個體的索引鍵值當作參數傳遞給資源的設定方法,還原節點以符合規範。

指定 VerboseWait 參數,即可監看 Start-DSCConfiguration Cmdlet 的進度。 在此範例中,節點已經符合規範。 Verbose 輸出指出已跳過設定方法。

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

另請參閱