DSC 故障排除
适用于:Windows PowerShell 4.0、Windows PowerShell 5.1
本文介绍有关常见错误的故障排除说明。
WinRM 依赖关系
Windows PowerShell Desired State Configuration (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 引擎主要写入三种类型的日志:运行、分析和调试日志。 由于默认情况下未启用分析和调试日志,因此应在 事件查看器 中启用它们。 为此,请按以下步骤操作:
通过以下任一方式打开事件查看器:
- 在 Windows PowerShell 中键入
Show-EventLog
- 依次选择“开始”按钮、控制面板、管理工具和事件查看器。
- 在 Windows PowerShell 中键入
在“事件查看器”的“ 视图 ”菜单上,选择“ 显示分析和调试日志”。
分析通道的日志名称为
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 操作的作业 ID 开头。 以下示例尝试从记录到操作 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 日志都可以按作业 ID 属性进行分组,该属性对于每个 DSC 操作都是唯一的。 作业 ID 显示为所有 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
包括按作业 ID 分组的日志。 此变量的每个数组元素表示由不同的 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:过去半小时内所运行操作的详细信息
每个 Windows 事件都具有 TimeCreated
属性,它表明创建该事件的时间。 将此属性与特定的日期/时间对象进行比较可用于筛选所有事件:
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:为特定作业 ID 生成的所有事件。
$SeparateDscOperations
是组的数组,每个组的名称都作为唯一的作业 ID。 通过运行 Where-Object
cmdlet,你可以提取这些具有特定作业 ID 的事件组:
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-DscConfiguration
和 Start-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
参数以获取某个特定计算机的某个特定操作的事件。
例如,如果你指定 9 的 SequenceID
,则 Trace-xDscOperation
获取 DSC 操作的跟踪(自上一次操作的第 9 个):
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 操作(由 Get-xDscOperation
cmdlet 返回)的 GUID 以获取 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 配置。 在此之后,可以通过使用 指定其 SequenceID 或 JobIDTrace-xDscOperation
来检查任何单个操作,以发现它在后台执行的操作。
获取远程计算机的事件
使用 ComputerName
cmdlet 的 Trace-xDscOperation
参数以获取远程计算机上的事件详细信息。 执行此操作前,你需要创建一个防火墙规则,以允许在远程计算机上进行远程管理:
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
的进程 ID。 然后,若要更新提供程序,请使用以下命令停止 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 :
可以看到, DebugMode
是 None
。
若要设置 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
的内容。
现在,在配置脚本中将 ForceModuleImport
设置为 DebugMode
:
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
注册 Windows 拉取服务器时,DSC 返回“意外的响应代码 InternalServerError”
将元配置应用于服务器以将其注册到 Windows Pull Server 实例时,可能会收到以下错误。
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>
当服务器上用于加密流量的证书的公用名称 (CN) 与节点用于解析 URL 的 DNS 名称不同时,可能会发生这种情况。 更新 Windows 拉取服务器实例,使用名称正确的证书。
在应用 DSC 配置后应用 Sysprep 时出错
在应用 DSC 配置后尝试运行 Sysprep 以通用化 Windows Server 时,可能会收到以下错误。
SYSPRP LaunchDll:Failure occurred while executing 'DscCore.dll,SysPrep_Cleanup', returned error code 0x2
不支持使用 Windows PowerShell Desired State Configuration 配置服务器后将其通用化。 转而,请在完成 Windows 设置的通用化阶段后将配置应用于 Windows。
另请参阅
概念
其他资源
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈