简体   繁体   中英

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.

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.

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

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)

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. Problem seems to be that Task Scheduler cannot access 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

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).

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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