Solved

Powershell / new-object PSObject rows

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

760 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

21 Experts available now in Live!

Get 1:1 Help Now