在SharePoint Online中使用PowerShell 403错误刷新Excel数据集

我一直在试图实现一个PowerShell脚本,它将访问一个Excel工作簿,检查出来,刷新工作簿中的数据集,最后再次检查它。

我已经将这与Windows任务计划程序中的任务结合使用,可以使用具有对SharePoint Online站点的访问权限的用户帐户从服务器每天运行脚本。

我的问题是脚本不会运行。 当我查看Windows事件日志,我可以看到它得到一个403错误

该脚本是从这里find的文件中获取文件: 链接下载文件

该任务从任务的动作configuration中获取以下脚本和Excel工作簿的位置(详见上述文档)

try { # Creating the excel COM Object $xl = New-Object -ComObject Excel.Application; # Setting up Excel to run without UI and without alerts $xl.DisplayAlerts = $false; $xl.Visible = $false; } Catch { Write-EventLog -EventId "5001" -LogName "Application" -Message "Failed to start Excel" -Source "Application" Exit } foreach ($i in $args) { write-host "handling $i" try { # Allow update only if we can perform check out If ($xl.workbooks.CanCheckOut($i)) { # Opening the workbook, can be local path or SharePoint URL $wb = $xl.workbooks.open($i); # Perform the check out $xl.workbooks.checkout($i) # Calling the refresh $wb.RefreshAll(); # Saving and closing the workbook $wb.CheckInWithVersion(); # in case you are not using checkout/checkin perform a save and close #$wb.Save(); #$wb.Close(); #Release Workbook [System.Runtime.Interopservices.Marshal]::ReleaseComObject($wb) } else { write-host "Check out failed for: $i" Write-EventLog -EventId "5001" -LogName "Application" -Message "Workbook can't be checked out $i" -Source "Application" } } catch { Write-EventLog -EventId "5001" -LogName "Application" -Message "Failed refreshing the workbook $i $_" -Source "Application" } } #Quiting Excel $xl.quit(); #Release Excel [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) 

我在这里错过了什么?

预先感谢,请让我知道是否需要更多的信息。

编辑:脚本工作如果从正确的参数从cmd手动运行。 问题似乎是任务计划程序无法访问PowerShell。

所以我最终得到了这个工作。 问题在于,在任务的常规设置中select了“用户已login或未login”选项时,未运行脚本。 当“用户login”被选中时工作正常。

以下是我必须采取的步骤才能正确运行。

首先,需要从System32文件夹中运行脚本(也可以在“Start In”框中指定该目录),同时确保指向32位版本的PowerShell,因为Excel不能用于64位

第二,事实certificate有一个Excel的错误,你必须在\ SysWOW64 \ config \ systemprofile \和\ System32 \ config \ systemprofile \目录下创build一个名为“Desktop”的文件夹(如果运行Excel,需要创build两个文件夹64位)。

这是我最终使用的参数string:C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ Powershell.exe -nologo -noprofile -noninteractive -executionpolicy绕过path\到\脚本path\到\ excelworkbook'