使用 DSC 報表伺服器
適用於:Windows PowerShell 5.0
重要
提取伺服器 (Windows 功能「DSC 服務」) 是支援的 Windows Server 元件,但未計劃提供新特性或功能。 我們希望您知道現在已正式推出較新版本的 DSC,由具名客體設定 Azure 原則 功能管理。 來賓設定服務結合 DSC 延伸模組、Azure 自動化狀態設定,以及客戶意見反應最常要求的功能。 來賓設定也包含透過已啟用 Arc 的伺服器的混合式電腦支援。
節點的本機設定管理員 (LCM) 可以設定為將其設定狀態相關報表傳送至提取伺服器,然後可查詢以擷取該資料。 每次節點檢查並套用設定時,皆會將報表傳送至報表伺服器。 這些報表會儲存在伺服器上的資料庫,而且可以藉由呼叫報告 Web 服務來擷取。 每份報表包含已套用的設定、是否成功套用、使用的資源、所擲回的任何錯誤,以及開始和完成時間等資訊。
設定要傳送報表的節點
您可告知節點將報表傳送至伺服器,方法是使用該節點 LCM 設定內的 ReportServerWeb 區塊 (如需關於設定 LCM 的相關資訊,請參閱設定本機設定管理員)。 節點傳送報表的目標伺服器必須設定為 Web 提取伺服器 (您無法將報表傳送至 SMB 共用)。 如需設定提取伺服器的資訊,請參閱設定 DSC Web 提取伺服器。 報表伺服器的服務可以與節點從中提取設定和取得資源的服務相同,或可以是不同的服務。
在 ReportServerWeb 區塊中,您可指定提取服務的 URL 和該伺服器已知的註冊金鑰。
下列設定會設定節點從一項服務提取設定,並傳送報表至不同伺服器上的服務。
[DSCLocalConfigurationManager()]
configuration ReportClientConfig
{
Node localhost
{
Settings
{
RefreshMode = 'Pull'
RefreshFrequencyMins = 30
RebootNodeIfNeeded = $true
}
ConfigurationRepositoryWeb CONTOSO-PullSrv
{
ServerURL = 'https://CONTOSO-PULL:8080/PSDSCPullServer.svc'
RegistrationKey = 'bbb9778f-43f2-47de-b61e-a0daff474c6d'
ConfigurationNames = @('ClientConfig')
}
ReportServerWeb CONTOSO-ReportSrv
{
ServerURL = 'http://CONTOSO-REPORT:8080/PSDSCPullServer.svc'
RegistrationKey = 'ba39daaa-96c5-4f2f-9149-f95c46460faa'
AllowUnsecureConnection = $true
}
}
}
ReportClientConfig
下列設定會設定節點針對設定、資源和報告使用單一伺服器。
[DSCLocalConfigurationManager()]
configuration PullClientConfig
{
Node localhost
{
Settings
{
RefreshMode = 'Pull'
RefreshFrequencyMins = 30
RebootNodeIfNeeded = $true
}
ConfigurationRepositoryWeb CONTOSO-PullSrv
{
ServerURL = 'https://CONTOSO-PullSrv:8080/PSDSCPullServer.svc'
RegistrationKey = 'fbc6ef09-ad98-4aad-a062-92b0e0327562'
}
ReportServerWeb CONTOSO-ReportSrv
{
ServerURL = 'https://CONTOSO-PullSrv:8080/PSDSCPullServer.svc'
}
}
}
PullClientConfig
注意
當您設定提取伺服器時,可以為 Web 服務指定任何名稱,但 ServerURL 屬性必須符合服務名稱。
取得報表資料
傳送到提取伺服器的報表會輸入到該伺服器上的資料庫中。 可透過呼叫 Web 服務使用報表。 若要擷取特定節點的報告,請以下列形式將 HTTP 要求傳送到報表 Web 服務:
http://CONTOSO-REPORT:8080/PSDSCReportServer.svc/Nodes(AgentId='MyNodeAgentId')/Reports
其中 MyNodeAgentId
是您要取得報表之節點的 AgentId。 您也可以呼叫該節點上的 Get-DscLocalConfigurationManager 以取得節點的 AgentID。
報表會以 JSON 物件的陣列傳回。
下列指令碼會傳回執行所在之節點的報表:
function GetReport
{
param
(
$AgentId = "$((glcm).AgentId)",
$serviceURL = "http://CONTOSO-REPORT:8080/PSDSCPullServer.svc"
)
$invokeWebRequestSplat = @{
Uri = "$serviceURL/Nodes(AgentId= '$AgentId')/Reports"
ContentType = "application/json;odata=minimalmetadata;streaming=true;charset=utf-8"
UseBasicParsing = $true
Headers = @{Accept = "application/json"; ProtocolVersion = "2.0"}
ErrorAction = 'SilentlyContinue'
ErrorVariable = 'ev'
}
$request = Invoke-WebRequest @invokeWebRequestSplat
$object = ConvertFrom-Json $request.content
return $object.value
}
檢視報表資料
如果您將變數設為 GetReport 函式的結果,您就可以在傳回陣列的項目中檢視個別欄位:
$reports = GetReport
$reports[1]
JobId : 019dfbe5-f99f-11e5-80c6-001dd8b8065c
OperationType : Consistency
RefreshMode : Pull
Status : Success
ReportFormatVersion : 2.0
ConfigurationVersion : 2.0.0
StartTime : 04/03/2016 06:21:43
EndTime : 04/03/2016 06:22:04
RebootRequested : False
Errors : {}
StatusData : {{"StartDate":"2016-04-03T06:21:43.7220000-07:00","IPV6Addresses":["2001:4898:d8:f2f2:852b:b255:b071:283b","fe80::852b:b255:b071
:283b%12","::2000:0:0:0","::1","::2000:0:0:0"],"DurationInSeconds":"21","JobID":"{019DFBE5-F99F-11E5-80C6-001DD8B8065C}","Curren
tChecksum":"A7797571CB9C3AF4D74C39A0FDA11DAF33273349E1182385528FFC1E47151F7F","MetaData":"Author: configAuthor; Name:
Sample_ArchiveFirewall; Version: 2.0.0; GenerationDate: 04/01/2016 15:23:30; GenerationHost: CONTOSO-PullSrv;","RebootRequested":"False
","Status":"Success","IPV4Addresses":["10.240.179.151","127.0.0.1"],"LCMVersion":"2.0","ResourcesNotInDesiredState":[{"SourceInf
o":"C:\\ReportTest\\Sample_xFirewall_AddFirewallRule.ps1::23::9::xFirewall","ModuleName":"xNetworking","DurationInSeconds":"8.785",
"InstanceName":"Firewall","StartDate":"2016-04-03T06:21:56.4650000-07:00","ResourceName":"xFirewall","ModuleVersion":"2.7.0.0","
RebootRequested":"False","ResourceId":"[xFirewall]Firewall","ConfigurationName":"Sample_ArchiveFirewall","InDesiredState":"False
"}],"NumberOfResources":"2","Type":"Consistency","HostName":"CONTOSO-PULLCLI","ResourcesInDesiredState":[{"SourceInfo":"C:\\ReportTest\\Sample_xFirewall_AddFirewallRule.ps1::16::9::Archive","ModuleName":"PSDesiredStateConfiguration","DurationInSeconds":"1.848",
"InstanceName":"ArchiveExample","StartDate":"2016-04-03T06:21:56.4650000-07:00","ResourceName":"Archive","ModuleVersion":"1.1","
RebootRequested":"False","ResourceId":"[Archive]ArchiveExample","ConfigurationName":"Sample_ArchiveFirewall","InDesiredState":"T
rue"}],"MACAddresses":["00-1D-D8-B8-06-5C","00-00-00-00-00-00-00-E0"],"MetaConfiguration":{"AgentId":"52DA826D-00DE-4166-8ACB-73F2B46A7E00",
"ConfigurationDownloadManagers":[{"SourceInfo":"C:\\ReportTest\\LCMConfig.ps1::14::9::ConfigurationRepositoryWeb","A
llowUnsecureConnection":"True","ServerURL":"http://CONTOSO-PullSrv:8080/PSDSCPullServer.svc","RegistrationKey":"","ResourceId":"[Config
urationRepositoryWeb]CONTOSO-PullSrv","ConfigurationNames":["ClientConfig"]}],"ActionAfterReboot":"ContinueConfiguration","LCMCo
mpatibleVersions":["1.0","2.0"],"LCMState":"Idle","ResourceModuleManagers":[],"ReportManagers":[{"AllowUnsecureConnection":"True
","RegistrationKey":"","ServerURL":"http://CONTOSO-PullSrv:8080/PSDSCPullServer.svc","ResourceId":"[ReportServerWeb]CONTOSO-PullSrv","S
ourceInfo":"C:\\ReportTest\\LCMConfig.ps1::24::9::ReportServerWeb"}],"StatusRetentionTimeInDays":"10","LCMVersion":"2.0","Config
urationMode":"ApplyAndMonitor","RefreshFrequencyMins":"30","RebootNodeIfNeeded":"True","RefreshMode":"Pull","DebugMode":["NONE"]
,"LCMStateDetail":"","AllowModuleOverwrite":"False","ConfigurationModeFrequencyMins":"15"},"Locale":"en-US","Mode":"Pull"}}
AdditionalData : {}
根據預設,報表會依 JobID 排序。 若要取得最新報表,您可以依 StartTime 屬性遞減排序報表,然後取得陣列的第一個項目︰
$reportsByStartTime = $reports | Sort-Object {$_."StartTime" -as [DateTime] } -Descending
$reportMostRecent = $reportsByStartTime[0]
請注意,StatusData 屬性是具有一些屬性的物件。 這是大部分報告資料的所在位置。 讓我們看看最新報表之 StatusData 屬性的個別欄位:
$statusData = $reportMostRecent.StatusData | ConvertFrom-Json
$statusData
StartDate : 2016-04-04T11:21:41.2990000-07:00
IPV6Addresses : {2001:4898:d8:f2f2:852b:b255:b071:283b, fe80::852b:b255:b071:283b%12, ::2000:0:0:0, ::1...}
DurationInSeconds : 25
JobID : {135D230E-FA92-11E5-80C6-001DD8B8065C}
CurrentChecksum : A7797571CB9C3AF4D74C39A0FDA11DAF33273349E1182385528FFC1E47151F7F
MetaData : Author: configAuthor; Name: Sample_ArchiveFirewall; Version: 2.0.0; GenerationDate: 04/01/2016 15:23:30; GenerationHost:
CONTOSO-PullSrv;
RebootRequested : False
Status : Success
IPV4Addresses : {10.240.179.151, 127.0.0.1}
LCMVersion : 2.0
ResourcesNotInDesiredState : {@{SourceInfo=C:\ReportTest\Sample_xFirewall_AddFirewallRule.ps1::23::9::xFirewall; ModuleName=xNetworking;
DurationInSeconds=10.725; InstanceName=Firewall; StartDate=2016-04-04T11:21:55.7200000-07:00; ResourceName=xFirewall;
ModuleVersion=2.7.0.0; RebootRequested=False; ResourceId=[xFirewall]Firewall; ConfigurationName=Sample_ArchiveFirewall;
InDesiredState=False}}
NumberOfResources : 2
Type : Consistency
HostName : CONTOSO-PULLCLI
ResourcesInDesiredState : {@{SourceInfo=C:\ReportTest\Sample_xFirewall_AddFirewallRule.ps1::16::9::Archive; ModuleName=PSDesiredStateConfiguration;
DurationInSeconds=2.672; InstanceName=ArchiveExample; StartDate=2016-04-04T11:21:55.7200000-07:00; ResourceName=Archive;
ModuleVersion=1.1; RebootRequested=False; ResourceId=[Archive]ArchiveExample; ConfigurationName=Sample_ArchiveFirewall;
InDesiredState=True}}
MACAddresses : {00-1D-D8-B8-06-5C, 00-00-00-00-00-00-00-E0}
MetaConfiguration : @{AgentId=52DA826D-00DE-4166-8ACB-73F2B46A7E00; ConfigurationDownloadManagers=System.Object[];
ActionAfterReboot=ContinueConfiguration; LCMCompatibleVersions=System.Object[]; LCMState=Idle;
ResourceModuleManagers=System.Object[]; ReportManagers=System.Object[]; StatusRetentionTimeInDays=10; LCMVersion=2.0;
ConfigurationMode=ApplyAndMonitor; RefreshFrequencyMins=30; RebootNodeIfNeeded=True; RefreshMode=Pull;
DebugMode=System.Object[]; LCMStateDetail=; AllowModuleOverwrite=False; ConfigurationModeFrequencyMins=15}
Locale : en-US
Mode : Pull
除了其他屬性,這還會顯示最新設定呼叫的兩項資源,其中一項處於預期狀態,另一項處於非預期狀態。 您可取得僅限 ResourcesNotInDesiredState 屬性之更容易讀取的輸出:
$statusData.ResourcesInDesiredState
SourceInfo : C:\ReportTest\Sample_xFirewall_AddFirewallRule.ps1::16::9::Archive
ModuleName : PSDesiredStateConfiguration
DurationInSeconds : 2.672
InstanceName : ArchiveExample
StartDate : 2016-04-04T11:21:55.7200000-07:00
ResourceName : Archive
ModuleVersion : 1.1
RebootRequested : False
ResourceId : [Archive]ArchiveExample
ConfigurationName : Sample_ArchiveFirewall
InDesiredState : True
請注意,這些範例主要供您了解可以如何處理報表資料。 如需在 PowerShell 中搭配使用 JSON 的簡介,請參閱Playing with JSON and PowerShell (以 JSON 和 PowerShell 播放)。