powershell - Retrieve a random number of items from a randomized array -


in powershell v4, need read in contents of file contains skus , associated product names (likely comma-delimited), randomize entries, display random number of resulting skus , names. instance, there may file 12 product names , associated skus in it. on first run, might result in this:

sku: 123456, productname: apples sku: 789012, productname: oranges 

...and next time it's run, might result in this:

sku: 524367, productname: bananas sku: 235023, productname: avocados sku: 123456, productname: apples sku: 543782, productname: peaches 

...and on. number of entries in list of skus , products large twenty thousand, i'd need display between 1 , fifty skus , products @ time. there way accomplish within powershell?

i'm new powershell, have basics down; far, have done lot of get-wmiobject commands or interacting processes , services. forgive wordiness remarks below; i'm trying make goal , process plain possible.

# create arrays of skus , product names (i have them in 2 separate files, # can combine them - separated them during experimentation # problem). $sku_array = get-content $scriptfolder\skus.txt $product_array = get-content $scriptfolder\product_names.txt  # there 12 items in sample files, .length doesn't count zero, subtract # 1 index number array don't end calling on empty item. $numberofskus = $sku_array.length - 1 $numberofproductnames = $product_array.length - 1  # pick random item array using whole range of index numbers (i stopped # worrying skus, , concentrating on getting product names # portion working here). $randomproductname = 0..$numberofproductnames | get-random  # display item picked in previous line; far, haven't figured out how # accomplish rest of goal. $product_array[$randomproductname] 

i put skus , product names in csv:

sku,productname 524367,bananas 235023,avocados 123456,apples 543782,peaches 789012,oranges 

you can random subset of 1 50 elements csv (as suggested @mikeshepard):

$products = import-csv 'c:\path\to\products.csv'  $num = (get-random -minimum 0 -maximum 50) + 1  $products | get-random -count $num 

if want able access product list sku read csv hashtable , modify above code this:

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

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -