Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Refresh Excel dataset in SharePoint Online with PowerShell 403 error

$
0
0
Hi,

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: https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CB0QFjAAahUKEwiBopKGmrXHAhVG6xQKHSIqAPY&url=http%3A%2F%2Fdownload.microsoft.com%2Fdownload%2FF%2FA%2F5%2FFA539F71-D658-4C59-A640-2E69CC6FFBA0%2FRefreshDataModelWorkbooksSharePointOnline.docx&ei=E37UVcHMHMbWU6LUgLAP&usg=AFQjCNHF6XR4PwpaHyCPFLOFXbUkaqOyJA&sig2=_SNLhV8mxDYorF5eLD1Gfw&bvm=bv.99804247,d.d24

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.

Viewing all articles
Browse latest Browse all 7589

Trending Articles