2

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
6
  • Hi, $array += $newItem. Commented Jul 27, 2016 at 7:51
  • You should share your csv with us. Also, you probably want to reverse your assignments.... Commented Jul 27, 2016 at 7:54
  • @MartinBrandl Trust me, i would share the csv to you all but it contains decrypted passwords to all our servers :/ Commented Jul 27, 2016 at 8:01
  • Just share the first two records with us, change the password and all other sensitive information.... Commented Jul 27, 2016 at 8:06
  • @MartinBrandl csv is fine. He is simply trying add new row to the table imported. this way it wont work. But the correct question is about the direction of the assignments. OP, you want to add new records to the imported csv? Commented Jul 27, 2016 at 8:13

3 Answers 3

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, why the need for @() please?
Wrapping the variable in @() defines it as an array
2

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 +=.

Comments

2

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.

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.