如何使用 Powershell 将进程写入 XML 文件?
How to write processes to an XML file with Powershell?
将流程导出到 CSV 文件看似拍摄快照,然后将其写出。然而 XML 变体会创建一个日志文件,它似乎会无限期地写入该文件。
如何将 csv.ps1
脚本的结果写入 XML 文件?
csv.ps1
Get-Process | Export-Csv -Path .\Processes.csv -NoTypeInformation
Processes.csv,在运行之后csv.ps1
:
"writeback","0",,"0","0","0","0",,"System.Diagnostics.Process (kthreadd)",,"0",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","30","-20",,"False","2019-03-16 7:37:18 a.m.",,"30",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","RealTime","0","0","writeback","3","0",,"System.Diagnostics.ProcessThreadCollection",,"0","0","False",,,,"0","0",,,"0","","True","00:00:00","00:00:00","00:00:00",,
"xbrlapi","3233",,"0","0","0","0",,"System.Diagnostics.Process (gnome-session-b)",,"0",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","3419","0",,"False","2019-03-16 7:45:43 a.m.",,"3419",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xbrlapi","3","3233",,"System.Diagnostics.ProcessThreadCollection",,"0","0","False",,,,"0","0",,,"0","","True","00:00:00","00:00:00","00:00:00",,
"xdg-desktop-por","12972","10","573689856","8712192","0","0","/usr/libexec/xdg-desktop-portal","System.Diagnostics.Process (systemd)",,"0.06",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","12972","0",,"False","2019-03-16 10:29:22 a.m.",,"12972",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-desktop-por","3","12972",,"System.Diagnostics.ProcessThreadCollection","10","573689856","573689856","False",,,,"8712192","8712192",,"System.Diagnostics.ProcessModule (xdg-desktop-portal)","0","","True","00:00:00.0300000","00:00:00.0600000","00:00:00.0300000",,
"xdg-desktop-por","13006","10","474832896","16887808","0","0","/usr/libexec/xdg-desktop-portal-gtk","System.Diagnostics.Process (systemd)",,"0.16",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","13006","0",,"False","2019-03-16 10:29:23 a.m.",,"13006",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-desktop-por","3","13006",,"System.Diagnostics.ProcessThreadCollection","10","474832896","474832896","False",,,,"16887808","16887808",,"System.Diagnostics.ProcessModule (xdg-desktop-portal-gtk)","0","","True","00:00:00.0200000","00:00:00.1600000","00:00:00.1400000",,
"xdg-document-po","3375",,"482291712","5652480","0","0",,"System.Diagnostics.Process (systemd)",,"0.08",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","3375","0",,"False","2019-03-16 7:45:42 a.m.",,"3375",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-document-po","3","3375",,"System.Diagnostics.ProcessThreadCollection",,"482291712","482291712","False",,,,"5652480","5652480",,,"0","","True","00:00:00","00:00:00.0800000","00:00:00.0800000",,
"xdg-document-po","12985","8","482291712","5857280","0","0","/usr/libexec/xdg-document-portal","System.Diagnostics.Process (systemd)",,"0.03",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","12985","0",,"False","2019-03-16 10:29:22 a.m.",,"12985",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-document-po","3","12985",,"System.Diagnostics.ProcessThreadCollection","8","482291712","482291712","False",,,,"5857280","5857280",,"System.Diagnostics.ProcessModule (xdg-document-portal)","0","","True","00:00:00.0100000","00:00:00.0300000","00:00:00.0200000",,
"xdg-permission-","3382",,"255184896","4739072","0","0",,"System.Diagnostics.Process (systemd)",,"0",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","3382","0",,"False","2019-03-16 7:45:43 a.m.",,"3382",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-permission-","3","3382",,"System.Diagnostics.ProcessThreadCollection",,"255184896","255184896","False",,,,"4739072","4739072",,,"0","","True","00:00:00","00:00:00","00:00:00",,
"xdg-permission-","12992","7","255184896","4780032","0","0","/usr/libexec/xdg-permission-store","System.Diagnostics.Process (systemd)",,"0",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","12992","0",,"False","2019-03-16 10:29:22 a.m.",,"12992",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-permission-","3","12992",,"System.Diagnostics.ProcessThreadCollection","7","255184896","255184896","False",,,,"4780032","4780032",,"System.Diagnostics.ProcessModule (xdg-permission-store)","0","","True","00:00:00","00:00:00","00:00:00",,
"Xorg","1369",,"490586112","98332672","0","0",,"System.Diagnostics.Process (lightdm)",,"131.97",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","1369","0",,"False","2019-03-16 7:38:02 a.m.",,"1369",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","Xorg","3","1369",,"System.Diagnostics.ProcessThreadCollection",,"490586112","490586112","False",,,,"98332672","98332672",,,"0","","True","00:00:43","00:02:11.9700000","00:01:28.9700000",,
"Xorg","12550",,"499658752","84373504","0","0",,"System.Diagnostics.Process (lightdm)",,"44.97",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","12550","0",,"False","2019-03-16 10:29:11 a.m.",,"12550",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","Xorg","3","12550",,"System.Diagnostics.ProcessThreadCollection",,"499658752","499658752","False",,,,"84373504","84373504",,,"0","","True","00:00:15.0800000","00:00:44.9700000","00:00:29.8900000",,
xml.ps1
Get-Process | Export-Clixml pi.xml
在运行xml.ps1
之后pi.xml
这样结束:
<S N="BaseAddress">140154072023040</S>
<I32 N="ModuleMemorySize">208896</I32>
<S N="EntryPointAddress">0</S>
</Props>
</Obj>
</IE>
</Obj>
</Props>
</Obj>
</Objs>
(是的,我想我可以将 CSV
转换为 XML
,我想问的是如何直接在 powershell
中使用此 XML
工具而不额外的步骤。)
我想我想要一个非流媒体选项,如果这是正确的术语。
另请参阅:
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-clixml?view=powershell-6
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-clixml?view=powershell-6
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-6
Edit - At first, I answered the question as originally presented, around "why am I having issues with Export-Clixml
, but see my edit further down for a solution involving ConvertTo-Xml
.
有趣的问题!我相信答案与流媒体无关,但实际上与性能和序列化有关。
背景:
CSV 是扁平的 - 它不会序列化结构化数据。因此,它可能会很快完成,尽管保真度较低。
XML 是结构化的 - XML 导出将序列化嵌套属性,如果这些属性本身具有嵌套属性,那么您最终可能会提取大量信息来自系统
序列化程序的智能程度是有限度的,它可能会通过多次顺序调用 对管道中的每个对象 来实现这一点。 (这是应用程序开发中的一个常见问题,序列化一个对象可以从数据层中为单个对象拉出一个大图。请记住 Export-Clixml
是一个通用的序列化器;可以想象写一个只是为了序列化 Get-Process
的输出,这可能会被优化以最小化进程外调用。)
我在 Windows 的 pwsh
中进行了测试。如果我通过管道传输 Get-Process | Export-Clixml processes.xml
,那么我序列化到文件中的对象数量是我在按下 Ctrl-C 之前等待的时间的函数。在我公司的笔记本电脑上,默认深度下每个进程大约需要一秒。
在 Windows 上的 Powershell 中,一个 ProcessInfo
对象(您从 Get-Process
返回的是本地对象的包装器,它通过 COM 公开。它是.NET 到 .NET 之外的东西。为此,请阅读 进程外调用 - 这比同进程调用慢得多。如果您正在管道传输纯 .NET 对象, 那么获取嵌套属性将是一个简单的取消引用,一切都会很快完成。
对于每个进程,它将通过 COM 获取加载的 DLL,然后通过 COM 获取线程...这些在 Windows 和 *nix 上可能是昂贵的调用也是。
答案:
设置 Export-Clixml
的深度。
如果我指定深度为 1: Get-Process | Export-Clixml -Depth 1 processes.xml
,那么我会在一两秒内获得所有 运行 进程。但是我没有得到嵌套属性的完整序列化。
一般来说,序列化是保真度和性能之间的权衡。
Edit 2019-03-19
我看到您添加了一条澄清评论 - 您想要到达 XML,而不是特定的 CLI XML。
Export-Clixml
(以及相关的 [System.Management.Automation.PSSerializer]::Serialize()
)输出 XML 即 有效 ,但已针对 Powershell 序列化进行了大量优化。如果您将数据发送到某些通用服务,您更有可能从 ConvertTo-Xml
中获得更多好处,这为您提供了更典型的进程间交换文档结构。
> $Xml = Get-Process a* | ConvertTo-Xml -Depth 4 # name begins with 'a'
> $Xml
xml Objects
--- -------
version="1.0" encoding="utf-8" Objects
> $Xml.Save('C:\dev\processes.xml')
> Get-Content 'C:\dev\processes.xml' -TotalCount 10
<?xml version="1.0" encoding="utf-8"?>
<Objects>
<Object Type="System.Diagnostics.Process">
<Property Name="Name" Type="System.String">AgentUI</Property>
<Property Name="SI" Type="System.Int32">1</Property>
<Property Name="Handles" Type="System.Int32">347</Property>
<Property Name="VM" Type="System.Int64">2204067930112</Property>
<Property Name="WS" Type="System.Int64">36667392</Property>
<Property Name="PM" Type="System.Int64">50425856</Property>
<Property Name="NPM" Type="System.Int64">29800</Property>
将流程导出到 CSV 文件看似拍摄快照,然后将其写出。然而 XML 变体会创建一个日志文件,它似乎会无限期地写入该文件。
如何将 csv.ps1
脚本的结果写入 XML 文件?
csv.ps1
Get-Process | Export-Csv -Path .\Processes.csv -NoTypeInformation
Processes.csv,在运行之后csv.ps1
:
"writeback","0",,"0","0","0","0",,"System.Diagnostics.Process (kthreadd)",,"0",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","30","-20",,"False","2019-03-16 7:37:18 a.m.",,"30",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","RealTime","0","0","writeback","3","0",,"System.Diagnostics.ProcessThreadCollection",,"0","0","False",,,,"0","0",,,"0","","True","00:00:00","00:00:00","00:00:00",,
"xbrlapi","3233",,"0","0","0","0",,"System.Diagnostics.Process (gnome-session-b)",,"0",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","3419","0",,"False","2019-03-16 7:45:43 a.m.",,"3419",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xbrlapi","3","3233",,"System.Diagnostics.ProcessThreadCollection",,"0","0","False",,,,"0","0",,,"0","","True","00:00:00","00:00:00","00:00:00",,
"xdg-desktop-por","12972","10","573689856","8712192","0","0","/usr/libexec/xdg-desktop-portal","System.Diagnostics.Process (systemd)",,"0.06",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","12972","0",,"False","2019-03-16 10:29:22 a.m.",,"12972",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-desktop-por","3","12972",,"System.Diagnostics.ProcessThreadCollection","10","573689856","573689856","False",,,,"8712192","8712192",,"System.Diagnostics.ProcessModule (xdg-desktop-portal)","0","","True","00:00:00.0300000","00:00:00.0600000","00:00:00.0300000",,
"xdg-desktop-por","13006","10","474832896","16887808","0","0","/usr/libexec/xdg-desktop-portal-gtk","System.Diagnostics.Process (systemd)",,"0.16",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","13006","0",,"False","2019-03-16 10:29:23 a.m.",,"13006",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-desktop-por","3","13006",,"System.Diagnostics.ProcessThreadCollection","10","474832896","474832896","False",,,,"16887808","16887808",,"System.Diagnostics.ProcessModule (xdg-desktop-portal-gtk)","0","","True","00:00:00.0200000","00:00:00.1600000","00:00:00.1400000",,
"xdg-document-po","3375",,"482291712","5652480","0","0",,"System.Diagnostics.Process (systemd)",,"0.08",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","3375","0",,"False","2019-03-16 7:45:42 a.m.",,"3375",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-document-po","3","3375",,"System.Diagnostics.ProcessThreadCollection",,"482291712","482291712","False",,,,"5652480","5652480",,,"0","","True","00:00:00","00:00:00.0800000","00:00:00.0800000",,
"xdg-document-po","12985","8","482291712","5857280","0","0","/usr/libexec/xdg-document-portal","System.Diagnostics.Process (systemd)",,"0.03",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","12985","0",,"False","2019-03-16 10:29:22 a.m.",,"12985",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-document-po","3","12985",,"System.Diagnostics.ProcessThreadCollection","8","482291712","482291712","False",,,,"5857280","5857280",,"System.Diagnostics.ProcessModule (xdg-document-portal)","0","","True","00:00:00.0100000","00:00:00.0300000","00:00:00.0200000",,
"xdg-permission-","3382",,"255184896","4739072","0","0",,"System.Diagnostics.Process (systemd)",,"0",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","3382","0",,"False","2019-03-16 7:45:43 a.m.",,"3382",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-permission-","3","3382",,"System.Diagnostics.ProcessThreadCollection",,"255184896","255184896","False",,,,"4739072","4739072",,,"0","","True","00:00:00","00:00:00","00:00:00",,
"xdg-permission-","12992","7","255184896","4780032","0","0","/usr/libexec/xdg-permission-store","System.Diagnostics.Process (systemd)",,"0",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","12992","0",,"False","2019-03-16 10:29:22 a.m.",,"12992",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","xdg-permission-","3","12992",,"System.Diagnostics.ProcessThreadCollection","7","255184896","255184896","False",,,,"4780032","4780032",,"System.Diagnostics.ProcessModule (xdg-permission-store)","0","","True","00:00:00","00:00:00","00:00:00",,
"Xorg","1369",,"490586112","98332672","0","0",,"System.Diagnostics.Process (lightdm)",,"131.97",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","1369","0",,"False","2019-03-16 7:38:02 a.m.",,"1369",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","Xorg","3","1369",,"System.Diagnostics.ProcessThreadCollection",,"490586112","490586112","False",,,,"98332672","98332672",,,"0","","True","00:00:43","00:02:11.9700000","00:01:28.9700000",,
"Xorg","12550",,"499658752","84373504","0","0",,"System.Diagnostics.Process (lightdm)",,"44.97",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","12550","0",,"False","2019-03-16 10:29:11 a.m.",,"12550",".","9223372036854775807","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","Normal","0","0","Xorg","3","12550",,"System.Diagnostics.ProcessThreadCollection",,"499658752","499658752","False",,,,"84373504","84373504",,,"0","","True","00:00:15.0800000","00:00:44.9700000","00:00:29.8900000",,
xml.ps1
Get-Process | Export-Clixml pi.xml
在运行xml.ps1
之后pi.xml
这样结束:
<S N="BaseAddress">140154072023040</S>
<I32 N="ModuleMemorySize">208896</I32>
<S N="EntryPointAddress">0</S>
</Props>
</Obj>
</IE>
</Obj>
</Props>
</Obj>
</Objs>
(是的,我想我可以将 CSV
转换为 XML
,我想问的是如何直接在 powershell
中使用此 XML
工具而不额外的步骤。)
我想我想要一个非流媒体选项,如果这是正确的术语。
另请参阅:
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-clixml?view=powershell-6
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-clixml?view=powershell-6
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-6
Edit - At first, I answered the question as originally presented, around "why am I having issues with
Export-Clixml
, but see my edit further down for a solution involvingConvertTo-Xml
.
有趣的问题!我相信答案与流媒体无关,但实际上与性能和序列化有关。
背景:
CSV 是扁平的 - 它不会序列化结构化数据。因此,它可能会很快完成,尽管保真度较低。
XML 是结构化的 - XML 导出将序列化嵌套属性,如果这些属性本身具有嵌套属性,那么您最终可能会提取大量信息来自系统
序列化程序的智能程度是有限度的,它可能会通过多次顺序调用 对管道中的每个对象 来实现这一点。 (这是应用程序开发中的一个常见问题,序列化一个对象可以从数据层中为单个对象拉出一个大图。请记住 Export-Clixml
是一个通用的序列化器;可以想象写一个只是为了序列化 Get-Process
的输出,这可能会被优化以最小化进程外调用。)
我在 Windows 的 pwsh
中进行了测试。如果我通过管道传输 Get-Process | Export-Clixml processes.xml
,那么我序列化到文件中的对象数量是我在按下 Ctrl-C 之前等待的时间的函数。在我公司的笔记本电脑上,默认深度下每个进程大约需要一秒。
在 Windows 上的 Powershell 中,一个 ProcessInfo
对象(您从 Get-Process
返回的是本地对象的包装器,它通过 COM 公开。它是.NET 到 .NET 之外的东西。为此,请阅读 进程外调用 - 这比同进程调用慢得多。如果您正在管道传输纯 .NET 对象, 那么获取嵌套属性将是一个简单的取消引用,一切都会很快完成。
对于每个进程,它将通过 COM 获取加载的 DLL,然后通过 COM 获取线程...这些在 Windows 和 *nix 上可能是昂贵的调用也是。
答案:
设置 Export-Clixml
的深度。
如果我指定深度为 1: Get-Process | Export-Clixml -Depth 1 processes.xml
,那么我会在一两秒内获得所有 运行 进程。但是我没有得到嵌套属性的完整序列化。
一般来说,序列化是保真度和性能之间的权衡。
Edit 2019-03-19
我看到您添加了一条澄清评论 - 您想要到达 XML,而不是特定的 CLI XML。
Export-Clixml
(以及相关的 [System.Management.Automation.PSSerializer]::Serialize()
)输出 XML 即 有效 ,但已针对 Powershell 序列化进行了大量优化。如果您将数据发送到某些通用服务,您更有可能从 ConvertTo-Xml
中获得更多好处,这为您提供了更典型的进程间交换文档结构。
> $Xml = Get-Process a* | ConvertTo-Xml -Depth 4 # name begins with 'a'
> $Xml
xml Objects
--- -------
version="1.0" encoding="utf-8" Objects
> $Xml.Save('C:\dev\processes.xml')
> Get-Content 'C:\dev\processes.xml' -TotalCount 10
<?xml version="1.0" encoding="utf-8"?>
<Objects>
<Object Type="System.Diagnostics.Process">
<Property Name="Name" Type="System.String">AgentUI</Property>
<Property Name="SI" Type="System.Int32">1</Property>
<Property Name="Handles" Type="System.Int32">347</Property>
<Property Name="VM" Type="System.Int64">2204067930112</Property>
<Property Name="WS" Type="System.Int64">36667392</Property>
<Property Name="PM" Type="System.Int64">50425856</Property>
<Property Name="NPM" Type="System.Int64">29800</Property>