[英]Refresh Excel dataset in SharePoint Online with PowerShell 403 error
我一直在尝试实现PowerShell脚本,该脚本将访问Excel工作簿,将其检出,刷新工作簿中的数据集,最后再次将其检入。
我将其与Windows Task Scheduler中的任务结合在一起,以每天使用具有访问SharePoint Online网站访问权限的用户帐户从服务器运行脚本。
我的问题是脚本将无法运行。 当我查看Windows事件日志时,我看到它收到403错误
该脚本取自以下文档中的文档: 下载文档的链接
该任务从任务的动作配置中的参数获取以下脚本和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。
所以我终于完成了这项工作。 问题在于,在任务的常规设置中选择了“用户是否登录”选项时,它没有运行脚本。 当选择“用户登录”时,工作正常。
这是我必须采取的步骤才能使其正常运行。
首先,需要从System32文件夹运行脚本(还要在任务“开始于”框中指定该目录。还要确保您指向的是32位版本的PowerShell,因为Excel无法与64位版本一起使用
其次,事实证明Excel有一个错误,您必须在\\ SysWOW64 \\ config \\ systemprofile \\和\\ System32 \\ config \\ systemprofile \\目录中创建一个名为“ Desktop”的文件夹(如果运行Excel,则需要创建两个文件夹) 64位)。
这是我最终使用的最后一个参数字符串:C:\\ Windows \\ System32 \\ WindowsPowerShell \\ v1.0 \\ Powershell.exe -nologo –noprofile -noninteractive -executionpolicy绕过路径\\ to \\ script'path \\ to \\ excelworkbook'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.