simpen
simpen

Reputation: 134

Saving CSV columns to array?

I want to save three columns I have in a CSV file to three different arrays, one for usernames, one for ip-addresses and one for passwords. But it doesn't seem to work (atleast from what I understand).

Here's the code:

$Importedcsv = Import-csv "C:\my\file\is\located\here.txt" -Delimiter ";"

$Importedcsv.password += $PasswordArray
$Importedcsv.address += $AdressArray
$Importedcsv.username += $UsernameArray

This is the error I get:

The property 'username' cannot be found on this object. Verify that the property exists and can be set.
At C:\blyat\cyka\ruski.ps1:7 char:1
+ $Importedcsv.username += $UsernameArray
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

I also get the error with the other columns.

If you want more specifics please ask nicely :D

Keep in mind that I have only worked with PowerShell for a few days so please try to explain everything as detailed as possible, thanks!

CSV file:

address;username;password
192.168.0.10;advokathuset\user;#adfgad
192.168.0.10;atek\user;#afdgadfg
192.168.0.10;digo\user;#adfgadf
192.168.0.10;daldens\user;#adfgsdfh
192.168.0.10;elservicetibro\user;adfgdasf
192.168.0.10;finmek\user;#adfhsfgj
192.168.0.10;formidabel\user;#zadfsgsdfh
192.168.0.10;gronsakshuset\user;#sdfgsfgh1
192.168.0.10;jarisafe\user;#adfgfjg
192.168.0.10;jattadalen\user;#adfgadfgh
192.168.0.10;ks\user;#sadfgsfdgh
192.168.0.10;karstorp\user;#sfdgsgfdh
192.168.0.10;leklandet\user;#gadsfhsd
192.168.0.10;nordiskindustri\useer;#sdfgm
192.168.0.10;odz\user;#gsdfjsdrg
192.168.0.10;oneup\user;#sfdtghsfdg
192.168.0.10;proline\user;#sdfjdgfh
192.168.0.10;saj\user;#dsfgsj
192.168.0.10;swedcon\user;#sadfasd
192.168.0.10;sek\user;#a gffghj
192.168.0.10;sir\user;sfhgd ghj
192.168.0.10;skinnwille\user;#sdfgs35 4dfgh
192.168.0.10;SMEA\user;dsfg456egt
192.168.0.10;tibromobel\user;fghjfgh1
192.168.0.10;tibrokok\user;sdfgsdfg
192.168.0.10;tottegott\user;sdfgsfgj
192.168.0.10;unojohanssons\user;sdfgsdfg
192.168.0.10;variantkok\user;#dfgsdfgs1
192.168.0.10;wexman\user;sdfg
192.168.0.10;wikstrands\user;khfjg
192.168.0.10;willbo\user;asdffdsafe
192.168.0.10;skovdeft\user;#5asdfasdf
192.168.0.10;kungsaterkok\user;asdfasdf
192.168.0.10;soderstroms\user; asdfasdf
192.168.0.10;elogistik\user;asdfasdf

Upvotes: 2

Views: 2071

Answers (3)

user6645658
user6645658

Reputation:

You may find the use of an array here to be redundant.

For example:

$Importedcsv.password

...is already an array. You can access the info by providing an index:

$Importedcsv.password[0] 

This would give you the first password.

Upvotes: 2

Glen Buktenica
Glen Buktenica

Reputation: 342

Two errors I can see. You are trying to add to an array the doesn't exist yet and you are trying to put the data in the wrong direction.

$Importedcsv = Import-csv "C:\my\file\is\located\here.txt" -Delimiter ";"

$PasswordArray = @($Importedcsv.password)
$AdressArray   = @($Importedcsv.Address)
$UsernameArray = @($Importedcsv.username)

Upvotes: 3

Martin Brandl
Martin Brandl

Reputation: 59021

Your Import-Csv invoke is correct but you have to reverse your assignment:

$PasswordArray = $Importedcsv.password
$AdressArray = $Importedcsv.address
$UsernameArray = $Importedcsv.username

If the arrays are already defined and containing data, you need to change the assignment to +=.

Upvotes: 2

Related Questions