powershell : what's wrong ?

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
François VDCIcT service DeskAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
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

1
Daryl BamforthTechnical ExpertCommented:
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.
1
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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.
0
Make Network Traffic Fast and Furious with SD-WAN

Software-defined WAN (SD-WAN) is a technology that determines the most effective way to route traffic to and from datacenter sites. Register for the webinar today to learn how your business can benefit from SD-WAN!

Daryl BamforthTechnical ExpertCommented:
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?
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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 ...
0
Daryl BamforthTechnical ExpertCommented:
Got bored .. parenthesise are your friend ..

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Daryl BamforthTechnical ExpertCommented:
Powershell to do as requested.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.