疑難排解 DSC

適用於:Windows PowerShell 4.0,Windows PowerShell 5.1

此文章提供常見錯誤的疑難排解指示。

WinRM 相依性

Windows PowerShell 預期狀態設定 (DSC) 取決於 WinRM。 Windows Server 2008 R2 和 Windows 7 預設不會啟用 WinRM。 若要啟用 WinRM,請在 Windows PowerShell 提高權限的工作階段中,執行 Set-WSManQuickConfig

使用 Get-DscConfigurationStatus

Get-DscConfigurationStatus Cmdlet 會從目標節點取得設定狀態的相關資訊。 此命令會傳回豐富的 物件,其中包含有關組態執行是否成功的高階資訊。 您可以深入了解此物件,以探索有關設定執行的詳細資料,例如︰

  • 失敗的每個資源。
  • 要求重新啟動的任何資源。
  • Meta-Configuration 設定執行時設定。

下列的參數集傳回上次設定執行時的狀態資訊︰

Get-DscConfigurationStatus [-CimSession <CimSession[]>]
                           [-ThrottleLimit <int>]
                           [-AsJob]
                           [<CommonParameters>]

下列參數集會傳回每個組態執行的狀態資訊:

Get-DscConfigurationStatus -All
                           [-CimSession <CimSession[]>]
                           [-ThrottleLimit <int>]
                           [-AsJob]
                           [<CommonParameters>]

範例

PS C:\> $Status = Get-DscConfigurationStatus

PS C:\> $Status

Status         StartDate                Type            Mode    RebootRequested        NumberOfResources
------        ---------                ----            ----    ---------------        -----------------
Failure        11/24/2015  3:44:56     Consistency        Push    True                36

PS C:\> $Status.ResourcesNotInDesiredState

ConfigurationName     :    MyService
DependsOn             :
ModuleName            :    PSDesiredStateConfiguration
ModuleVersion         :    1.1
PsDscRunAsCredential  :
ResourceID            :    [File]ServiceDll
SourceInfo            :    c:\git\CustomerService\Configs\MyCustomService.ps1::5::34::File
DurationInSeconds     :    0.19
Error                 :    SourcePath must be accessible for current configuration. The related file/directory is:
                           \\Server93\Shared\contosoApp.dll. The related ResourceID is [File]ServiceDll
FinalState            :
InDesiredState        :    False
InitialState          :
InstanceName          :    ServiceDll
RebootRequested       :    False
ResourceName          :    File
StartDate             :    11/24/2015  3:44:56
PSComputerName        :

我的指令碼不執行:使用 DSC 記錄診斷指令碼錯誤

就像所有 Windows 軟體一樣,DSC 會在記錄檔中記錄錯誤和事件,以供 事件檢視器 檢閱。 檢查這些記錄檔可以幫助您了解特定作業失敗的原因,以及如何避免失敗再度發生。 若要在撰寫時更輕鬆地追蹤錯誤,請使用 DSC 記錄資源來追蹤 DSC Analytic 事件記錄檔中的設定進度。

DSC 事件記錄檔在哪裡?

在事件檢視器中,DSC 事件位於:Applications and Services Logs/Microsoft/Windows/Desired State Configuration

您可以執行對應的 PowerShell Cmdlet Get-WinEvent,以檢視事件記錄檔:

PS C:\> Get-WinEvent -LogName "Microsoft-Windows-Dsc/Operational"

   ProviderName: Microsoft-Windows-DSC

TimeCreated                     Id LevelDisplayName Message
-----------                     -- ---------------- -------
11/17/2014 10:27:23 PM        4102 Information      Job {02C38626-D95A-47F1-9DA2-C1D44A7128E7} :

DSC 的主要記錄檔名稱是 Microsoft-Windows-DSC>> (Windows 底下的其他記錄檔名稱不會在此顯示,以求簡潔) 。 主要名稱會附加至通道名稱,以建立完整的記錄檔名稱。

DSC 引擎主要會寫入三種記錄:作業、分析和偵錯記錄。 由於預設不會啟用分析和偵錯記錄,因此您應該在 事件檢視器 中加以啟用。 作法如下:

  1. 開啟 事件檢視器 下列其中一項:

    • 輸入 Show-EventLog Windows PowerShell
    • 選取 [開始] 按鈕,然後 控制台、[系統管理工具] 和 [事件檢視器]。
  2. 在事件查看器的 [ 檢視 ] 功能表上,選取 [顯示分析和偵錯記錄]。

    剖析通道的記錄名稱稱為 Microsoft-Windows-Dsc/Analytic,而偵錯通道為 Microsoft-Windows-Dsc/Debug

您也可以使用 wevtutil 公用程式來啟用記錄,如下列範例所示。

wevtutil.exe set-log "Microsoft-Windows-Dsc/Analytic" /q:true /e:true

或者,使用 PowerShell 和 .NET 來啟用記錄,如下列範例所示:

$logName = 'Microsoft-Windows-Dsc/Analytic'
$log = New-Object System.Diagnostics.Eventing.Reader.EventLogConfiguration $logName
$log.IsEnabled = $true
$log.SaveChanges()

DSC 記錄檔包含哪些內容?

DSC 會根據訊息的重要性,將 DSC 記錄到三個不同的記錄通道。 DSC 中的作業記錄檔包含所有錯誤訊息,而且有助於識別問題。 分析記錄檔具有較高的事件量,並可識別發生錯誤的位置。 此通道也包含任何發出的詳細資訊訊息。 偵錯記錄具有可協助您了解錯誤發生方式的記錄。 DSC 事件訊息的開頭是唯一代表 DSC 作業的作業標識碼。 下列範例會嘗試從登入操作 DSC 記錄的第一個事件取得訊息。

PS C:\> $AllDscOpEvents = Get-WinEvent -LogName "Microsoft-Windows-Dsc/Operational"
PS C:\> $FirstOperationalEvent = $AllDscOpEvents[0]
PS C:\> $FirstOperationalEvent.Message
Job {02C38626-D95A-47F1-9DA2-C1D44A7128E7} :
Consistency engine was run successfully.

DSC 會以結構記錄事件,讓用戶能夠從一個 DSC 作業收集事件。 其結構如下所示:

Job ID : <Guid>
<Event Message>

收集單一 DSC 作業的事件

DSC 事件記錄檔包含各種 DSC 作業產生的事件。 不過,您通常擔心特定作業的詳細數據。 所有 DSC 記錄都可以依每個 DSC 作業唯一的作業標識碼屬性來分組。 作業標識子會顯示為所有 DSC 事件中的第一個屬性值。 下列步驟說明如何將所有事件累計到分組的陣列結構中。

<##########################################################################
 Step 1 : Enable analytic and debug DSC channels (Operational channel is enabled by default)
###########################################################################>

wevtutil.exe set-log "Microsoft-Windows-Dsc/Analytic" /q:true /e:true
wevtutil.exe set-log "Microsoft-Windows-Dsc/Debug" /q:True /e:true

<##########################################################################
 Step 2 : Perform the required DSC operation (Below is an example, you could run any DSC operation instead)
###########################################################################>

Get-DscLocalConfigurationManager

<##########################################################################
Step 3 : Collect all DSC Logs, from the Analytic, Debug and Operational channels
###########################################################################>

$DscEvents=[System.Array](Get-WinEvent "Microsoft-Windows-Dsc/Operational") `
         + [System.Array](Get-WinEvent "Microsoft-Windows-Dsc/Analytic" -Oldest) `
         + [System.Array](Get-WinEvent "Microsoft-Windows-Dsc/Debug" -Oldest)


<##########################################################################
 Step 4 : Group all logs based on the job ID
###########################################################################>
$SeparateDscOperations = $DscEvents | Group {$_.Properties[0].value}

在這裡,變數 $SeparateDscOperations 包含依作業標識碼分組的記錄。 這個變數的每個陣列元素都代表一組依照不同 DSC 作業記錄的事件,讓您存取有關記錄檔的詳細資訊。

PS C:\> $SeparateDscOperations

Count Name                      Group
----- ----                      -----
   48 {1A776B6A-5BAC-11E3-BF... {System.Diagnostics.Eventing.Reader.EventLogRecord, System.Diagnostics....
   40 {E557E999-5BA8-11E3-BF... {System.Diagnostics.Eventing.Reader.EventLogRecord, System.Diagnostics....

PS C:\> $SeparateDscOperations[0].Group

   ProviderName: Microsoft-Windows-DSC

TimeCreated                     Id LevelDisplayName Message
-----------                     -- ---------------- -------
12/2/2013 3:47:29 PM          4115 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4198 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4114 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4102 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4098 Warning          Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4098 Warning          Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4176 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4182 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4182 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4182 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4182 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4182 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4182 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4182 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...
12/2/2013 3:47:29 PM          4182 Information      Job {1A776B6A-5BAC-11E3-BF41-00155D553612} : ...

您可以使用 Where-Object 擷取變數 $SeparateDscOperations 中的資料。 以下是您可能會想要擷取數據以進行 DSC 疑難解答的五個案例:

1:作業失敗

所有的事件都有嚴重性等級。 此資訊有助於識別錯誤事件:

PS C:\> $SeparateDscOperations | Where-Object {$_.Group.LevelDisplayName -contains "Error"}

Count Name                      Group
----- ----                      -----
   38 {5BCA8BE7-5BB6-11E3-BF... {System.Diagnostics.Eventing.Reader.EventLogRecord, System.Diagnostics....

2:過去半小時內執行的作業詳細資料

TimeCreated 是每個 Windows 事件都有的屬性,指出事件的建立時間。 比較此屬性與特定的日期/時間對象,對於篩選所有事件很有用:

PS C:\> $DateLatest = (Get-Date).AddMinutes(-30)
PS C:\> $SeparateDscOperations | Where-Object {$_.Group.TimeCreated -gt $DateLatest}

Count Name                      Group
----- ----                      -----
    1 {6CEC5B09-5BB0-11E3-BF... {System.Diagnostics.Eventing.Reader.EventLogRecord}

3:最新作業的訊息

最新的作業會儲存在陣列群組 $SeparateDscOperations 的第一個索引中。 查詢索引 0 的群組訊息會傳回最新作業的所有訊息:

PS C:\> $SeparateDscOperations[0].Group.Message
Job {5BCA8BE7-5BB6-11E3-BF41-00155D553612} :
Running consistency engine.
Job {1A776B6A-5BAC-11E3-BF41-00155D553612} :
Configuration is sent from computer NULL by user sid S-1-5-18.
Job {1A776B6A-5BAC-11E3-BF41-00155D553612} :
Displaying messages from built-in DSC resources:
 WMI channel 1
 ResourceID:
 Message : [INCH-VM]:                            [] Starting consistency engine.
Job {1A776B6A-5BAC-11E3-BF41-00155D553612} :
Displaying messages from built-in DSC resources:
 WMI channel 1
 ResourceID:
 Message : [INCH-VM]:                            [] Consistency check completed.

4:近期的作業失敗錯誤訊息記錄

$SeparateDscOperations[0].Group 具有最新作業的事件集。 執行 Where-Object Cmdlet 會根據層級顯示名稱篩選事件。 結果儲存在 $myFailedEvent 變數中,可進一步解析以取得事件訊息:

PS C:\> $myFailedEvent = ($SeparateDscOperations[0].Group |
    Where-Object {$_.LevelDisplayName -eq "Error"})

PS C:\> $myFailedEvent.Message

Job {5BCA8BE7-5BB6-11E3-BF41-00155D553612} :
DSC Engine Error :
 Error Message Current configuration does not exist. Execute Start-DscConfiguration command with
 -Path parameter to specify a configuration file and create a current configuration first.
Error Code : 1

5:針對特定工作識別碼產生的所有事件。

$SeparateDscOperations 是群組的陣列,每個群組的名稱都是唯一的作業標識碼。 執行 Where-Object Cmdlet 就可以擷取這些有特定工作識別碼的事件群組:

PS C:\> ($SeparateDscOperations | Where-Object {$_.Name -eq $jobX} ).Group

   ProviderName: Microsoft-Windows-DSC

TimeCreated                     Id LevelDisplayName Message
-----------                     -- ---------------- -------
12/2/2013 4:33:24 PM          4102 Information      Job {847A5619-5BB2-11E3-BF41-00155D553612} : ...
12/2/2013 4:33:24 PM          4168 Information      Job {847A5619-5BB2-11E3-BF41-00155D553612} : ...
12/2/2013 4:33:24 PM          4146 Information      Job {847A5619-5BB2-11E3-BF41-00155D553612} : ...
12/2/2013 4:33:24 PM          4120 Information      Job {847A5619-5BB2-11E3-BF41-00155D553612} : ...

使用 xDscDiagnostics 分析 DSC 記錄

xDscDiagnostics 是 PowerShell 模組,由數個可協助分析電腦上 DSC 失敗的函數組成。 這些函式可協助您識別來自過去 DSC 作業的所有本機事件,或遠端電腦上的 DSC 事件。 在這裡,DSC 作業一詞會定義從頭到尾的單一唯一 DSC 執行。 例如,Test-DscConfiguration 就會是另外一個 DSC 作業。 同樣地,DSC 中的所有其他 Cmdlet,例如 Get-DscConfigurationStart-DscConfiguration,都可以識別為個別的 DSC 作業。 如需診斷函式的詳細資訊,請參閱 xDscDiagnostics

取得 DSC 作業的詳細資訊

Get-xDscOperation 式可讓您尋找在一或多部計算機上執行的 DSC 作業結果。 函式會傳回物件,該物件具有每個 DSC 作業所產生的事件集合。 例如,在下列輸出中,執行了三個命令。 第一個成功,而其他兩個失敗了。 的 Get-xDscOperation 輸出會摘要說明這些結果。

PS C:\DiagnosticsTest> Get-xDscOperation
ComputerName   SequenceId TimeCreated           Result   JobID                                 AllEvents
------------   ---------- -----------           ------   -----                                 ---------
SRV1   1          6/23/2016 9:37:52 AM  Failure  9701aadf-395e-11e6-9165-00155d390509  {@{Message=; TimeC...
SRV1   2          6/23/2016 9:36:54 AM  Failure  7e8e2d6e-395c-11e6-9165-00155d390509  {@{Message=; TimeC...
SRV1   3          6/23/2016 9:36:54 AM  Success  af72c6aa-3960-11e6-9165-00155d390509  {@{Message=Operati...

您可以藉由指定 Newest 參數,只取得最近作業的結果:

PS C:\DiagnosticsTest> Get-xDscOperation -Newest 5
ComputerName   SequenceId TimeCreated           Result   JobID                                 AllEvents
------------   ---------- -----------           ------   -----                                 ---------
SRV1   1          6/23/2016 4:36:54 PM  Success                                        {@{Message=; TimeC...
SRV1   2          6/23/2016 4:36:54 PM  Success  5c06402b-399b-11e6-9165-00155d390509  {@{Message=Operati...
SRV1   3          6/23/2016 4:36:54 PM  Success                                        {@{Message=; TimeC...
SRV1   4          6/23/2016 4:36:54 PM  Success  5c06402a-399b-11e6-9165-00155d390509  {@{Message=Operati...
SRV1   5          6/23/2016 4:36:51 PM  Success                                        {@{Message=; TimeC...

取得 DSC 事件的詳細資訊

Trace-xDscOperation Cmdlet 傳回的物件包含了事件集合、其事件類型,以及從特定 DSC 作業產生的訊息輸出。 一般而言,當您使用 Get-xDscOperation在任何作業中找到失敗時,您會追蹤該作業來尋找造成失敗的事件。

使用 SequenceID 參數以取得特定電腦的特定作業事件。 例如,SequenceID 如果指定為 9,則 Trace-xDscOperation 就會取得上一個作業的第 9 筆 DSC 作業追蹤記錄︰

PS C:\DiagnosticsTest> Trace-xDscOperation -SequenceID 9
ComputerName   EventType    TimeCreated           Message
------------   ---------    -----------           -------
SRV1   OPERATIONAL  6/24/2016 10:51:52 AM Operation Consistency Check or Pull started by user sid S-1-5-20 from computer NULL.
SRV1   OPERATIONAL  6/24/2016 10:51:52 AM Running consistency engine.
SRV1   OPERATIONAL  6/24/2016 10:51:52 AM The local configuration manager is updating the PSModulePath to WindowsPowerShell\Modules;C:\Prog...
SRV1   OPERATIONAL  6/24/2016 10:51:53 AM  Resource execution sequence :: [WindowsFeature]DSCServiceFeature, [xDSCWebService]PSDSCPullServer.
SRV1   OPERATIONAL  6/24/2016 10:51:54 AM Consistency engine was run successfully.
SRV1   OPERATIONAL  6/24/2016 10:51:54 AM Job runs under the following LCM setting. ...
SRV1   OPERATIONAL  6/24/2016 10:51:54 AM Operation Consistency Check or Pull completed successfully.

傳遞指派給特定 DSC 作業的 GUID (如 Get-xDscOperation Cmdlet 所傳回) 來取得該 DSC 作業的事件詳細資料:

PS C:\DiagnosticsTest> Trace-xDscOperation -JobID 9e0bfb6b-3a3a-11e6-9165-00155d390509

ComputerName   EventType    TimeCreated           Message
------------   ---------    -----------           -------
SRV1   OPERATIONAL  6/24/2016 11:36:56 AM Operation Consistency Check or Pull started by user sid S-1-5-20 from computer NULL.
SRV1   ANALYTIC     6/24/2016 11:36:56 AM Deleting file from C:\Windows\System32\Configuration\DSCEngineCache.mof
SRV1   OPERATIONAL  6/24/2016 11:36:56 AM Running consistency engine.
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]:                            [] Starting consistency engine.
SRV1   ANALYTIC     6/24/2016 11:36:56 AM Applying configuration from C:\Windows\System32\Configuration\Current.mof.
SRV1   ANALYTIC     6/24/2016 11:36:56 AM Parsing the configuration to apply.
SRV1   OPERATIONAL  6/24/2016 11:36:56 AM  Resource execution sequence :: [WindowsFeature]DSCServiceFeature, [xDSCWebService]PSDSCPullServer.
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]: LCM:  [ Start  Resource ]  [[WindowsFeature]DSCServiceFeature]
SRV1   ANALYTIC     6/24/2016 11:36:56 AM Executing operations for PS DSC resource MSFT_RoleResource with resource name [WindowsFeature]DSC...
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]: LCM:  [ Start  Test     ]  [[WindowsFeature]DSCServiceFeature]
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]:                            [[WindowsFeature]DSCServiceFeature] The operation 'Get...
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]:                            [[WindowsFeature]DSCServiceFeature] The operation 'Get...
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]: LCM:  [ End    Test     ]  [[WindowsFeature]DSCServiceFeature] True in 0.3130 sec...
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]: LCM:  [ End    Resource ]  [[WindowsFeature]DSCServiceFeature]
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]: LCM:  [ Start  Resource ]  [[xDSCWebService]PSDSCPullServer]
SRV1   ANALYTIC     6/24/2016 11:36:56 AM Executing operations for PS DSC resource MSFT_xDSCWebService with resource name [xDSCWebService]P...
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]: LCM:  [ Start  Test     ]  [[xDSCWebService]PSDSCPullServer]
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]:                            [[xDSCWebService]PSDSCPullServer] Check Ensure
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]:                            [[xDSCWebService]PSDSCPullServer] Check Port
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]:                            [[xDSCWebService]PSDSCPullServer] Check Physical Path ...
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]:                            [[xDSCWebService]PSDSCPullServer] Check State
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]:                            [[xDSCWebService]PSDSCPullServer] Get Full Path for We...
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]: LCM:  [ End    Test     ]  [[xDSCWebService]PSDSCPullServer] True in 0.0160 seconds.
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]: LCM:  [ End    Resource ]  [[xDSCWebService]PSDSCPullServer]
SRV1   VERBOSE      6/24/2016 11:36:56 AM [SRV1]:                            [] Consistency check completed.
SRV1   ANALYTIC     6/24/2016 11:36:56 AM Deleting file from C:\Windows\System32\Configuration\DSCEngineCache.mof
SRV1   OPERATIONAL  6/24/2016 11:36:56 AM Consistency engine was run successfully.
SRV1   OPERATIONAL  6/24/2016 11:36:56 AM Job runs under the following LCM setting. ...
SRV1   OPERATIONAL  6/24/2016 11:36:56 AM Operation Consistency Check or Pull completed successfully.
SRV1   ANALYTIC     6/24/2016 11:36:56 AM Deleting file from C:\Windows\System32\Configuration\DSCEngineCache.mof

請注意,由於 Trace-xDscOperation 匯總來自分析、偵錯和作業記錄的事件,因此會提示您啟用這些記錄。

此外,您也可以將 Trace-xDscOperation 的輸出存入變數中來收集事件相關資訊。 您可以使用下列命令來顯示特定 DSC 作業的所有事件。

PS C:\DiagnosticsTest> $Trace = Trace-xDscOperation -SequenceID 4

PS C:\DiagnosticsTest> $Trace.Event

這會顯示與 Cmdlet 相同的結果 Get-WinEvent ,例如在下列輸出中:

   ProviderName: Microsoft-Windows-DSC

TimeCreated                     Id LevelDisplayName Message
-----------                     -- ---------------- -------
6/23/2016 1:36:53 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 1:36:53 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 2:07:00 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 2:07:01 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 2:36:55 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 2:36:56 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 3:06:55 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 3:06:55 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 3:36:55 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 3:36:55 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 4:06:53 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 4:06:53 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 4:36:52 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 4:36:53 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 5:06:52 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 5:06:53 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 5:36:54 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 5:36:54 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 6:06:52 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 6:06:53 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 6:36:56 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 6:36:57 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 7:06:52 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 7:06:53 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 7:36:53 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.
6/23/2016 7:36:54 AM          4343 Information      The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 5.
6/23/2016 8:06:54 AM          4312 Information      The DscTimer is running LCM method PerformRequiredConfigurationChecks with the flag set to 5.

理想的情況是先使用 Get-xDscOperation 列出電腦上執行的最新幾個 DSC 設定。 遵循此動作,您可以指定 其 SequenceIDJobIDTrace-xDscOperation 來檢查任何單一作業,以探索其幕後所執行的內容。

取得遠端電腦的事件

使用 Trace-xDscOperation Cmdlet 的 ComputerName 參數來取得遠端電腦上的事件詳細資訊。 執行這項作業之前,您必須先建立防火牆規則,允許在遠端電腦上進行遠端系統管理︰

New-NetFirewallRule -Name "Service RemoteAdmin" -DisplayName "Remote" -Action Allow

現在,您可以在 Trace-xDscOperation 呼叫中指定該部電腦:

Trace-xDscOperation -ComputerName SRV2 -Credential Get-Credential -SequenceID 5
ComputerName   EventType    TimeCreated           Message
------------   ---------    -----------           -------
SRV2   OPERATIONAL  6/24/2016 11:36:56 AM Operation Consistency Check or Pull started by user sid S-1-5-20 f...
SRV2   ANALYTIC     6/24/2016 11:36:56 AM Deleting file from C:\Windows\System32\Configuration\DSCEngineCach...
SRV2   OPERATIONAL  6/24/2016 11:36:56 AM Running consistency engine.
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]:                            [] Starting consistency...
SRV2   ANALYTIC     6/24/2016 11:36:56 AM Applying configuration from C:\Windows\System32\Configuration\Curr...
SRV2   ANALYTIC     6/24/2016 11:36:56 AM Parsing the configuration to apply.
SRV2   OPERATIONAL  6/24/2016 11:36:56 AM  Resource execution sequence :: [WindowsFeature]DSCServiceFeature,...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]: LCM:  [ Start  Resource ]  [[WindowsFeature]DSCSer...
SRV2   ANALYTIC     6/24/2016 11:36:56 AM Executing operations for PS DSC resource MSFT_RoleResource with re...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]: LCM:  [ Start  Test     ]  [[WindowsFeature]DSCSer...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]:                            [[WindowsFeature]DSCSer...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]:                            [[WindowsFeature]DSCSer...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]: LCM:  [ End    Test     ]  [[WindowsFeature]DSCSer...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]: LCM:  [ End    Resource ]  [[WindowsFeature]DSCSer...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]: LCM:  [ Start  Resource ]  [[xDSCWebService]PSDSCP...
SRV2   ANALYTIC     6/24/2016 11:36:56 AM Executing operations for PS DSC resource MSFT_xDSCWebService with ...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]: LCM:  [ Start  Test     ]  [[xDSCWebService]PSDSCP...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]:                            [[xDSCWebService]PSDSCP...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]:                            [[xDSCWebService]PSDSCP...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]:                            [[xDSCWebService]PSDSCP...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]:                            [[xDSCWebService]PSDSCP...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]:                            [[xDSCWebService]PSDSCP...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]: LCM:  [ End    Test     ]  [[xDSCWebService]PSDSCP...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]: LCM:  [ End    Resource ]  [[xDSCWebService]PSDSCP...
SRV2   VERBOSE      6/24/2016 11:36:56 AM [SRV2]:                            [] Consistency check co...
SRV2   ANALYTIC     6/24/2016 11:36:56 AM Deleting file from C:\Windows\System32\Configuration\DSCEngineCach...
SRV2   OPERATIONAL  6/24/2016 11:36:56 AM Consistency engine was run successfully.
SRV2   OPERATIONAL  6/24/2016 11:36:56 AM Job runs under the following LCM setting. ...
SRV2   OPERATIONAL  6/24/2016 11:36:56 AM Operation Consistency Check or Pull completed successfully.
SRV2   ANALYTIC     6/24/2016 11:36:56 AM Deleting file from C:\Windows\System32\Configuration\DSCEngineCach...

我的資源不更新:如何重設快取

基於效率考量,DSC 引擎會快取實作為 PowerShell 模組的資源。 不過,當您撰寫資源並同時進行測試時,這可能會造成問題,因為 DSC 會載入快取的版本,直到進程重新啟動為止。 讓 DSC 載入較新版本的唯一方法,是明確結束主控 DSC 引擎的程序。

同樣地,當您在 Start-DscConfiguration新增和修改自定義資源之後執行 時,除非計算機重新啟動,否則修改可能不會執行。 這是因為 DSC 會在 WMI 提供者主機進程 (WmiPrvSE) 中執行,而且通常一次執行許多 WmiPrvSE 實例。 當您重新啟動時,主機進程會重新啟動並清除快取。

若要順利回收設定和清除快取卻不重新開機,您必須停止然後再重新啟動主機處理程序。 這可以依據每個實例來識別進程、加以停止,然後重新啟動。 或者如下文所示,使用 DebugMode 重新載入 PowerShell DSC 資源。

若要識別裝載 DSC 引擎的進程,請列出 裝載 DSC 引擎的進程 WmiPrvSE識別碼。 然後,若要更新提供者,請使用下列命令停止 WmiPrvSE 進程,然後再執行 Start-DscConfiguration 一次。

###
### find the process that is hosting the DSC engine
###
$CimParameters = @{
    ClassName = 'Msft_Providers'
    Filter    = "provider='dsctimer' OR provider='dsccore'"
}
$dscProcessID = Get-CimInstance @CimParameters |
    Select-Object -ExpandProperty HostProcessIdentifier

###
### Stop the process
###
Get-Process -Id $dscProcessID | Stop-Process

使用 DebugMode

您可以設定 DSC 本機設定管理員 (LCM) 使用 DebugMode 在重新啟動主機處理程序時,一律清除快取。 當設定為 $true時,它會導致引擎一律重載 PowerShell DSC 資源。 完成撰寫資源之後,您可以將它設定回 $false ,而引擎會還原為快取模組的行為。

下面的示範會說明 DebugMode 如何自動重新整理快取。 首先,我們來看一下預設設定:

Get-DscLocalConfigurationManager
AllowModuleOverwrite           : False
CertificateID                  :
ConfigurationID                :
ConfigurationMode              : ApplyAndMonitor
ConfigurationModeFrequencyMins : 30
Credential                     :
DebugMode                      : {None}
DownloadManagerCustomData      :
DownloadManagerName            :
LocalConfigurationManagerState : Ready
RebootNodeIfNeeded             : False
RefreshFrequencyMins           : 15
RefreshMode                    : PUSH
PSComputerName                 :

您可以看到是 DebugModeNone

若要安裝 DebugMode 示範,請使用下列 PowerShell 資源:

function Get-TargetResource {
    param (
        [Parameter(Mandatory)] $onlyProperty
    )

    $Path = "$env:SystemDrive\OutputFromTestProviderDebugMode.txt"

    return @{
        onlyProperty = Get-Content -Path $Path
    }
}
function Set-TargetResource {
    param (
        [Parameter(Mandatory)] $onlyProperty
    )

    "1" | Out-File -PSPath "$env:SystemDrive\OutputFromTestProviderDebugMode.txt"
}
function Test-TargetResource {
    param (
        [Parameter(Mandatory)]
        $onlyProperty
    )

    return $false
}

現在,撰寫名為 的 TestProviderDebugMode組態:

Configuration ConfigTestDebugMode
{
    Import-DscResource -Name TestProviderDebugMode
    Node localhost
    {
        TestProviderDebugMode test
        {
            onlyProperty = "blah"
        }
    }
}
ConfigTestDebugMode

檔案 $env:SystemDrive\OutputFromTestProviderDebugMode.txt 的內容為 1

現在使用下列指令碼來更新提供者程式碼:

$newResourceOutput = Get-Random -Minimum 5 -Maximum 30
$OutputPath = "C:\Program Files\WindowsPowerShell\Modules\MyPowerShellModules\DSCResources\TestProviderDebugMode\TestProviderDebugMode.psm1"
$content = @"
function Get-TargetResource {
    param (
        [Parameter(Mandatory)] `$onlyProperty
    )

    `$Path = "$env:SystemDrive\OutputFromTestProviderDebugMode.txt"
    return @{
        onlyProperty = Get-Content -Path $Path
    }
}
function Set-TargetResource {
    param (
        [Parameter(Mandatory)] `$onlyProperty
    )

    "$newResourceOutput" | Out-File -PSPath C:\OutputFromTestProviderDebugMode.txt
}
function Test-TargetResource {
    param (
        [Parameter(Mandatory)] `$onlyProperty
    )

    return `$false
}
"@ | Out-File -FilePath $OutputPath

此文稿會產生隨機數並更新提供者程序代碼。 在將 DebugMode 設定為 False 的情況下,$env:SystemDrive\OutputFromTestProviderDebugMode.txt 檔案的內容一律不會變更。

現在,在組態文稿中設定 DebugModeForceModuleImport

LocalConfigurationManager
{
    DebugMode = "ForceModuleImport"
}

當您再次執行腳本時,請注意,每次檔案的內容都不同。 您可以執行 Get-DscConfiguration 以檢查它。 下列代碼段顯示兩個執行的結果。 當您執行腳本時,結果可能會不同。

PS C:\> Get-DscConfiguration -CimSession (New-CimSession localhost)

onlyProperty                            PSComputerName
------------                            --------------
20                                      localhost

PS C:\> Get-DscConfiguration -CimSession (New-CimSession localhost)

onlyProperty                            PSComputerName
------------                            --------------
14                                      localhost

DSC 向 Windows 提取伺服器註冊時,傳回了「未預期的回應碼 InternalServerError」

將中繼設定套用至伺服器以向 Windows 提取伺服器的實例註冊時,您可能會收到下列錯誤。

Registration of the Dsc Agent with the server https://<serverfqdn>:8080/PSDSCPullServer.svc failed. The underlying error is: The attempt to register Dsc Agent with AgentId <ID> with the server
https://<serverfqdn>:8080/PSDSCPullServer.svc/Nodes(AgentId='<ID>') returned unexpected response code InternalServerError. .
    + CategoryInfo          : InvalidResult: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : RegisterDscAgentUnsuccessful,Microsoft.PowerShell.DesiredStateConfiguration.Commands.RegisterDscAgentCommand
    + PSComputerName        : <computername>

當伺服器上用來加密流量的憑證具有與節點用來解析 URL 之 DNS 名稱不同的一般名稱 (CN) 時,就會發生這種情況。 更新 Windows 提取伺服器執行個體,以使用名稱經更正的憑證。

在套用 DSC 組態後,執行 Sysprep 時發生錯誤

嘗試執行 Sysprep 以在套用 DSC 設定之後將 Windows Server 一般化時,您可能會收到下列錯誤。

SYSPRP LaunchDll:Failure occurred while executing 'DscCore.dll,SysPrep_Cleanup', returned error code 0x2

使用 Windows PowerShell Desired State Configuration 設定伺服器之後,將伺服器一般化不是支援的案例。 請改為在 Windows 安裝程式的特殊化階段完成之後,將組態套用至 Windows。

另請參閱

概念

其他資源