我尝试使用PowerShell从SharePoint打开Excel工作簿。我没有加载SharePoint管理单元 - 我没有它。
当PowerShell尝试启动工作簿时,SharePoint会提示输入凭据。问题是我们正在尝试安排脚本,我们希望脚本具有类似SSO的体验。
这是MWE:
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open(http://site/file.xlsx)
$Worksheet = $Workbook.Sheets.Item($WorksheetName)
$Excel.Visible = $false
# some Excel manipulation
$Workbook.Close($false)
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null
Remove-Variable Excel
答案 0 :(得分:3)
首先,在本地下载SharePoint文件,然后打开,操作等。
以下示例使用运行脚本/ PowerShell会话的用户的默认信用。
$fromfile = "http://site/file.xlsx"
$tofile = "c:\somedirectory\file.xlsx"
$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
$webclient.DownloadFile($fromfile, $tofile)
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($tofile)
$Worksheet = $Workbook.Sheets.Item($WorksheetName)
$Excel.Visible = $false
# some Excel manipulation
$Workbook.Close($false)
$Excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null
Remove-Variable Excel
答案 1 :(得分:2)
我最近遇到了同一问题。 我没有将其保存到本地计算机就解决了该问题。
COM Excel对象的WorkBook属性具有一种称为CheckIn和CheckOut的方法。您将需要同时使用两者。
此外,要直接从Sharepoint打开文件,只需转到Sharepoint页面上的库,然后在资源管理器中打开即可。 您将得到如下内容:
您只需将路径替换为UNC路径,如下所示:
\\ sitename \ sites \ TeamSite \ FileLibrary \
(在开始时使用双反斜杠)
$ExcelObject = New-Object -ComObject Excel.Application
$ExcelWorkBook = $ExcelObject.Workbooks.Open("\\sitename\sites\TeamSite\FileLibrary\test.xlsx")
$ExcelObject.Workbooks.CheckOut("\\sitename\sites\TeamSite\FileLibrary\test.xlsx")
$ExcelWorkSheet = $ExcelWorkBook.Sheets.Item(1)
#This part selects the A:1 cell and changes it's value.
$ExcelWorkSheet.Cells.Item(1,1) = "Edited text"
$ExcelWorkBook.Save()
$ExcelWorkBook.CheckIn()