使用PowerShell刷新Office 365上的Excel连接
有一个Office 365站点,其中包含多个Excel文档,这些文档的连接必须每6小时刷新一次。我希望开发一个简单的PowerShell脚本:使用PowerShell刷新Office 365上的Excel连接,excel,powershell,office365,webdav,Excel,Powershell,Office365,Webdav,有一个Office 365站点,其中包含多个Excel文档,这些文档的连接必须每6小时刷新一次。我希望开发一个简单的PowerShell脚本: 签出并打开一个文件 刷新连接 保存并签入文件 我编写了这个简单的PowerShell脚本来刷新作为参数传递的任何路径,并向开发人员发送成功/电子邮件通知。我的计划是使用Task Scheduler计划定期执行此任务,并传递Excel文件的SSL(WebDav)路径 示例路径: \\microsoft.sharepoint.com@SSL\DavWWWRo
Task Scheduler
计划定期执行此任务,并传递Excel文件的SSL(WebDav)路径
示例路径:
\\microsoft.sharepoint.com@SSL\DavWWWRoot\teams\project\files\Workbook.xlsx
PowerShell脚本:
# Configurables
$EmailFrom ="norreply.projectemail@gmail.com"
$EmailTo = "myemail@domain.com"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("USERNAME", "PASSWORD")
# Open new instance of Excel
$excel = New-Object -ComObject Excel.Application
$excel.visible = $true
# Give delay to open
Start-Sleep -s 5
Foreach ($file In $args)
{
Write-Host ""
$fileName = (Split-Path -Path $file -Leaf)
If ((Test-Path $file) -And ($excel.Workbooks.CanCheckOut($file)))
{
Try
{
# The following code applies only for excel files in document library, can be skiped for others
$excelworkbook = $excel.Workbooks.Open($file)
$excelworkbook = $excel.Workbooks.CheckOut($file)
$excelworkbook = $excel.Workbooks.Open($file) # Opening a second time after checkout is required
# Refresh all data connections.
Write-Host "Refreshing: ", $file
$excelworkbook.RefreshAll()
# Checkin the file
Start-Sleep -s 15
$excelworkbook.CheckInWithVersion()
SendSuccessEmail $fileName
}
Catch
{
SendErrorEmail $fileName $_.Exception.Message
}
}
Else
{
SendErrorEmail $fileName "Unable to locate or checkout file. Please verify file exists and is checked in."
}
}
# Close Excel instance
$excel.quit()
这很有效,但前提是我的令牌保持活动状态。我通过单击SharePoint中的“使用资源管理器打开”按钮来完成此操作。此时,我可以使用SSL WebDav路径访问文件。当代币第二天到期时,我必须再次单击“使用资源管理器打开”按钮进行续订
是否有任何编程方法更新我的令牌以使我的脚本持续工作一天以上,或者我的做法是错误的?也许您可以使用
Microsoft.Sharepoint.Client
从Sharepoint服务器下载文件这里有描述下载()和上载()的链接