d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Powershell (hash)table Help
Add Reply New Topic New Poll
Member
Posts: 22,269
Joined: May 26 2007
Gold: 100.24
Apr 26 2013 05:10am
Hi,

I got a CSV-file, which I'm gonna import. Let the CSV-file has values like:

Country;name;number;
UK;John;4;
Sweden;Puppelpojken;6;
Canada;Supercanadian;1;

I want to read them into array (or hashtable), and then print one or two colums. So far I have a code like this:

Code
$importedlist= Import-Csv C:\skriptit\test.csv

foreach($line in $importedlist) {
write-host $line.name
}



No matter what I do, I can't get anything out. Only way to get all the values out is to write:

Code
write-host $importedlist




I assume this is some kind of noobishness-issue with hashtables :baby: Can't afford fg for solution but if I had I would ^_^
Member
Posts: 7,565
Joined: Apr 10 2007
Gold: 31.65
Apr 26 2013 05:29am
Code
$array = @()

$import = Import-Csv -Path C:\Users\test\Desktop\t.txt -Delimiter ';'

foreach ($line in $import)
{

$array += $line.Country
$array += $line.name
$array += $line.number
}

$array


dunno why you want it in an array but here it is,

stay with an object thats way more confortable,

you can access the attributes for each line in the $line variable,

just append a period with its attribute name (country, name, number),

Code
$a += "test"

is the same as;

Code
$a = $a + test


or a bit more concise;

Code
foreach ($line in (ipcsv -Path C:\Users\test\Desktop\t.txt))
{

$array += $line.Country
$array += $line.name
$array += $line.number
}

$array


This post was edited by uglygeorge08 on Apr 26 2013 05:37am
Member
Posts: 22,269
Joined: May 26 2007
Gold: 100.24
Apr 26 2013 05:59am
Somehow I don't get it to work. If I copy from your code I'll get only blank:

Code
$array = @()

$import = Import-Csv -Path C:\Users\test\Desktop\t.txt -Delimiter ';'

foreach ($line in $import)
{

$array += $line.Country
$array += $line.name
$array += $line.number
}

$array






Here is what I'm trying to do in big scale:
Read csv-file, and compare one value (that comes from somewhere else) to one column's value line by line. If it finds equivalent value from any line of that column, I can continue to edit that value.


I was just wondering why I can't do it like this. I tried something like this first, and my problem was that I can't refer with $line.columntitle (or at least it doesn't match any value):

Code
$import = Import-Csv -Path C:\Users\test\Desktop\t.txt -Delimiter ';'

foreach ($line in $import)
{

if ($line.columntitle = "wantedvalue") {
do something
}
}


This post was edited by Deppi on Apr 26 2013 06:02am
Member
Posts: 22,269
Joined: May 26 2007
Gold: 100.24
May 6 2013 07:31am
I still need halp with this one. I'll post my entire code below:

Code
$imported = Import-Csv C:\test.csv

$ErrorActionPreference = "SilentlyContinue"
import-module ActiveDirectory


:loop foreach ($user in $users) {  #users contains ADUser-powershell objects

foreach($line in $imported) {
   ForEach($item in $line) {
       write-host $item
       if ($user.properties.distinguishedname -match $item) {
              write-host "yay it works"
       }
   }
}
}



Problem is that, $item remains emtpy. So, how to handle hashtables with powershell v2? Below, is my cvs-file if you want to do any testing.

Code
idnumber;Organization;Ad-security-Group Membership;
1;management;management, staff, overlords;
2;slaves;ABC, slavegroup;
3;customers;moneygroup, richguys, investors;




If I write-host $line, I get a hashtable out like this:
@{idnumber;Organization;Ad-security-Group Membership;=1;management;management, staff, overlords}
@{idnumber;Organization;Ad-security-Group Membership;=2;slaves;ABC, slavegroup}
@{idnumber;Organization;Ad-security-Group Membership;=3;customers;moneygroup, richguys, investors}

This post was edited by Deppi on May 6 2013 07:31am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll