0

Let's say I have this CSV file

customer 60; SomeLocation; 60
customer 120; SomeLocation; 120
customer 140; SomeLocation; 140
customer 200; SomeLocation; 200
customer 300; SomeLocation; 300

I'm importing using Import-CSV. I'm able to walk through every line and filter each property using pre-set headers. I want to set a target, let's say 140. I want to use $random to pick some line, let's say

customer 200; SomeLocation; 200 Now, as 200 is bigger than 140, I want to remove it from memory (not from the CSV), but looks like I'm having an issue understanding how can I manipulate Arrays, since I'm getting Collection was of a fixed size. all the way.

I know how to do the random stuff, but I'm struggling finding out a way to import the CSV and use it freely.

3
  • Your .csv file does not seem right. The first line have to be headers (separated with a delimiter), the 2nd to nth line have to be values (separated with a delimiter). Using import-csv is correct, try and also define -delimiter parameter. Also post your code with which you are trying to manipulate the array. Commented Dec 24, 2019 at 10:46
  • 1
    Both Import-CSV and ConvertFrom-CSV support the -Header parameter to define headerless csvs so the lack of header in the csv itself is not really a problem here. Commented Dec 24, 2019 at 11:39
  • Please add more info, your question isn't clear. From what I can see though, you may want to look into piping into a "Where-object" to re-import your CSV based on conditions. Commented Dec 26, 2019 at 17:54

1 Answer 1

1

See the example below.

Using Get-Content and ConvertFrom-CSV with its -header parameter to define the csv headers since they are not present in your CSV, you can get an object out of the data.

Then, instead of trying to remove stuff to the original array, you instead build an output "array" using an object which you can modify freely, such as the .net list object.

From there, you can do what you want.

$CsvData = @'
customer 60; SomeLocation; 60
customer 120; SomeLocation; 120
customer 140; SomeLocation; 140
customer 200; SomeLocation; 200
customer 300; SomeLocation; 300
'@

# In your case, rather than the direct csv data, you'd use
#$CsvData = Get-Content -Path 'PATH_TO_THE_CSV' -Raw

$Customers = ConvertFrom-Csv $CsvData -Delimiter ';' -Header 'CustomerName','Location','SomeInt'

# Not sure why you'd use it if you set your target to 140
$Random = Get-Random -InputObject $Customers

# We build a new List instead of trying to remove from the original object.
$CustomersOutput = [System.Collections.Generic.List[psobject]]::new()
#Arbitrary filtering - Notice I do convert SomeInt to an int before doing the comparison. 
# Otherwise, the comparison would be done between a string and an int, which would produce weird results. 
$Customers.Where( { [int]$_.SomeInt -le 140 }) | % { $CustomersOutput.Add($_) }

$CustomersOutput

Result from $CustomerOutput Results

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.