ConvertTo-CliXml
將物件轉換成 CliXml 格式的字串。
語法
Default (預設值)
ConvertTo-CliXml
[-InputObject] <PSObject>
[-Depth <Int32>]
[<CommonParameters>]
Description
ConvertTo-CliXml Cmdlet 會將物件轉換成格式化為 Common Language Infrastructure (CLI) XML 的字串。 此命令類似於 Export-Clixml,但不會寫入檔案。 相反地,它會輸出字串。
此 Cmdlet 是在 PowerShell 7.5-preview.4 中引進的。
範例
範例 1 - 將進程物件轉換為 CliXml 和返回
此範例顯示將進程物件轉換成 CliXml 和返回的結果。 首先,目前的進程會儲存在變數中 $process。 進程物件的 pstypenames 屬性會顯示物件的類型 為 system.Diagnostics.Process類型。 下一個命令會顯示進程物件中每個成員類型的計數。
$process = Get-Process -Id $PID
$process.pstypenames
System.Diagnostics.Process
System.ComponentModel.Component
System.MarshalByRefObject
System.Object
$process | Get-Member | Group-Object MemberType | Select-Object Name, Count
Name Count
---- -----
AliasProperty 7
CodeProperty 1
Property 52
NoteProperty 1
ScriptProperty 8
PropertySet 2
Method 19
Event 4
$xml = $process | ConvertTo-CliXml
$fromXML = ConvertFrom-CliXml $xml
$fromXML.pstypenames
Deserialized.System.Diagnostics.Process
Deserialized.System.ComponentModel.Component
Deserialized.System.MarshalByRefObject
Deserialized.System.Object
$fromXML | Get-Member | Group-Object MemberType | Select-Object Name, Count
Name Count
---- -----
Property 46
NoteProperty 17
PropertySet 2
Method 2
接下來,程序物件會轉換成 CliXml 並再轉換回來。 新 物件的類型前面會加上 Deserialized。 新物件中的成員計數與原始物件不同。
參數
-Depth
指定在 XML 表示中包含多少層次的嵌套物件。 預設值為 2。
參數屬性
| 類型: | Int32 |
| 預設值: | 2 |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-InputObject
要轉換成 CliXml 格式字串的物件。
參數屬性
| 類型: | PSObject |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。