Why am I able to do a command outside the script block, but not inside it?
My final goal is to sort a directory listing based on predefined categories. The code creates a dynamic object from the directory listing and adds two columns. The first column created is a category derived from the name, the second column that is based on the first is an index for later sorting.
Actual Error
The hash literal was incomplete.
At line:0 char:0
Stand Alone Working Code
$importance = "Hierarchy", "Item", "UPC", "Color", "Price"
$importance.IndexOf('UPC') # returns 2
Failure Code
The overall task is to do a directory listing and ultimately sort the list by $importance
. By creating dynamic columns, it will be sorted (not shown) on that new column named Index
.
$match = "UPC|Item|Color|Price|Hierarchy"
gci $incrementalTargetDir `
| Select-Object Name, @{label="Category"; Expression= {[regex]::match($_.BaseName, $match).Groups[0].Value}} `
| Select-Object Name, @{label="Index"; Expression= { $importance.IndexOf($_.Category) }
Fails on the second Select-Object
First select successfully returns this data:
Name Category
---- --------
ColorStyleSize.txt Color
Item.txt Item
FieldProductHierarchy.txt Hierarchy
UPC.txt UPC
PriceAdjust.txt Price
Also tried adding scoping script:
with the same result:
…. Expression= { $script:importance.IndexOf($_.Category)