1

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
Matt
  • 45,022
  • 8
  • 78
  • 119
Arnold
  • 49
  • 8
  • When you say at random do you mean choose one of the `Channel` and `CompleteName` at random? A random one from each of those two sets? I'm not completely sure what output you are hoping for – Matt Mar 28 '15 at 15:25
  • Both your `Where-Object` clauses don't make much sense. Your first major issue is that you need to be using `$Xliste` in your code and not `$liste`. – Matt Mar 28 '15 at 15:41

1 Answers1

0

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
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Hi Matt, Yess! Please can you omit the haeder (not Tb version) This is transportable for me: folder-1 channel2 Merci, Greats Arnold – Arnold Mar 28 '15 at 20:13