首页 > 解决方案 > Powershell 使用 OLAP 查询刷新 Excel。凭证问题

问题描述

我对 PowerShell 脚本比较陌生,所以请耐心等待我:)

我有多个带有连接到 Power BI 数据集的 OLAP 查询连接的 excel 文件,以下是脚本;

 $libraryPath = "C:\Repos\AUSD\3.0\Test"  
 $excel = new-object -comobject Excel.Application 
 $excel.Visible = $false
 # Give delay to open  
 Start-Sleep -s 3  
 $allExcelfiles = Get-ChildItem $libraryPath -recurse -include “*.xls*”  
 foreach ($file in $allExcelfiles)  
 {  
      $workbookpath = $file.fullname  
           Write-Host "Updating " $workbookpath  
           # Open the Excel file  
           $excelworkbook = $excel.workbooks.Open($workbookpath)  
           $connections = $excelworkbook.Connections  
           # This will Refresh All the pivot tables data.  
           $excelworkbook.RefreshAll()  
           # The following script lines will Save the file.  
           $excelworkbook.Save()  
           $excelworkbook.Close()
           Write-Host "Update Complete " $workbookpath  

      }  
 $excel.quit()

如果遵循选项,它工作正常;$excel.Visible 为真

然而,这将在服务器中安排,希望这可以在后台完成,因此 $excel.Visible = $false

这导致以下错误;

错误 1 ​​- 需要凭据

错误 2 - 需要凭据

我怀疑这是由于 Excel 打开时发生的自动登录,由于它没有打开,它在登录过程中失败。

如何绕过或更确切地说设置凭据/权限?

标签: excelpowershell

解决方案


推荐阅读