在 Powershell 中打开带有链接的文件时,如何防止 Excel 尝试连接到其链接?

How do I prevent Excel from attempting to connect to its links when opening a file with links in Powershell?

我有一个从任务计划程序运行的 PS 脚本。将 .xlsm 文件添加到监视文件夹时,它会打开该文件并从中收集特定数据,然后将其输出为 CSV。我最近遇到的问题是 XLSM 文件有 links 到外部 SharePoint 站点。任何时候用它挂起的脚本打开这些文件。如果我尝试手动打开文件 Excel 首先要求我 "Enable Content",然后一旦我单击以启用提示以输入我的凭据以连接到与 link 关联的 SharePoint 站点。

断了link然后运行脚本可以确认问题解决了,肯定是这个link挂了脚本。在打开文件之前,我已经尝试研究打破 link 的方法,但我收集的不多。我能找到的所有资源都参考了通过 Powershell 更新 links,而不是破坏它们。

以下是与打开文件相关的代码片段:

$watchedfolder = "C:\Watched"
$filedirectory = Get-ChildItem $watchedfolder | Where-Object {($_.Extension -eq ".xlsm")} | Select-Object -ExpandProperty Name 

foreach ($file in $filedirectory){
    $sheetName = "Daily Dash"

    #OPEN EXCEL WORKBOOK
    $objExcel = New-Object -ComObject Excel.Application
    $workbook = $objExcel.Workbooks.Open("C:\Watched$file")
    $sheet = $workbook.Worksheets.Item($sheetName)
    $objExcel.Visible = $false
    $objExcel.DisplayAlerts = $false
    $rowMax = ($sheet.UsedRange.Rows).count

我不确定我可以向我的脚本的这个开头部分添加什么来阻止尝试连接到 SharePoint 站点。有什么建议吗?

我目前无法对此进行测试,但您需要在打开文件之前对Excel对象进行设置。

AskToUpdateLinks 属性 设置为 $false 应该可以满足您的要求。

#OPEN EXCEL WORKBOOK
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$objExcel.DisplayAlerts = $false
$objExcel.AskToUpdateLinks = $false
$workbook = $objExcel.Workbooks.Open("C:\Watched$file")
$sheet = $workbook.Worksheets.Item($sheetName)
$rowMax = ($sheet.UsedRange.Rows).count