powershell : what's wrong ?

Daoust ICT
Daoust ICT used Ask the Experts™
on
function ManageAdUsers($CsvFile)
{
$MyVariable = 99
$SAM = ""
$bAlreadyProcessed = $false

$Items = Import-Csv -path "$CsvFile" –delimiter ";" | foreach ($Item in $Items) {Write-Host $Item}

--> give this error
PS M:\ICT Manager\Scripts\PS1> .\importad.ps1
Unexpected token 'in' in expression or statement.
At M:\ICT Manager\Scripts\PS1\ImportAD.ps1:18 char:72
+ $Items = Import-Csv -path "$CsvFile" -delimiter ";" | foreach ($Item in <<<<
 $Items) {Write-Host $Item}
    + CategoryInfo          : ParserError: (in:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
Commented:
There's a difference between the foreach statement and the ForEach-Object cmdlet.  Even though "foreach" is also an alias for the cmdlet, you have to follow the syntax rules for whichever you're using.  The foreach statement doesn't take pipeline input.
$Items = Import-Csv -path "$CsvFile" –delimiter ";"
foreach ($Item in $Items) {Write-Host $Item}

Open in new window

Daryl BamforthTechnical Expert

Commented:
Because you're already in the pipeline, when you pass through a list you don't need to specify a holding variable to work through it as this is already implied and handled by the $_ parameter.

So you can just use
$Items = Import-Csv -path "$CsvFile" –delimiter ";" | foreach {Write-Host $_}

Open in new window


$_ refers to the current object from the pipeline.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Daryl, this is not correct. Your code is assigning the final result of the pipe, which is nothing because nothing has been put into the pipeline again. However, just removing $Items = is sufficient.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Daryl BamforthTechnical Expert

Commented:
Qlemo, it is correct in terms of the pipeline always takes precedence. Hence it will still write-out to the screen all the contents of the imported CSV file.

However, you do bring up a valuable point. Nothing will actually be assigned to the $items variable (when I tested it I was only checking info wrote to the screen, and forgot about the variable assignment). Also you will need to specify your field header otherwise it will take the first item as the header (assuming there isn't already a header). So

$items = Import-Csv -path "$CsvFile" –delimiter ";" -header "name"
$items | foreach {write-host $_.name}

Open in new window

If there is already a header just remove the -header "name" and change the $_.name to $_.header

I am assuming he wants to assign the contents to a variable as well as writing out to the screen?
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
If that last assumption is correct, footech's code is the way to go. We cannot tell, because the original code posted is only sample code without much real value ...
Technical Expert
Commented:
Got bored .. parenthesise are your friend ..

($items = Import-Csv -path "$CsvFile" –delimiter ";" -header "name")  | foreach {write-host $_.name}

Open in new window

Daryl BamforthTechnical Expert

Commented:
Powershell to do as requested.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial