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