You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行ExportWorkspace.ps1遇ParserError,直接粘贴代码却正常

问题:PowerShell脚本直接粘贴运行正常,通过powershell.exe调用时报语法错误

我有一个存储在D:\Ovie\Powershell路径下的ExportWorkspace.ps1文件,用于从Power BI Service导出JSON文件到本地目录。将文件内代码直接粘贴到PowerShell CLI运行时一切正常,但使用命令powershell.exe '& D:\Ovie\Powershell\ExportWorkspace.ps1'运行时,出现如下错误:

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

原脚本代码:

# #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
    }
}

问题原因

脚本中存在全角破折号(–)和HTML实体字符("),这些字符在PowerShell直接粘贴时会被自动转换为标准符号,但通过powershell.exe调用脚本文件时,解析器无法识别非标准字符,从而触发语法错误。

解决步骤

  1. 替换全角破折号:将所有–(全角)替换为-(半角减号),比如Login–PowerBI改为Login-PowerBI、–Format改为-Format。
  2. 替换HTML实体:将所有"替换为标准双引号",比如-Name "Ovie"改为-Name "Ovie"。
  3. 统一缩进格式:确保循环、条件语句的缩进一致,避免潜在解析问题。

修正后的脚本

# #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
    }
}

验证方法

修改后保存脚本,再次执行命令powershell.exe '& D:\Ovie\Powershell\ExportWorkspace.ps1',即可正常运行。

内容的提问来源于stack exchange,提问作者vie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 08:15:05