我一直在試圖實現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。
您的$ args/$我看起來像什麼? –
嗨,我相信$ args/$ i是從任務的參數中獲得的。 $ i是工作簿的路徑,foreach循環只是爲了迎合多個工作簿路徑。 –