0

I've file ExportWorkspace.ps1 stored at D:\Ovie\Powershell. This file is export json file to my local directory from power bi service. It run normally when I copy the code inside file and direct paste into powershell cli But when I run using powershell.exe '& D:\Ovie\Powershell\ExportWorkspace.ps1' it show error :

At D:\Ovie\Powershell\ExportWorkspace.ps1:23 char:3
+   }
+   ~
Unexpected token '}' in expression or statement.
At D:\Ovie\Powershell\ExportWorkspace.ps1:48 char:5
+     }
+     ~
Unexpected token '}' in expression or statement.
At D:\Ovie\Powershell\ExportWorkspace.ps1:49 char:1
+ }
+ ~
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

Here is the code inside ExportWorkspace.ps1 :

# #Log in to Power BI Service
# Login–PowerBI  –Environment Public  

#First, Collect all (or one) of the workspaces in a parameter called PBIWorkspace
# $PBIWorkspace = Get-PowerBIWorkspace              # Collect all workspaces you have access to
$PBIWorkspace = Get-PowerBIWorkspace -Name "Ovie"   # Use the -Name parameter to limit to one workspace

#Now collect todays date
$TodaysDate = Get-Date –Format "yyMMdd_hhmmss" 

#Almost finished: Build the outputpath. This Outputpath creates a news map, based on todays date
$OutPutPath = "D:\Ovie\Powershell\pbi\" + $TodaysDate 

#Now loop through the workspaces, hence the ForEach
ForEach($Workspace in $PBIWorkspace)
{
  #For all workspaces there is a new Folder destination: Outputpath + Workspacename
  $Folder = $OutPutPath + "\" + $Workspace.name 
  #If the folder doens't exists, it will be created.
  If(!(Test-Path $Folder))
  {
    New-Item –ItemType Directory –Force –Path $Folder
  }
  #At this point, there is a folder structure with a folder for all your workspaces 
  
  #Collect all (or one) of the reports from one or all workspaces 
  $PBIReports = Get-PowerBIDataflow –WorkspaceId $Workspace.Id             # Collect all reports from the workspace we selected.
  #$PBIReports = Get-PowerBIReport -WorkspaceId $Workspace.Id -Name "My Report Name" # Use the -Name parameter to limit to one report
    
    #Now loop through these reports: 
    ForEach($Report in $PBIReports)
    {
      #Your PowerShell comandline will say Downloading Workspacename Reportname
      Write-Host "Downloading "$Workspace.name":" $Report.name 
      
      #The final collection including folder structure + file name is created.
      $OutputFile = $OutPutPath + "\" + $Workspace.name + "\" + $Report.name + ".json"
      
      # If the file exists, delete it first; otherwise, the Export-PowerBIReport will fail.
       if (Test-Path $OutputFile)
        {
          Remove-Item $OutputFile
        }
      
      #The pbix is now really getting downloaded
      Export-PowerBIDataflow –WorkspaceId $Workspace.ID –Id $Report.ID –OutFile $OutputFile
      # Export-PowerBIReport –WorkspaceId $Workspace.ID –Id $Report.ID –OutFile $OutputFile
    }
}

Please help, Thank you

Sincerely, Oviedityanto

0

1 Answer 1

1

Oh okay this is because "dash". It's some other character, not "dash". I've replace "–" to "-", resave and run int again using powershell.exe '& D:\Ovie\Powershell\ExportWorkspace.ps1' work well

Sign up to request clarification or add additional context in comments.

2 Comments

This frequently occurs when code is copied from work processing documents like Microsoft Word or from web sites. scribbr.com/language-rules/dashes/….
Indeed your code uses (EN DASH, U+2013) instead of the ASCII-range - (HYPHEN-MINUS, U+002D). However, this in itself is not a problem, so the likeliest explanation is that your script file is misinterpreted by the Windows PowerShell engine, which happens if the script is saved as UTF-8 without a BOM (this is no longer a problem in PowerShell (Core) 7+). Saving you script as UTF-8 with BOM is an alternative.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.