问题描述
我正在尝试使用 PowerShell 从 SharePoint 打开 Excel 工作簿.我没有加载 SharePoint 管理单元 - 我没有.
I'm trying to open an Excel workbook from SharePoint using PowerShell. I'm not loading the SharePoint snap-in—I do not have it.
当 PowerShell 尝试启动工作簿时,SharePoint 会提示输入凭据.问题是我们正在尝试安排脚本,我们希望脚本具有类似 SSO 的体验.
When PowerShell tries to launch the workbook, SharePoint prompts for credentials. The problem is that we're trying to schedule the script, and we would like the script to have an SSO-like experience.
这是 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
推荐答案
首先在本地下载SharePoint文件,然后打开、操作等
First, download the SharePoint file locally, then open, manipulate, etc.
以下示例使用运行脚本/PowerShell 会话的用户的默认凭据.
The example below uses the default creds of the user running the script/PowerShell session.
$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
这篇关于使用 PowerShell 从 SharePoint 打开 Excel 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!