• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 61
  • Last Modified:

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
0
François VDC
Asked:
François VDC
  • 4
  • 2
2 Solutions
 
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
 
QlemoBatchelor, 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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
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
 
QlemoBatchelor, 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
 
Daryl BamforthTechnical ExpertCommented:
Powershell to do as requested.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now