Search code examples
xmlpowershellxpathnavigatorselect-xml

Find XML attributes at random


How can I find the 2 attributes $_.CompleteName and $_.Channel at random?

+ $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

Sample Data

$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>
"@

Code

$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

Solution

  • Still not clear what you are looking for but this will take that XML and select, at random, one of the Channel and CompleteName pairings as a custom object

    $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
    

    Sample Output

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

    Pure String based output

    $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 
    

    Output

    folder-2    channel1