There are a few potential issues with the code you have provided that could be causing it to hang:
The script is trying to open an Excel file over the network, which can be slow and may cause the script to appear to hang. You can try copying the file to a local directory first and then opening it with Excel.
You are using the "-ComObject" parameter to create a new instance of Excel, which can sometimes cause issues with Excel. Instead, you can use the "Invoke-Item" cmdlet to open the Excel file directly without creating a new instance of Excel.
Here is an updated version of your script that addresses these issues:
#script for file in share point $ExcelFilePath = "\domain.sharepoint.com@SSL\DavWWWRoot\Shared Documents\testFileRealData02.xlsx" $LocalFilePath = "C:\temp\testFileRealData02.xlsx" $webclient = New-Object System.Net.WebClient $webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials $webclient.DownloadFile($ExcelFilePath, $LocalFilePath)
$ExcelObject = New-Object -ComObject Excel.Application $ExcelWorkBook = $ExcelObject.Workbooks.Open($LocalFilePath) $ExcelWorkSheet = $ExcelWorkBook.Sheets.Item(1) Write-Host $ExcelWorkSheet.Cells.Item(1,1)
Cleanup
$ExcelWorkBook.Close($false) $ExcelObject.Quit() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelObject) | Out-Null
This version of the script downloads the Excel file to a local directory using the WebClient object before opening it with Excel. It also includes cleanup code to release the Excel object when finished, which is important to prevent memory leaks.
Comments
Post a Comment