locked
Problem exporting to csv (Unexpected token '}' in expression or statement. At E:\powershell\Get_All_Admins_By_Group.ps1:11 char:7) RRS feed

  • Question

  • Full error:

    Unexpected token '}' in expression or statement.
    At E:\powershell\Get_All_Admins_By_Group.ps1:11 char:7
    +      } <<<<  | Export-CSV e:\output\admins.csv -NoTypeInformation
        + CategoryInfo          : ParserError: (}:String) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedToken
    

    My script

    Get-ADGroup  -Filter 'Name -like "MyCo*Admins"' |
         ForEach-Object{
              $hash=@{GroupName=$_.Name;Member=''}
              $_ | Get-ADGroupMember -ea 0 -recurs |
                   ForEach-Object{
                        $hash.Member=$_.Name
                        New-Object psObject -Property $hash
                   }
              } |
         sort groupname,member
         } |
          Export-CSV e:\output\admins.csv -NoTypeInformation
    

    I know it's I have to put a bracket somewhere but can't seem to figure out where. If I take the last 2 lines away it outputs to screen fine.

    Tom

    
    Wednesday, May 14, 2014 3:06 PM

Answers

  • The clue is in the error message, its pointing to the unexpected token.

    This one...

         sort groupname,member
         } |
          Export-CSV e:\output\admins.csv -NoTypeInformation
         sort groupname,member |
          Export-CSV e:\output\admins.csv -NoTypeInformation

    • Proposed as answer by Mike Laughlin Wednesday, May 14, 2014 3:36 PM
    • Marked as answer by TomG2014 Wednesday, May 14, 2014 3:42 PM
    Wednesday, May 14, 2014 3:27 PM

All replies

  • The clue is in the error message, its pointing to the unexpected token.

    This one...

         sort groupname,member
         } |
          Export-CSV e:\output\admins.csv -NoTypeInformation
         sort groupname,member |
          Export-CSV e:\output\admins.csv -NoTypeInformation

    • Proposed as answer by Mike Laughlin Wednesday, May 14, 2014 3:36 PM
    • Marked as answer by TomG2014 Wednesday, May 14, 2014 3:42 PM
    Wednesday, May 14, 2014 3:27 PM
  • That worked, thanks.

    I've been working on 2 issues with this script and decided to stop the first one and concentrate on this.

    Wednesday, May 14, 2014 3:31 PM