Script Center > Scripting Forums > The Official Scripting Guys Forum! > Need help getting Unexpected token '}' an expresstion or statment

Answered Need help getting Unexpected token '}' an expresstion or statment

  • Sunday, February 05, 2012 5:31 PM
     
     

    I am working in the MOAC lab and have hit a Snag 

    getting Unexpected token '}' an expresstion or statment  in statment

    Import-CSV `c:\Project58members.csv'| foreach-object -process {Set mailbox -identity $_.Identity -managedfoldermailboxpolicy `Project58 Managed folder policy'} <<<<

    any suggestions

     

Answers

  • Sunday, February 05, 2012 7:01 PM
     
     Answered Has Code

    Try this instead:

     

    Import-CSV c:\Project58members.csv| foreach-object -process {Set mailbox -identity $_.Identity -managedfoldermailboxpolicy 'Project58 Managed folder policy'}
    

    It looks like you were using apostrophes instead of single quotes.

     


    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool 2.0
    AD User Creation tool


    In both quoted strings the OP used a back-tick for the opening quote and a single-quote for the closing quote:

        Import-CSV `c:\Project58members.csv'| foreach-object -process {Set mailbox -identity $_.Identity -managedfoldermailboxpolicy `Project58 Managed folder policy'}

    he only quoted string in the above is therefore this:

       '| foreach-object -process {Set mailbox -identity $_.Identity -managedfoldermailboxpolicy `Project58 Managed folder policy'

    reducing the statement to this:

        Import-CSV `c:\Project58members.csv'whatever'}

    In which it is easier to see that the "}" was unexpected.

     

All Replies

  • Sunday, February 05, 2012 5:37 PM
    Moderator
     
     Proposed Answer Has Code

    Try this instead:

    Import-CSV c:\Project58members.csv| foreach-object -process {Set mailbox -identity $_.Identity -managedfoldermailboxpolicy 'Project58 Managed folder policy'}
    

    It looks like you were using apostrophes instead of single quotes.


    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool 2.0
    AD User Creation tool
  • Sunday, February 05, 2012 7:01 PM
     
     Answered Has Code

    Try this instead:

     

    Import-CSV c:\Project58members.csv| foreach-object -process {Set mailbox -identity $_.Identity -managedfoldermailboxpolicy 'Project58 Managed folder policy'}
    

    It looks like you were using apostrophes instead of single quotes.

     


    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool 2.0
    AD User Creation tool


    In both quoted strings the OP used a back-tick for the opening quote and a single-quote for the closing quote:

        Import-CSV `c:\Project58members.csv'| foreach-object -process {Set mailbox -identity $_.Identity -managedfoldermailboxpolicy `Project58 Managed folder policy'}

    he only quoted string in the above is therefore this:

       '| foreach-object -process {Set mailbox -identity $_.Identity -managedfoldermailboxpolicy `Project58 Managed folder policy'

    reducing the statement to this:

        Import-CSV `c:\Project58members.csv'whatever'}

    In which it is easier to see that the "}" was unexpected.

     

  • Sunday, February 05, 2012 8:18 PM
     
      Has Code

    I think this might make it a bit easier to see.

    $csvfile="c:\Project58members.csv"
    $mbpolicy="Project58 Managed folder policy"
    Import-CSV $csvfile | 
         foreach-object -process {
                  Set mailbox -identity $_.Identity -managedfoldermailboxpolicy $mbpolicy
         }
    
    
    

    This way the quotes are not hidden in the lines.

    The issue is use of back-ticks instead of single quotes (apostrophes).  Back-ticks are only used to escape a character in a string or as a line continuation character.  The behavior of the back-tick in this scenario is unpredictable or not easily predictable.

    Stay away from back-ticks in static strings.

     Al's example is also easy to see.

     

     


    ¯\_(ツ)_/¯