1. 程式人生 > >[22]Window PowerShell DSC學習系列---- 如何用PowerShell指令碼檢視DSC伺服器的執行報表(Report)

[22]Window PowerShell DSC學習系列---- 如何用PowerShell指令碼檢視DSC伺服器的執行報表(Report)

本文只適合DSC 5.x的版本。我們知道在DSC 5.x的版本中,Pull伺服器和Report是安裝在一起的。那麼當目標節點註冊成功到Pull和Report伺服器之後,且Pull伺服器上的MOF和Resource檔案都部署好之後,目標節點就會間隔固定的時間到Pull伺服器上去取MOF配置檔案。第一次取到相對於的MOF檔案後,就會立馬執行,並把相應的執行結果返回給Report伺服器(成功的話,就是成功的輸出資訊,失敗的話,就會包括失敗的錯誤資訊),那麼這個時候,我應該如何檢視執行特定客戶節點返回的結果呢?有很多的方式。下面筆者就給大家介紹3中。

@通過瀏覽器直接訪問

在瀏覽器中輸入下面的地址,其中pullserver.example.com為伺服器的地址。

http://pullserver.example.com:8080/PSDSCPullServer.svc/Nodes(AgentId='199404F3-E202-11E6-80B8-BE117D36B7A4')/Reports

那麼AgentId如何知道的呢? 登陸目標節點機器,pullClient.example.com,執行下面的命令,就可以得到上面的AgentID

PS C:\DSC> $((glcm).AgentId)
199404F3-E202-11E6-80B8-BE117D36B7A4


@通過PowerShell指令碼訪問

執行下面的PowerShell指令碼,可以獲得其report的結果。 function GetReport
{
    param($AgentId = "$((glcm).AgentId)", $serviceURL = "http://pullserver.example.com:8080/PSDSCPullServer.svc")
    $requestUri = "$serviceURL/Nodes(AgentId= '$AgentId')/Reports"
    $request = Invoke-WebRequest -Uri $requestUri  -ContentType "application/json;odata=minimalmetadata;streaming=true;charset=utf-8" `
               -UseBasicParsing -Headers @{Accept = "application/json";ProtocolVersion = "2.0"} `
               -ErrorAction SilentlyContinue -ErrorVariable ev
    $request.content
    $object = ConvertFrom-Json $request.content


    return $object.value
}
GetReport

@通過Fiddler訪問

開啟Fiddler 4, 然後點選Composer tab,在字的Raw Tab中輸入下面的內容後,點選Execute按鈕。就能檢視到返回結果。 GET http://pullserver.example.com:8080/PSDSCPullServer.svc/Nodes(AgentId='199404F3-E202-11E6-80B8-BE117D36B7A4')/Reports HTTP/1.1
Host: pull51w2k12nssl:8080 Connection: keep-alive
Upgrade-Insecure-Requests: 1
ProtocolVersion: 2.0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4


如果是Basic認證,還需要加上Authorization的HTTP認證頭,類似於下面

Authorization: Basic YWRtaW46dGVzdA==

其中YWRtaW46dGVzdA==是"admin:test" 做base64 編碼之後生成的。

@通過直接檢視Report的資料庫

首先把Pull伺服器的IIS暫時停止一下,然後把其預設的資料庫檔案拷貝到桌面

C:\Program Files\WindowsPowerShell\DscService\Devices.edb

下載一個edb的資料庫的檢視器軟體,比如ESEDatabaseView

然後用其開啟Devices.edb檔案。進入StatusReport表,其Report結果如下:


@通過Advanced Rest Client 的Chrome瀏覽器外掛

可以通過Advanced Rest Client的Chrome的瀏覽器外掛進行訪問,所以需要先安裝Advanced Rest Client的Chrome的瀏覽器外掛。呼叫的過程如下圖所示意:

最後給大家附上部分返回結果瀏覽器,PowerShell和Fiddler返回的部分結果。

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="http://pullserver.example.com:8080/PSDSCPullServer.svc/" xmlns="http://www.w3.org/2005/Atom"
	xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
	xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
	xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
	<id>http://pullserver.example.com:8080/PSDSCPullServer.svc/Nodes(AgentId='199404F3-E202-11E6-80B8-BE117D36B7A4')/Reports
	</id>
	<title type="text">Reports</title>
	<updated>2017-02-14T05:16:18Z</updated>
	<link rel="self" title="Reports" href="Reports" />
	<entry>
		<id>http://pullserver.example.com:8080/PSDSCPullServer.svc/Reports('015c5282-f265-11e6-80bc-c528ec5710e7')
		</id>
		<category term="MSFT.DSCReport"
			scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
		<link rel="edit" title="DSCReport"
			href="Reports('015c5282-f265-11e6-80bc-c528ec5710e7')" />
		<link
			rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Nodes"
			type="application/atom+xml;type=entry" title="Nodes"
			href="Reports('015c5282-f265-11e6-80bc-c528ec5710e7')/Nodes" />
		<title />
		<updated>2017-02-14T05:16:18Z</updated>
		<author>
			<name />
		</author>
		<content type="application/xml">
			<m:properties>
				<d:JobId>015c5282-f265-11e6-80bc-c528ec5710e7</d:JobId>
				<d:OperationType>LocalConfigurationManager</d:OperationType>
				<d:RefreshMode>Pull</d:RefreshMode>
				<d:Status>Failure</d:Status>
				<d:ReportFormatVersion>2.0</d:ReportFormatVersion>
				<d:ConfigurationVersion>2.0.0</d:ConfigurationVersion>
				<d:StartTime m:type="Edm.DateTime">2017-02-14T03:23:51.982
				</d:StartTime>
				<d:EndTime m:type="Edm.DateTime">2017-02-14T03:23:53.982</d:EndTime>
				<d:RebootRequested>False</d:RebootRequested>
				<d:Errors m:type="Collection(Edm.String)" />
				<d:StatusData m:type="Collection(Edm.String)">
					<d:element>{"StartDate":"2017-02-14T03:23:51.9820000+00:00","Error":"Registration
						of the Dsc Agent with the server
						http://pull51w2k12nssl:8080/PSDSCPullServer.svc failed. The
						underlying error is: Cannot register at
						http://pull51w2k12nssl:8080/PSDSCPullServer.svc. Registering over
						HTTP is not allowed. To ensure security in registration, use an
						HTTPS address for the ServerUrl in the ConfigurationRepository of
						the LocalConfigurationManager resource for this device. If you
						understand the security implications of using HTTP and want to
						allow its use on this device, set AllowUnsecureConnection property
						to true in the ConfigurationRepository of the
						LocalConfigurationManager resource for this device.
						.","IPV6Addresses":["fe80::55d7:2403:fd6a:568f%12","::2000:0:0:0","::1","::2000:0:0:0","2001:0:338c:2b9f:18e3:44:3f57:feaf","fe80::18e3:44:3f57:feaf%14"],"DurationInSeconds":"2","JobID":"{015C5282-F265-11E6-80BC-C528EC5710E7}","MACAddresses":["FA-16-3E-BC-BF-52","00-00-00-00-00-00-00-E0","00-00-00-00-00-00-00-E0"],"MetaData":"Author:
						Admin; Name: PullClientConfigNames; Version: 2.0.0;
						GenerationDate: 02/14/2017 03:23:51; GenerationHost:
						DSCC51W2008NSSL;","Status":"Failure","IPV4Addresses":["192.168.1.80","127.0.0.1"],"LCMVersion":"2.0","CurrentChecksum":"22250721B388995018BD8A76BC89CF96AA4A2B65492DCCC10712A8D22566B273","NumberOfResources":"0","Type":"LocalConfigurationManager","HostName":"DSCC51W2008NSSL","RebootRequested":"False","Mode":"Pull","Locale":"en-US"}
					</d:element>
				</d:StatusData>
				<d:AdditionalData m:type="Collection(MSFT.PropertyBag)">
					<d:element>
						<d:Key>OSVersion</d:Key>
						<d:Value>{"VersionString":"Microsoft Windows NT
							6.3.9600.0","ServicePack":"","Platform":"Win32NT"}</d:Value>
					</d:element>
					<d:element>
						<d:Key>PSVersion</d:Key>
						<d:Value>{"CLRVersion":"4.0.30319.42000","PSVersion":"5.1.14409.1005","BuildVersion":"10.0.14409.1005"}
						</d:Value>
					</d:element>
				</d:AdditionalData>
			</m:properties>
		</content>
	</entry>
	<entry>
		<id>http://pullserver.example.com:8080/PSDSCPullServer.svc/Reports('a7c972f0-f26e-11e6-80bc-c528ec5710e7')
		</id>
		<category term="MSFT.DSCReport"
			scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
		<link rel="edit" title="DSCReport"
			href="Reports('a7c972f0-f26e-11e6-80bc-c528ec5710e7')" />
		<link
			rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Nodes"
			type="application/atom+xml;type=entry" title="Nodes"
			href="Reports('a7c972f0-f26e-11e6-80bc-c528ec5710e7')/Nodes" />
		<title />
		<updated>2017-02-14T05:16:18Z</updated>
		<author>
			<name />
		</author>
		<content type="application/xml">
			<m:properties>
				<d:JobId>a7c972f0-f26e-11e6-80bc-c528ec5710e7</d:JobId>
				<d:OperationType>Consistency</d:OperationType>
				<d:RefreshMode>Pull</d:RefreshMode>
				<d:Status>Success</d:Status>
				<d:ReportFormatVersion>2.0</d:ReportFormatVersion>
				<d:ConfigurationVersion>2.0.0</d:ConfigurationVersion>
				<d:StartTime m:type="Edm.DateTime">2017-02-14T04:32:56.701
				</d:StartTime>
				<d:EndTime m:type="Edm.DateTime">2017-02-14T04:33:04.701</d:EndTime>
				<d:RebootRequested>False</d:RebootRequested>
				<d:Errors m:type="Collection(Edm.String)" />
				<d:StatusData m:type="Collection(Edm.String)">
					<d:element>{"StartDate":"2017-02-14T04:32:56.7010000+00:00","IPV6Addresses":["fe80::55d7:2403:fd6a:568f%12","::2000:0:0:0","::1","::2000:0:0:0","2001:0:338c:2b9f:185f:b3d:3f57:feaf","fe80::185f:b3d:3f57:feaf%14"],"DurationInSeconds":"8","JobID":"{A7C972F0-F26E-11E6-80BC-C528EC5710E7}","CurrentChecksum":"22250721B388995018BD8A76BC89CF96AA4A2B65492DCCC10712A8D22566B273","MetaData":"Author:
						Admin; Name: testzip; Version: 2.0.0; GenerationDate: 02/14/2017
						03:20:09; GenerationHost:
						PULL51W2K12NSSL;","RebootRequested":"False","Status":"Success","IPV4Addresses":["192.168.1.80","127.0.0.1"],"LCMVersion":"2.0","NumberOfResources":"1","Type":"Consistency","HostName":"DSCC51W2008NSSL","ResourcesInDesiredState":[{"SourceInfo":"::5::7::Archive","ModuleName":"PSDesiredStateConfiguration","DurationInSeconds":"0.391","InstanceName":"ArchiveExample","StartDate":"2017-02-14T04:33:04.9510000+00:00","ResourceName":"Archive","ModuleVersion":"1.1","RebootRequested":"False","ResourceId":"[Archive]ArchiveExample","ConfigurationName":"testzip","InDesiredState":"True"}],"MACAddresses":["FA-16-3E-BC-BF-52","00-00-00-00-00-00-00-E0","00-00-00-00-00-00-00-E0"],"MetaConfiguration":{"AgentId":"199404F3-E202-11E6-80B8-BE117D36B7A4","ConfigurationDownloadManagers":[{"SourceInfo":"::15::9::ConfigurationRepositoryWeb","AllowUnsecureConnection":"True","ServerURL":"http://pull51w2k12nssl:8080/PSDSCPullServer.svc","RegistrationKey":"","ResourceId":"[ConfigurationRepositoryWeb]pull51w2k12nssl","ConfigurationNames":["unzipFile"]}],"ActionAfterReboot":"ContinueConfiguration","LCMCompatibleVersions":["1.0","2.0"],"LCMState":"Idle","ResourceModuleManagers":[],"ReportManagers":[{"AllowUnsecureConnection":"True","RegistrationKey":"","ServerURL":"http://pull51w2k12nssl:8080/PSDSCPullServer.svc","ResourceId":"[ReportServerWeb]pull51w2k12nssl","SourceInfo":"::24::9::ReportServerWeb"}],"StatusRetentionTimeInDays":"10","LCMVersion":"2.0","MaximumDownloadSizeMB":"500","ConfigurationMode":"ApplyAndAutoCorrect","RefreshFrequencyMins":"30","RebootNodeIfNeeded":"True","SignatureValidationPolicy":"NONE","RefreshMode":"Pull","DebugMode":["NONE"],"LCMStateDetail":"","AllowModuleOverwrite":"False","ConfigurationModeFrequencyMins":"15","SignatureValidations":[]},"Locale":"en-US","Mode":"Pull"}
					</d:element>
				</d:StatusData>
				<d:AdditionalData m:type="Collection(MSFT.PropertyBag)">
					<d:element>
						<d:Key>OSVersion</d:Key>
						<d:Value>{"VersionString":"Microsoft Windows NT
							6.3.9600.0","ServicePack":"","Platform":"Win32NT"}</d:Value>
					</d:element>
					<d:element>
						<d:Key>PSVersion</d:Key>
						<d:Value>{"CLRVersion":"4.0.30319.42000","PSVersion":"5.1.14409.1005","BuildVersion":"10.0.14409.1005"}
						</d:Value>
					</d:element>
				</d:AdditionalData>
			</m:properties>
		</content>
	</entry>
</feed>