簡體   English   中英

使用PowerShell 403錯誤刷新SharePoint Online中的Excel數據集

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM