[置顶] [9] Window PowerShell DSC 学习系列----错误诊断和分析

标签: DevOpsPowerShellDSCPowerShellWindows自动化运维
1393人阅读 评论(0) 收藏 举报
分类:

PowerShell DSC就像一个汽车,内部复杂,司机作为一个普通的用户在使用它,因为知道一些汽车的基本常识,所以当汽车发生一些简单的故障时,可以通过汽车自身带的仪表仪器诊断问题出在哪方面,也可以借助于专业的汽车工具去进行故障定位;其实PowerShell DSC有何尝不是这样。 PowerShell DSC 5.x 提供了一系列的工具用来帮助用户进行故障分析和诊断。下面请让笔者为大家一一娓娓道来。

使用Get-DSCConfigurationStatus 命令

Get-DSCConfigurationStatus cmdlet 能够得到目标节点的配置状态。 其返回一个功能强大的对象,包括配置信息是否在目标节点上应用成功。而且用户还能进一步分析其返回的对象,比如下面的一些细节。
  • 所有的失败的DSC的Resource
  • 那个DSC resource需要重启系统
  • 配置运行时的元配置信息
下面是一个具体的例子。
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
ReosurceName            :    File
StartDate                :    11/24/2015  3:44:56
PSComputerName            :

查看DSC 事件日志(Event Log)

和许多其他的Window软件一样,DSC本身也能记录错误的事件信息并通过Event Viewer软件查看。探测这些日志,能够帮助我们理解为什么一些特别的操作失败了,下次如何去避免这种失败。写配置脚本的时候,可能发现不了问题,但是执行的时候可能会出各种各样诡异的问题,通过DSC的事件日志,我们能够跟踪我的错误来源。

那么,DSC 的事件日志在哪呢?
在Event Viewer中,DSC的事件存在于Applications and Services Logs/Microsoft/Windows/Desired State Configuration

我们能够下面的 PowerShell cmdlet, Get-WinEvent 查看日志:  Get-WinEvent -LogName"Microsoft-Windows-Dsc/Operational"

其实,DSC 引擎会记录三种不同类型的日志:  Operational, Analytic, 和 Debug logs
默认情况下,Analytic, 和 Debug logs不会开启。为了开启Analytic, 和 Debug logs的日志,需要打开 Event Viewer。 在PowerShell 的控制台下,输入下面的命令: Show-EventLog 就能把Event Viewer软件打开。


在View的菜单中选择Show Analytic and Debug Logs. 这样的话,就自动开启了Analytic, 和 Debug logs的日志。
为了获取Analytic的log,可以执行下面的命令:
 Get-WinEvent -LogName "Microsoft-Windows-Dsc/Analytic"
 Get-WinEvent -LogName "Microsoft-Windows-Dsc/Debug"

也可以通过下面的命令开启Analytic, 和 Debug logs的日志
wevtutil.exe set-log “Microsoft-Windows-Dsc/Analytic” /q:true /e:true

那么三种Operational, Analytic, 和 Debug logs不同类的日志,究竟有什么区别呢?一般来说,Operational日志一般记录的是错误信息;Analytic日志记录了更多的信息,可以帮助我们定位错误是在什么地方发生的;而debug日志,则更详细,能帮助我们理解错误是如何发生的。下面就是一个获取第一条日志信息的Powershell脚本。

PS C:\Users\Admin>  $AllDscOpEvents = Get-WinEvent -LogName "Microsoft-Windows-Dsc/Operational"
PS C:\Users\Admin> $FirstOperationalEvent = $AllDscOpEvents[0]
PS C:\Users\Admin> $FirstOperationalEvent.Message
The local configuration manager was shut down.
PS C:\Users\Admin> $FirstOperationalEvent = $AllDscOpEvents[1]
PS C:\Users\Admin> $FirstOperationalEvent.Message
The DscTimer has successfully run LCM method PerformRequiredConfigurationChecks with flag 1
下面是一个脚本,用来收集三种不同类型的消息:
<##Step1 : Enable analytic and debug DSC channels (Operational channel is enabled bydefault)##>

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

<#Step2 : Perform the required DSC operation (Below is an example, you could run any DSC operation instead)#>
Get-DscLocalConfigurationManager
<##Step3 : 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)

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

那么这些信息被收集后,如何进行查找,分类和分析呢?
首先看看$SeparateDscOperationsCount里面包含了什么信息。

PS C:\> $SeparateDscOperationsCount
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-DSCTimeCreated Id LevelDisplayName Message ----------- -- ---------------- -------12/2/20133:47:29 PM 4115 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4198 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4114 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4102 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4098 Warning Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4098 Warning Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4176 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4182 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4182 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4182 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4182 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4182 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4182 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4182 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ... 12/2/20133:47:29 PM 4182 Information Job{1A776B6A-5BAC-11E3-BF41-00155D553612} : ...

然后,对这些信息进行不同维度的查询。

#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 得到最后一个小时的信息

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 最后一个操作的消息

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 最近一次失败的消息

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


PS C:\>$myFailedEvent.MessageJob {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 一个贴吧JobID的信息的聚合

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

ProviderName: Microsoft-Windows-DSCTimeCreated Id LevelDisplayName Message 
----------- -- ---------------- ------- 
12/2/20134:33:24 PM 4102 Information Job {847A5619-5BB2-11E3-BF41-00155D553612} : 
... 12/2/20134:33:24 PM 4168 Information Job {847A5619-5BB2-11E3-BF41-00155D553612} : 
... 12/2/20134:33:24 PM 4146 Information Job {847A5619-5BB2-11E3-BF41-00155D553612} : 
... 12/2/20134:33:24 PM 4120 Information Job {847A5619-5BB2-11E3-BF41-00155D553612} : ...

使用xDscDiagnostics的DSC 扩展的Module

xDscDiagnostics 是一个Powershell的module,其中包含了一些cmdlet,能够帮助我们分析DSC失败的原因。xDscDiagnostics 提供的函数,能够从本地的事件中识别出DSC的操作;同时通过一个有效的认证证书(credentials)还能远程连接到远程的其他机器上;在这里先解释一下什么是DSC 操作(Operation),DSC操作是用来定义单个的唯一的从头至尾的DSC的执行过程;比如Test-DscConfiguration
就是一个单独的DSC操作;类似的,每一个DSC的cmdlet,比如Get-DSCConfiguration,Start-DSCConfiguration等等,都是一个独立的DSC的操作;如果想查看如何使用,可以输入命令 Get-Help <cmdlet的名字>;安装xDscDiagnostics 也很简单,在DSC 5.x 直接在Powershell中输入下面的命令: Install-Module -Name xDscDiagnostics ,其会自动安装。

@得到DSC操作的详细信息

通过Get-xDSCOperation 方法,我们可以找出DSC操作在一台或者多台机器上的执行结果;其会返回一个PowerShell的对象:包含了每次DSC操作的集合;比如,下面的输出,有三个命令被执行,其中1个成功,2个失败。
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...You can also specify that you want only results for the most recent operations by using the Newest parameter:


同时,我们也可以通过Get-xDscOperation -Newest 5 得到最新的5条记录
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返回一个事件Powershell对象集合,其包含了事件类型,DSC操作的消息输出。这个命令在发生操作失败时特别有用。我可以使用Trace-xDscOperation -SequenceID 命令得到具体的错误信息。下面是一个具体的例子。

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.

另外,也可以通过传递一个GUID的值给参数JobID,从而获得其具体的错误信息。

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聚集了Analytic, Debug, and Operational l三种类型的日志,其将会提示我们是否把上面三种log生效。另外,我们也能把Trace-xDSCOperation其输出的信息保存到一个变量。我们可以通过下面的命令进行查看消息。

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

PS C:\DiagnosticsTest> $gs.Event

其会和Get-WinEvent cmdlet显示一样的结果,另外,其实,左边输出的信息其实是通过out-default | out-host 直接输出到屏幕上的,因为受限制于屏幕的尺寸其显示的消息会出现省略号(...),为了能把全部详细信息都输出来,可以尝试把其信息通过管道导入xml文件或者csv文件,比如,$gs=Get-xDscOperation;$gs.AllEvents | Export-Clixml 1.xml

   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.

@从远处计算机上获取DSC的事件

使用ComputerName参数,可以获取远程计算机的详细DSC事件信息。在使用这个命令之前,我们需要在远程计算机上修改防火墙的设置,使用的命令如下:

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

下面是一个具体的例子:

PS C:\DiagnosticsTest> 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...

当资源不能更新时,如何重置缓存(cache)

DSC的引擎缓存是为了提高DSC的执行效率。然而,这可能会引起问题:当我们更新了DSC的resource并测试通过了,新的版本不会立即生效;如果要生效,必须等到程序重启;能让DSC引擎重启的唯一方法是显式的终止DSC引擎,从而能够加载新的版本的DSC资源。

类似的,当我们运行start-DscConfiguration时,当我们添加或者修改一个定制的资源的时候,修改并不会立即执行;除非计算机重启。这是因为DSC是运行在WMI Provider主机进程之上的(WmiPrvSE),通常,这里有许多WmiPrvSE的实例会运行一次;当我们重启的时候,缓存就会被清除。

为了成功的清除缓存,另外一种方法,我们必须先找到,停止并重启守护进程。 我们必须辨识出进程,并停止和重启进程。或者我们可以使用DebugMode,重新加载DSC的资源。可以通过下面的命令重启DSC的守护进程。
###
### find the process that is hosting the DSC engine
###
$dscProcessID = Get-WmiObject msft_providers | 
Where-Object {$_.provider -like 'dsccore'} | 
Select-Object -ExpandProperty HostProcessIdentifier 

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

使用Debug模式

我们配置DSC的LCM为DebugMode 模式,守护进程重启后,使用DebugMode总会清除缓存。当我们设置DebugMode为TRUE时,其将会促使DSC引擎重新加载DSC的资源;一旦我们重写了我们的资源,我们又可以把DebugMode设置回FALSE;这样缓存又能生效了。

下面是一个例子,首先先查看其默认值。

PS C:\> Get-DscLocalConfigurationManager


AllowModuleOverwrite           : False
CertificateID                  : 
ConfigurationID                : 
ConfigurationMode              : ApplyAndMonitor
ConfigurationModeFrequencyMins : 30
Credential                     : 
DebugMode                      : False
DownloadManagerCustomData      : 
DownloadManagerName            : 
LocalConfigurationManagerState : Ready
RebootNodeIfNeeded             : False
RefreshFrequencyMins           : 15
RefreshMode                    : PUSH
PSComputerName                 :You can see that DebugMode is set to FALSE.

我们可以通过下面的方式进行设置。

function Get-TargetResource
{
    param
    (
        [Parameter(Mandatory)]
        $onlyProperty
    )
    return @{onlyProperty = Get-Content -Path "$env:SystemDrive\OutputFromTestProviderDebugMode.txt"}
}
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.

现在,我们使用下面的脚本更新Provider的代码:

$newResourceOutput = Get-Random -Minimum 5 -Maximum 30
$content = @"
function Get-TargetResource
{
    param
    (
        [Parameter(Mandatory)]
        `$onlyProperty
    )
    return @{onlyProperty = Get-Content -Path "$env:SystemDrive\OutputFromTestProviderDebugMode.txt"}
}
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 "C:\Program Files\WindowsPowerShell\Modules\MyPowerShellModules\DSCResources\TestProviderDebugMode\TestProviderDebugMode.psm1

这个搅拌生成了一个随机数并更新了相应的Provider. 当我们把DebugMode设置成False后,$env:SystemDrive\OutputFromTestProviderDebugMode.txt”的内容就永远也不会发生变化了。设置 DebugMode to TRUE 的脚本如下:

LocalConfigurationManager
{
    DebugMode = $true
}

当我们把上面DSC的LCM的DebugMode设置成TRUE时,我们会发现其文件的内容每次都会发生变化。我们可以通过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

使用WireShark进行消息的监听

使用wireShark也可以进行错误的排查。具体做法是,在Pull服务器端打开Wireshark软件。 在过滤条件中输入 port 8080 这样的话,就能抓取所以目标节点往Pull服务器上的8080端口的通信信息。具体分析,请读者参考下一篇的文章。敬请期待。。。。。

在View的菜单中选择Show Analytic and Debug Logs. 这样的话,就自动开

参考文献:

https://msdn.microsoft.com/en-us/powershell/dsc/troubleshooting

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:385167次
    • 积分:6337
    • 等级:
    • 排名:第4069名
    • 原创:227篇
    • 转载:0篇
    • 译文:5篇
    • 评论:157条
    博客专栏