从随机数组中检索随机数量的项目

时间:2015-06-14 00:56:22

标签: arrays powershell random powershell-v4.0

在Powershell v4中,我需要读取包含SKU和相关产品名称(可能是逗号分隔)的文件内容,随机化这些条目,然后显示随机数量的结果SKU和名称。例如,可能存在包含12个产品名称和相关SKU的文件。在第一次运行时,它可能会导致类似这样的事情:

SKU: 123456, ProductName: Apples
SKU: 789012, ProductName: Oranges

...并且下次它运行时,可能会导致:

SKU: 524367, ProductName: Bananas
SKU: 235023, ProductName: Avocados
SKU: 123456, ProductName: Apples
SKU: 543782, ProductName: Peaches

......等等。 SKU和产品列表中的条目数量可能高达两万,但我只需要一次显示一到五十个SKU和产品。有没有办法在Powershell中实现这个目标?

我是Powershell的新手,但我已经掌握了基础知识;到目前为止,我大多只是做了很多Get-WMIObject命令或者与进程和服务进行交互。用以下的评论原谅我的啰嗦;我试图让我的目标和过程尽可能简单。

# Create the arrays of SKUs and Product names (I currently have them in two separate files,
# but can combine them easily - I separated them during my experimentation with this
# problem).
$SKU_Array = Get-Content $ScriptFolder\SKUs.txt
$Product_Array = Get-Content $ScriptFolder\Product_Names.txt

# There are 12 items in the sample files, but .Length doesn't count zero, so we subtract
# one index number from the array so that we don't end up calling on an empty item.
$NumberOfSKUs = $SKU_Array.Length - 1
$NumberOfProductNames = $Product_Array.Length - 1

# Pick a random item from the array using the whole range of index numbers (I stopped
# worrying about the SKUs, and was just concentrating on getting the product names
# portion working here).
$RandomProductName = 0..$NumberOfProductNames | Get-Random

# Display the item picked in the previous line; thus far, I haven't figured out how to
# accomplish the rest of my goal.
$Product_Array[$RandomProductName]

2 个答案:

答案 0 :(得分:1)

让我们首先将SKU和产品名称合并到一个对象中,然后得到一个介于1和项目总数之间的随机数,然后我们会多次得到一个随机索引:

$SKU_Array = Get-Content $ScriptFolder\SKUs.txt
$Product_Array = Get-Content $ScriptFolder\Product_Names.txt

# Make sure they're the same length
if ($SKU_Array.Length -ne $Product_Array.Length) {
    throw "Mismatch in the length of the SKU and Product Name files."
}
$ItemCount = $SKU_Array.Length
$MaxIndex = $ItemCount - 1

$Items = (0..$MaxIndex) | ForEach-Object {
    New-Object PSObject -Property @{
        Name = $Product_Array[$_]
        SKU = $SKU_Array[$_]
    }
}

# How many items to return this time?
$RndCount = Get-Random -Minimum 1 -Maximum $ItemCount

1..$RndCount | ForEach-Object {
    $RndIndex = 0..$MaxIndex | Get-Random
    $Items[$RndIndex]
}

请注意,虽然这会获得随机数量的项目,但它可以重复。例如,它可能返回相同的产品(例如,苹果),12次。

为了确保您只获得一次产品,我们可能会将代码的结尾更改为:

# How many items to return this time?
$RndCount = Get-Random -Minimum 1 -Maximum $ItemCount
$RndItems = @()

while ($RndItems.Count -lt $RndCount)
    $RndIndex = 0..$MaxIndex | Get-Random
    if ($RndItems -notcontains $Items[$RndIndex]) {
        $RndItems += $Items[$RndIndex]
    }
}

$RndItems

答案 1 :(得分:1)

我会将SKU和产品名称放在CSV中:

SKU,ProductName
524367,Bananas
235023,Avocados
123456,Apples
543782,Peaches
789012,Oranges

您可以从这样的CSV中获取1到50个元素的随机子集(由@MikeShepard建议):

$products = Import-Csv 'C:\path\to\products.csv'

$num = (Get-Random -Minimum 0 -Maximum 50) + 1

$products | Get-Random -Count $num

如果您还希望能够通过SKU访问产品列表,您可以将CSV读入哈希表并修改上述代码,如下所示:

$products = @{}
Import-Csv 'C:\path\to\products.csv' | % {
  $products[$_.SKU] = $_.ProductName
}

$num = (Get-Random -Minimum 0 -Maximum 50) + 1

$products.Keys | Get-Random -Count $num | % { $products[$_] }