【问题标题】:Find XML attributes at random随机查找 XML 属性
【发布时间】:2015-06-01 17:44:39
【问题描述】:

如何随机找到$_.CompleteName$_.Channel这两个属性?

+ $channel | ? {$liste} | get-random <<<<  -min 0 -max $liste.list.groupe.count
    + CategoryInfo          : InvalidArgument: (position:PSObject) [Get-Random 
   ], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Command 
   s.GetRandomCommand

样本数据

$Xliste = [xml]@"
<list>
   <groupe>
        <position type="General">
            <CompleteName>folder-1</CompleteName>
            <dateYY>2014</dateYY>
            <dateMM>jaenner</dateMM>
            <dateDD>mittwoch</dateDD>
            <Overall_mode>cbr</Overall_mode>
            <Duration>00:1:27</Duration>
            <Overall_rate>96.0Kbps</Overall_rate>
        </position>
        <position type="Version">
            <Channel>channel2</Channel>
            <CodecID>55</CodecID>
            <Duration>00:1:27</Duration>
            <Compression>Lossy</Compression>
            <StreamSize>96.0Kbps</StreamSize>
        </position>
    </groupe>
    <groupe>
        <position type="General">
            <CompleteName>folder-2</CompleteName>
            <dateYY>2013</dateYY>
            <dateMM>maerz</dateMM>
            <dateDD>montag</dateDD>
            <Overall_mode>cbr</Overall_mode>
            <Duration>00:8:12</Duration>
            <Overall_rate>96.0Kbps</Overall_rate>
        </position>
        <position type="Version">
            <Channel>channel1</Channel>
            <CodecID>49</CodecID>
            <Duration>00:8:12</Duration>
            <Compression>Lossy</Compression>
            <StreamSize>96.0Kbps</StreamSize>
        </position>
    </groupe>
</list>
"@

代码

$channel_and_CompleteName = Select-Xml $liste -xpath "*/*/*" | Select-Object -Expand node | ? {$_ -ne ($_.CompleteName)+" "+($_.Channel)}
$channel_and_CompleteName | ? {$liste} | get-random -min 0 -max $liste.list.groupe.count

【问题讨论】:

  • 当您说随机时,您的意思是随机选择ChannelCompleteName 之一吗?从这两组中的每一个中随机一个?我不完全确定您希望得到什么输出
  • 您的两个Where-Object 子句都没有多大意义。您的第一个主要问题是您需要在代码中使用$Xliste 而不是$liste

标签: xml powershell xpathnavigator select-xml


【解决方案1】:

仍然不清楚您在寻找什么,但这将采用 XML 并随机选择 ChannelCompleteName 配对之一作为自定义对象

$Xliste.List.groupe | ForEach-Object{
    $props = @{}
    $_.Position | ForEach-Object{    
        If($_.Type -eq "General"){
            $props.CompleteName =  $_.CompleteName
        } ElseIf($_.Type -eq "Version"){
            $props.Channel =  $_.Channel
        }
    }
    New-Object -TypeName PSCustomObject -Property $props 
} | Get-Random

样本输出

CompleteName Channel 
------------ ------- 
folder-1     channel2

基于纯字符串的输出

$Xliste.List.groupe | ForEach-Object{
    $string = @()
    $_.Position | ForEach-Object{    
        If($_.Type -eq "General"){
            $string +=  $_.CompleteName
        } ElseIf($_.Type -eq "Version"){
            $string +=  $_.Channel
        }
    }
    $string -join "`t"
} | Get-Random 

输出

folder-2    channel1

【讨论】:

  • 嗨,马特,是的!请你省略 haeder(不是 Tb 版本)这对我来说是可移动的:文件夹 1 通道 2 谢谢,伟大的阿诺德
猜你喜欢
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 2012-05-06
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
相关资源
最近更新 更多