Instead, you are creating new Excel and workbook objects inside the loop over and over again where there is absolutely no reason for it since you can re-use the one you created before the loop.Īs an aside: The following characters are not allowed in excel worksheet names \/?*: You are creating a $Script:Excel = New-Object -ComObject excel.application object before the foreach loop but you don't use that. Whenever you have created Com objects and finished with them, use these lines to free up the memory: $Excel.Quit() Looking at your script, it doesn't surprise me you eventually run out of memory, because you are continouisly creating Com objects and never release them from memory. Should continue script and create the xlsx. $Connector = $($txtconnector,$worksheet.Range("A1")) $Excel = New-Object -ComObject Excel.Application
# Loops through each CVS, pulling all the data from each one $script:Excel = New-Object -ComObject Excel.Application $csvs = dir -path $CsvDir # Collects all the. $Ma4 = $FSCSV + "All Server Shares for Domain $CH4" $script:EC元 = $E4 + "Shares_Exceptions_$Date.txt" $script:ERL1 = $E4 + "Shares_Errors1_$Date.txt" $script:ECL1 = $E4 + "Shares_Exceptions1_$Date.txt" $script:ER元 = $E4 + "Shares_Errors_$Date.txt" $script:Message3 = "Unknown Errors Occurred" $script:FSCSV = "C:\Temp\Server_Shares\Server Lists\"
I have tried single csv with the same code and still the same result. + FullyQualifiedErrorId : System.OutOfMemoryException + CategoryInfo : OperationStopped: (:), OutOfMemoryException Insufficient memory to continue the execution of the program.Īt C:\Temp\Scripts\Shares_Complete.psm1:254 char:13 On its own the script runs fine, but when added to the larger one it fails. However, when the script runs as part of a larger script it failes when it refreshes the query. I have a script that cycles through a folder and condenses multiple CSVs to one xlsx file with the names of the CSV as worksheets.