首页 > 解决方案 > PowerShell 获取所有相关值

问题描述

我有一个xml,我提取了一些需要的信息。

这是我从节点获取每个值的代码:

 $CustomTypeColl = $XmlDocument.XIP.ContentObject.CustomType | Group-Object -NoElement
 $ContentObjectColl = $XmlDocument.XIP.ContentObject.SecurityTag | Group-Object -NoElement
 $BitstreamColl = [System.Collections.ArrayList]@($XmlDocument.XIP.Generation.Bitstreams.Bitstream) 
 $DurationColl = ($xmlDocument.XIP.Generation.Properties.Property |  where {$_.PropertyName -eq "Duration"}).value 

在这个节点 - $CustomTypeColl 变量我有这个值:音频、视频、文档

在这个节点 - $BitstreamColl mp3,mp3,mp3,mov,mov,pdf,pdf

我该怎么做:Foreach Audio 获取 mp3 格式,Video 获取 mov 格式,Document 获取 pdf

需要输出:'您有 3 个 mp3 格式的音频';2个mov格式的视频和2个pdf格式的文档。

我正在尝试做某事,但没有成功!

$XmlDocument.XIP.ContentObject.CustomType | % {$XmlDocument.XIP.Generation.Bitstreams.Bitstream} 

谢谢你的帮助!

这是我的 xml 文件:

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <XIP xmlns="http://preservica.com/XIP/v6.0">
  <InformationObject>
    <Ref>de76f81d-18bd-4a0c-84d7-0a43e5385ec7</Ref>
    <Title>AUT_cik02-RK-2018-10-08</Title>
    <SecurityTag>U_UserAccess</SecurityTag>
    <Parent>9615af72-e69b-45d1-b0f2-b04923765c21</Parent>
</InformationObject>
<Representation>
    <InformationObject>de76f81d-18bd-4a0c-84d7-0a43e5385ec7</InformationObject>
    <Name>Preservation-1</Name>
    <Type>Preservation</Type>
    <ContentObjects>
        <ContentObject>1cdcc348-ab06-4938-99a9-7642edc2370c</ContentObject>
    </ContentObjects>
</Representation>
<ContentObject>
    <Ref>1cdcc348-ab06-4938-99a9-7642edc2370c</Ref>
    <Title>AUT_cik02-RK-2018-10-08</Title>
    <SecurityTag>U_UserAccess</SecurityTag>
    <CustomType>video</CustomType>
    <Parent>de76f81d-18bd-4a0c-84d7-0a43e5385ec7</Parent>
</ContentObject>
<Generation original="true" active="true">
    <ContentObject>1cdcc348-ab06-4938-99a9-7642edc2370c</ContentObject>
    <FormatGroup>mpeg-video</FormatGroup>
    <EffectiveDate>2020-05-13T10:38:47Z</EffectiveDate>
    <Bitstreams>
        <Bitstream>Representation_Preservation_1/AUT_cik02-RK-2018-10-08/Generation/AUT_cik02-RK-2018-10-08.mp4</Bitstream>
    </Bitstreams>
    <Formats>
        <Format valid="false">
            <PUID>fmt/199</PUID>
            <Priority>1</Priority>
            <IdentificationMethod>Signature</IdentificationMethod>
            <FormatName>MPEG-4 Media File</FormatName>
            <FormatVersion>       </FormatVersion>
        </Format>
    </Formats>
    <Properties>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>prp/10011</PUID>
            <PropertyName>Vertical Aspect Ratio</PropertyName>
            <Value>9</Value>
        </Property>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>prp/10005</PUID>
            <PropertyName>Video Bitrate</PropertyName>
            <Value>1910425</Value>
        </Property>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>prp/10006</PUID>
            <PropertyName>Encoding Type</PropertyName>
            <Value>Main@L4</Value>
        </Property>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>TSS-prp/10</PUID>
            <PropertyName>Codec Name</PropertyName>
            <Value>AVC</Value>
        </Property>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>prp/19</PUID>
            <PropertyName>Creating Application</PropertyName>
            <Value>x264 core 155 r2901 7d0ff22</Value>
        </Property>
        <Property>
            <Group>Audio Stream 0</Group>
            <PUID>prp/10003</PUID>
            <PropertyName>Number of Audio Channels</PropertyName>
            <Value>2</Value>
        </Property>
        <Property>
            <PUID>prp/10008</PUID>
            <PropertyName>Number Of Video Streams</PropertyName>
            <Value>1</Value>
        </Property>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>prp/10009</PUID>
            <PropertyName>Frame Rate</PropertyName>
            <Value>25</Value>
        </Property>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>prp/10011</PUID>
            <PropertyName>Horizontal Aspect Ratio</PropertyName>
            <Value>16</Value>
        </Property>
        <Property>
            <Group>Audio Stream 0</Group>
            <PUID>prp/10005</PUID>
            <PropertyName>Audio Bitrate</PropertyName>
            <Value>160362</Value>
        </Property>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>prp/3</PUID>
            <PropertyName>Video Width</PropertyName>
            <Value>1920</Value>
        </Property>
        <Property>
            <Group>Audio Stream 0</Group>
            <PUID>TSS-prp/10</PUID>
            <PropertyName>Codec Name</PropertyName>
            <Value>AAC</Value>
        </Property>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>prp/4</PUID>
            <PropertyName>Video Height</PropertyName>
            <Value>1080</Value>
        </Property>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>TSS-prp/40</PUID>
            <PropertyName>Scan Type</PropertyName>
            <Value>progressive</Value>
        </Property>
        <Property>
            <PUID>prp/19</PUID>
            <PropertyName>Creating Application</PropertyName>
            <Value>HandBrake 1.1.1 2018061800</Value>
        </Property>
        <Property>
            <PUID>prp/10008</PUID>
            <PropertyName>Number Of Audio Streams</PropertyName>
            <Value>1</Value>
        </Property>
        <Property>
            <Group>Audio Stream 0</Group>
            <PUID>prp/12</PUID>
            <PropertyName>Sampling Frequency</PropertyName>
            <Value>48000</Value>
        </Property>
        <Property>
            <PUID>prp/10002</PUID>
            <PropertyName>Duration</PropertyName>
            <Value>2M9.686S</Value>
        </Property>
        <Property>
            <Group>Video Stream 0</Group>
            <PUID>prp/5</PUID>
            <PropertyName>Colour Space</PropertyName>
            <Value>YUV</Value>
        </Property>
    </Properties>
</Generation>
<Bitstream>
    <Filename>AUT_cik02-RK-2018-10-08.mp4</Filename>
    <FileSize>33653548</FileSize>
    <PhysicalLocation>Representation_Preservation_1/AUT_cik02- RK-2018-10-08/Generation</PhysicalLocation>
    <Fixities>
        <Fixity>
            <FixityAlgorithmRef>SHA1</FixityAlgorithmRef>
                       <FixityValue>8872b97b619e967fceba3b36e8ee02aca940dfba</FixityValue>
        </Fixity>
    </Fixities>
</Bitstream>
</XIP>

标签: xmlpowershell

解决方案


在您的示例中,它们只是一个 xml,我假设您的所有 xml 都位于此“根”文件夹的一个文件夹或子文件夹中,因此您可以通过以下方式获取所有这些:

Get-ChildItem -Path PATHTOYOURROOTFOLDER -Filter "*.xml" -Recurse 

(如果您不想搜索子文件夹,请忽略递归)并且不要忘记更改 PATHTOYOURROOTFOLDER

$allMediaFiles = Get-ChildItem -Path PATHTOYOURROOTFOLDER -Filter "*.xml" -Recurse | % {
    $content = get-content -Path $_ -Raw
    $xml = [xml]$content

    #this will generate a new object with only the properties you are intrested in. The advantage of this is that you dont hav nested properies wich are easier to handle and also easier to visualize
    [pscustomobject]@{
        title = $xml.XIP.ContentObject.Title
        video_audio = $xml.XIP.ContentObject.CustomType # video of audio
        SecurityTag = $xml.XIP.ContentObject.SecurityTag 
        length = ($xml.XIP.Generation.Properties.Property | where {$_.PropertyName -eq "Duration"}).value
        formatname = $xml.XIP.Generation.Formats.Format.FormatName
        filename = $xml.XIP.Bitstream.Filename
        extention = $xml.XIP.Bitstream.Filename -replace ".*\.", ""
    }
}

#run this to visualize the result of above
$allMediaFiles | ft
# i just copied your xml 3 times and changed one of them to mov format instead of mp4 for demo purpose
# the above line should output this:

# title                   video_audio SecurityTag  length   formatname        filename                    extention
# -----                   ----------- -----------  ------   ----------        --------                    ---------
# AUT_cik02-RK-2018-10-08 video       U_UserAccess 2M9.686S MPEG-4 Media File AUT_cik02-RK-2018-10-08.mp4 mp4      
# AUT_cik02-RK-2018-10-08 video       U_UserAccess 2M9.686S MOV Media File    AUT_cik02-RK-2018-10-08.mov mov      
# AUT_cik02-RK-2018-10-08 video       U_UserAccess 2M9.686S MPEG-4 Media File AUT_cik02-RK-2018-10-08.mp4 mp4      


$allMediaFiles | ? {$_.video_audio -eq "video"} | Group-Object -Property extention | select name, count
# this should output the results for the videos:

# Name Count
# ---- -----
# mp4      2
# mov      1


$allMediaFiles | ? {$_.video_audio -eq "audio"} | Group-Object -Property extention | select name, count
# this should output a similar result for audio

总秒数:

$allMediaFiles = $allMediaFiles | select *, totalseconds | % {
    # calculate the total seconds, u will need to use following code:
    $_.length.Split("(H|M|S)")
    [float]"2.45"

    # set the new totalseconds field
    $_.totalseconds = (New-TimeSpan -Minutes 5 -Seconds 5.254).TotalSeconds

    #return $_ because this $_ will have the totalseconds filled
    $_
    }

    $allMediaFiles | Measure-Object -Property totalseconds -sum

推荐阅读