使用Export-clixml在powershell中导出对象并使用.xml文件作为备份是否有效?
我要在我的林中大量删除数千个MailContacts,我想在删除之前备份所有MailContacts
提前致谢, O.Z
答案 0 :(得分:1)
我会说不,因为当您重新读取对象时,PowerShell不会重新创建原始的MailContact对象。它创建一个特殊类型的对象,仅表示原始对象的公共数据字段。如果您要执行此操作:
Start-Process notepad
Get-Process notepad | Export-Clixml notepad.clixml
Stop-Process -name notepad
然后像这样导入clixml文件并将其转储到屏幕上:
PS> $n = Import-Clixml .\notepad.clixml
PS> $n
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
78 7 1280 5916 96 0.06 32752 notepad
看起来您正在运行记事本进程,但Import-CliXml还没有重新创建记事本进程。您会注意到反序列化的对象没有像Kill()或WaitForExit()那样的任何Process方法。这是因为导入的对象只包含原始对象数据的快照。没有可行的方法使普通方法适用于这样的对象。您可以通过Get-Member:
运行导入的对象来查看PS> $n | Get-Member
TypeName: Deserialized.System.Diagnostics.Process
Name MemberType Definition
---- ---------- ----------
GetType Method type GetType()
ToString Method string ToString(), string ToString(string format, System.IFormatProvider for...
Company NoteProperty System.String Company=Microsoft Corporation
CPU NoteProperty System.Double CPU=0.0625
...
请注意类型名称Deserialized.System.Diagnostics.Process
。
现在这并不是说您无法使用这些对象中的数据手动重建MailContacts,但我会寻找更直接的路径。例如,您无法备份MailContacts所包含的文件 - 假设它们存储在文件中?或者可能有一个API允许将联系人保存到文件中?