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

Retrieve a random number of items from a randomized array

在 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]

让我们先将 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]
}

请注意,虽然这将获得随机数量的项目,但它可以重复。例如,它可能 return 相同的产品(比如苹果)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

我会将 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[$_] }