2015-08-19 56 views
1

我一直在試圖實現PowerShell腳本,該腳本將訪問Excel工作簿,檢出它,刷新工作簿中的數據集並最終再次檢查它。使用PowerShell 403在SharePoint Online中刷新Excel數據集錯誤

我已將此項與Windows任務計劃程序中的任務相結合,以便每天通過可訪問SharePoint Online網站的用戶帳戶從服務器運行腳本。

我的問題是腳本不能運行。當我查看Windows事件日誌,我可以看到它是得到一個403錯誤

劇本是從文件採取在這裏找到文檔: Link to download document

的任務得到下面的腳本和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運行正確的參數,腳本工作。問題似乎是Task Scheduler無法訪問PowerShell。

+0

您的$ args/$我看起來像什麼? –

+0

嗨,我相信$ args/$ i是從任務的參數中獲得的。 $ i是工作簿的路徑,foreach循環只是爲了迎合多個工作簿路徑。 –

回答

0

所以我最終得到了這個工作。問題是當選擇了任務的常規設置中的「用戶已登錄或未登錄」選項時,它沒有運行腳本。當「用戶登錄」被選中時工作正常。

下面是我必須採取的步驟才能正確運行。

首先需要從System32文件夾運行的腳本(也可以在「Start In」框中指定該目錄),並確保指向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繞過路徑\ \ \ \'路徑\ \ \ \ excelworkbook'

相關問題