PowerShell如何打开Excel的多个实例



我的脚本从SPO打开了一张Excel表,在读取文件后,我关闭了它。问题是当我从两个PS实例运行此脚本时(Ctrl+T(虽然第一个脚本运行得很好,但当第二个脚本运行时,我收到一条弹出消息,说文件已经打开这是从excel 中读取的脚本

Connect-PnPOnline -Url $SharePointSiteURL
$ExcelObject = New-Object -ComObject Excel.Application
$ExcelWorkBook = $ExcelObject.Workbooks.Open($SharePointSiteURL)
$ExcelWorkSheet = $ExcelWorkBook.Sheets.Item("VIP List")
QuitExcel

function QuitExcel {
# when done, quit Excel and remove the used COM objects from memory (important)
$ExcelWorkBook.Close()
$ExcelObject.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelWorkSheet)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelWorkBook)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelObject)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Disconnect-PnPOnline

当第一个脚本访问xls时,访问同一xls的第二个脚本肯定会导致问题,因为它正在使用中。备用是在运行时复制相同的xls并使用它。

最新更新