locked
Error: Unexcepted token 'in" RRS feed

  • Question

  • 
    
    $import = Import-CSv -Path "c:\csv\Add.csv"
    foreach ($item in $import) {
    Add-DistributionGroupMember -Identity $item.GroupName -Member $item.UPN - Verbose
    }

    Script turns me back error:

    unexpected token 'in' in expression or statement 

    Can you help me?

    Friday, January 12, 2018 10:07 AM

Answers

  • There is a difference between Foreach-Object and a Foreach - Statement. In your case you would need to use Foreach-Object.

    The code you run is obviously not the code you showed above.


    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Friday, January 12, 2018 12:29 PM
    • Proposed as answer by jrv Friday, January 12, 2018 12:36 PM
    • Marked as answer by Richard MuellerMVP Saturday, February 17, 2018 4:10 PM
    Friday, January 12, 2018 12:27 PM

All replies

  • The complete error message might be helpful.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Friday, January 12, 2018 10:49 AM
  • Friday, January 12, 2018 11:15 AM
  • There is a difference between Foreach-Object and a Foreach - Statement. In your case you would need to use Foreach-Object.

    The code you run is obviously not the code you showed above.


    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Friday, January 12, 2018 12:29 PM
    • Proposed as answer by jrv Friday, January 12, 2018 12:36 PM
    • Marked as answer by Richard MuellerMVP Saturday, February 17, 2018 4:10 PM
    Friday, January 12, 2018 12:27 PM
  • The code you first posted is not the code used in this image.  You cannot pipe into a "foreach()".  As noted above, only "ForEach-Object" can be piped into.  Either use that or use you original code.


    \_(ツ)_/

    Friday, January 12, 2018 12:34 PM
  • The following will NOT cause an error.

    $csv = Import-CSv c:\csv\Add.csv
    foreach ($item in $csv) {
        Add-DistributionGroupMember -Identity $item.GroupName -Member $item.UPN - Verbose
    }


    \_(ツ)_/

    Friday, January 12, 2018 12:36 PM
  • Well -- foreach is also a default alias for the ForEach-Object cmdlet; thus these two are functionally equivalent:

    $something | ForEach-Object { ...

    and

    $something | foreach { ...

    The foreach language statement is not the same as the ForEach-Object cmdlet. The foreach language statement's syntax is foreach ( x in y ) { }.

    Unfortunately, foreach is also a default alias for ForEach-Object. This was a bad design decision IMO and leads to a lot of confusion.

    To clarify (in general): On the right-hand side of a pipe, you are using the ForEach-Object cmelet. If as the first language statement on a line, you are using the foreach language statement.


    -- Bill Stewart [Bill_Stewart]


    Friday, January 12, 2018 4:27 PM
  • Yes.  I suspect it is one of Jeffrey's little jokes to show off his love of syntactical candy.


    \_(ツ)_/

    Friday, January 12, 2018 4:36 PM
  • Maybe so - it's one of those things that might have seemed like a good idea at the time, but they didn't suspect the confusion it would probably cause. Now that it's there, we have to leave it alone or lots of stuff will probably break. (IMO it is bad practice to use any aliases in scripts anyway.)

    -- Bill Stewart [Bill_Stewart]

    Friday, January 12, 2018 5:27 PM
  • I like to think Jeffrey was having a little joke and hoping it would push people into the pipeline.  I have a picture in my head of Snover sitting on a pipeline, waving his cowboy hat and yelling "How I learned to stop worrying and love the pipeline!"


    \_(ツ)_/

    Friday, January 12, 2018 5:35 PM