简体   繁体   English

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

[英]Refresh Excel dataset in SharePoint Online with PowerShell 403 error

I have been attempting to implement a PowerShell script that will access an Excel workbook, check it out, refresh the dataset in the workbook and finally check it back in again. 我一直在尝试实现PowerShell脚本,该脚本将访问Excel工作簿,将其检出,刷新工作簿中的数据集,最后再次将其检入。

I've combined this with a task in Windows Task Scheduler to run the script daily from a server with a user account that has access to the SharePoint Online site. 我将其与Windows Task Scheduler中的任务结合在一起,以每天使用具有访问SharePoint Online网站访问权限的用户帐户从服务器运行脚本。

My issue is that the script will not run. 我的问题是脚本将无法运行。 When I view the Windows Event logs I can see it is getting a 403 error 当我查看Windows事件日志时,我看到它收到403错误

The script was taken from the document found here document: Link to download document 该脚本取自以下文档中的文档: 下载文档的链接

The the task gets the following script and the location of the Excel Workbook from arguments in the action config of the task (detailed in the document above) 该任务从任务的动作配置中的参数获取以下脚本和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)

Am I missing something here? 我在这里想念什么吗?

Thanks in advance and please let me know if more info is required. 在此先感谢您,如果需要更多信息,请告诉我。

EDIT: The script works if run manually from cmd with the correct arguments. 编辑:如果使用正确的参数从cmd手动运行,则脚本可以工作。 Problem seems to be that Task Scheduler cannot access PowerShell. 问题似乎是任务计划程序无法访问PowerShell。

So I got this working finally. 所以我终于完成了这项工作。 Issue was that it wasn't running the script when the option 'User logged on or not' in the general settings of the task was selected. 问题在于,在任务的常规设置中选择了“用户是否登录”选项时,它没有运行脚本。 Worked fine when 'User logged on' was selected. 当选择“用户登录”时,工作正常。

Here are the steps I had to take to get this to run properly. 这是我必须采取的步骤才能使其正常运行。

First the script needed to run from the System32 folder (also specify that directory in the tasks "Start In" box. Also make sure that you are pointing to the 32-bit version of PowerShell since Excel won't work with 64-bit 首先,需要从System32文件夹运行脚本(还要在任务“开始于”框中指定该目录。还要确保您指向的是32位版本的PowerShell,因为Excel无法与64位版本一起使用

And second, turns out there is a bug with Excel where you have to create a folder called “Desktop” in the \\SysWOW64\\config\\systemprofile\\ and \\System32\\config\\systemprofile\\ directories(both folders need to be created if running Excel 64-bit). 其次,事实证明Excel有一个错误,您必须在\\ SysWOW64 \\ config \\ systemprofile \\和\\ System32 \\ config \\ systemprofile \\目录中创建一个名为“ Desktop”的文件夹(如果运行Excel,则需要创建两个文件夹) 64位)。

This is the final argument string I ended up using: C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\Powershell.exe -nologo –noprofile -noninteractive -executionpolicy bypass path\\to\\script 'path\\to\\excelworkbook' 这是我最终使用的最后一个参数字符串: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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM