Solved

Powershell / new-object PSObject rows

Posted on 2016-09-30
2
328 Views
Last Modified: 2016-09-30
Hello!

I'm trying to figure out how PSObjects are working.
I've got a CSV file like that :


HostName;IP;DriverLang
PRINTER1;192.168.100.100;PCL6
PRINTER2;192.168.100.101;PS

I'm trying to organize the informations in a table (to add more after)

$CSV = Import-CSV "$PSScriptRoot\PRINTERS.csv" -Delimiter ";"

ForEach ($item in $CSV) {
$CSV2 = new-object PSObject
$CSV2 | Add-Member -type Noteproperty -Name HostNames -value $CSV.HostName
$CSV2 | Add-Member -type Noteproperty -Name IPs -value $CSV.IP
$CSV2 | Add-Member -type Noteproperty -Name DriverLangs -value $CSV.DriverLang
Write-Output $CSV2
}

Open in new window


But the result appears like that, when I want only 1 value per line of course.

HostNames            IPs                                DriverLangs
---------            ---                                -----------
{PRINTER1, PRINTER2} {192.168.100.100, 192.168.100.101} {PCL6, PS} 
{PRINTER1, PRINTER2} {192.168.100.100, 192.168.100.101} {PCL6, PS}

Open in new window


Where is my mistake?

Cheers,
0
Comment
Question by:Johann Buve
2 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41823178
Inside the ForEach loop, you're using the whole $CSV instead of the loop variable "$Item".
$CSV = Import-CSV "$PSScriptRoot\PRINTERS.csv" -Delimiter ";"
ForEach ($item in $CSV) {
	$CSV2 = new-object PSObject
	$CSV2 | Add-Member -type Noteproperty -Name HostNames -value $item.HostName
	$CSV2 | Add-Member -type Noteproperty -Name IPs -value $item.IP
	$CSV2 | Add-Member -type Noteproperty -Name DriverLangs -value $item.DriverLang
	Write-Output $CSV2
}

Open in new window

0
 

Author Comment

by:Johann Buve
ID: 41823192
Damn I'm dumb... Thx!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now