如何使用Powershell将过程写入XML文件?

时间:2019-03-16 19:21:34

标签: xml powershell csv ps system-administration

将进程导出到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,我要问的是如何直接从XML内部使用此powershell工具,而无需执行其他步骤)

如果这是正确的术语,我想我想要一个非流式传输选项。

另请参阅:

1 个答案:

答案 0 :(得分:2)

  

编辑-首先,我回答了最初提出的问题,内容是“为什么我对refreshControl有疑问,但是请进一步查看我的修改以获取涉及{{1 }}。

有趣的问题!我相信答案与流媒体无关,而实际上与性能和序列化有关。

背景

  • CSV是平面的-不会序列化结构化数据。因此,尽管保真度较低,它可能会很快完成。

  • XML是结构化的-XML导出将序列化嵌套的属性,如果这些属性本身具有嵌套的属性,那么您可能最终会从系统中提取很多信息。

串行器的智能程度是有限的,它很可能会通过对管道中的每个对象进行多次顺序调用来实现此目的。 (在应用程序开发中,一个常见的问题是,对一个对象进行序列化可以仅针对单个对象从数据层提取一个大图。请记住,Export-Clixml是一种通用的序列化程序;可以想象只为一个序列化程序而写一个。序列化来自ConvertTo-Xml的输出,可以对其进行优化以最大程度地减少进程外调用。)

我在Windows上的Export-Clixml中进行了测试。如果我用管道传输Get-Process,则序列化到文件的对象数是按Ctrl-C之前等待时间的函数。在我笨拙的公司笔记本电脑上,默认深度为每个过程大约一秒钟。

在Windows上的Powershell中,一个pwsh对象(您从Get-Process | Export-Clixml processes.xml回来的东西)是一个本地对象的包装,该本地对象是通过COM公开的。它是.NET中对某个对象的句柄。在.NET外部。为此,请阅读进程外调用-这比同进程调用要慢得多。如果您使用纯.NET对象进行管道传输,则可以轻松地取消引用获取嵌套属性,一切将很快完成。

对于每个进程,将进入COM来获取已加载的DLL,然后进入COM来获取线程...在Windows上,可能在* nix上也是昂贵的调用。

答案:

ProcessInfo上设置深度。

如果将深度指定为1:Get-Process,那么一秒钟或两秒钟内就会得到所有正在运行的进程。但是我没有获得嵌套属性的完整序列化。

通常,序列化是保真度和性能之间的折衷。

  

编辑2019-03-19

我看到您添加了一个澄清的注释-您想要使用XML,而不是CLI XML。

Export-Clixml(和相关的Get-Process | Export-Clixml -Depth 1 processes.xml)输出XML 有效,但针对Powershell序列化进行了优化。如果您要将数据发送到某些常规服务,则更有可能从Export-Clixml获得里程,这为您提供了一种更典型的文档结构,用于进程间交换。

[System.Management.Automation.PSSerializer]::Serialize()