Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PowerShell解析XML文件,如果找到值,则将文件复制到新位置_Xml_Powershell - Fatal编程技术网

使用PowerShell解析XML文件,如果找到值,则将文件复制到新位置

使用PowerShell解析XML文件,如果找到值,则将文件复制到新位置,xml,powershell,Xml,Powershell,我有一个应用程序,在软件中的事件生命周期内输出XML文件。我需要解析XML文件,一旦匹配了某些文本,就将文件移动到另一个文件夹 每个XML文件的文件名格式为:事件所有者(MEMS)、年份(2020)和序列号(00012345)。软件将在整个活动中多次写入同一文件,因此我需要在采取任何行动之前查找特定项目。 如果可能的话,我更愿意在PowerShell中执行此操作 我需要找到 `<EventUnits> <Unit> <UnitCode&

我有一个应用程序,在软件中的事件生命周期内输出XML文件。我需要解析XML文件,一旦匹配了某些文本,就将文件移动到另一个文件夹

每个XML文件的文件名格式为:事件所有者(MEMS)、年份(2020)和序列号(00012345)。软件将在整个活动中多次写入同一文件,因此我需要在采取任何行动之前查找特定项目。 如果可能的话,我更愿意在PowerShell中执行此操作

我需要找到

 `<EventUnits>
      <Unit>
        <UnitCode>xxxxxxx></UnitCode>
</EventUnits>`
`
xxxxxxx>
`


YYYY-MM-DD HH:MM:SS

一旦UnitCode等于4个单位代码中的一个,并且EventCompleted具有有效的日期和时间,文件将被移动到另一个文件夹。

您可以读取文件内容,告诉powershell您希望文件在摄取时为xml。然后您就可以访问其中的对象。例如:

# read in the file contents of xml file
[xml]$fileContents = Get-Content 'C:\my\file\path.xml'

# you should now be able to access the contents of the file using dot notation
Write-Host $fileContents.EventUnits.Unit.UnitCode
Write-Host $fileContents.EventDetails.EventCompletedTime
本例假设

  • EventUnits和EventDetails是顶级节点(不嵌套) 在另一个xml节点中)
  • 文件绝对会在那里。如果 您不确定是否可以测试该文件的存在
  • 该文件绝对是xml格式的

  • 这回答了你的问题吗?在powershell中,有很多帖子可以引导您解析xml。
    # read in the file contents of xml file
    [xml]$fileContents = Get-Content 'C:\my\file\path.xml'
    
    # you should now be able to access the contents of the file using dot notation
    Write-Host $fileContents.EventUnits.Unit.UnitCode
    Write-Host $fileContents.EventDetails.EventCompletedTime