Solved

Powershell / new-object PSObject rows

Posted on 2016-09-30
2
235 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article will help you understand what HashTables are and how to use them in PowerShell.
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

22 Experts available now in Live!

Get 1:1 Help Now