0

I am trying to close excel after an SQL import script, but I am unable to release the Com object using the Interopservices method.

When I attempt to release the com object with [System.Runtime.Interopservices.Marshal]::ReleaseComObject(New-Object -Com Excel.Application) I receive an error that I am missing a bracket. I have reviewed a number of examples of this process in other peoples codes, and while I must be missing something, it is something that I keep missing and would love some assistance in locating it.

Below is my script.

$serverName = "SERVER";
$databaseName = "User_Data" ;
$tableName = "TABLE" ;
$filepath = "Path to Xls";

#create object to open Excel workbook
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open($filepath)
$Worksheet = $Workbook.Worksheets.Item(1)
$startRow = 2

#create System.DataTable
    $dt = new-object "System.Data.DataTable"                                
    [void]$dt.Columns.Add("StaffStudentID", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("SurName", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("FirstName", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("Year", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("HomeGroup", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("DOB", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("StaffStudent", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("email", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("ImageName", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("CardKey", [System.Type]::GetType("System.String"))
    [void]$dt.Columns.Add("F11", [System.Type]::GetType("System.String"))
    Do {                    
        $ColValues1 = $Worksheet.Cells.Item($startRow, 2).Value()
        $ColValues2 = $Worksheet.Cells.Item($startRow, 3).Value()
        $ColValues3 = $Worksheet.Cells.Item($startRow, 4).Value()
        $ColValues4 = $Worksheet.Cells.Item($startRow, 5).Value()
        $ColValues5 = $Worksheet.Cells.Item($startRow, 6).Value()
        $ColValues6 = $Worksheet.Cells.Item($startRow, 7).Value()
        $ColValues7 = $Worksheet.Cells.Item($startRow, 8).Value()
        $ColValues8 = $Worksheet.Cells.Item($startRow, 9).Value()
        $ColValues9 = $Worksheet.Cells.Item($startRow, 10).Value()
        $ColValues10 = $Worksheet.Cells.Item($startRow, 11).Value()
        $ColValues11 = $Worksheet.Cells.Item($startRow, 12).Value()
        $startRow++
        $dt.Rows.Add($ColValues1,$ColValues2,$ColValues3,$ColValues4,$ColValues5,$ColValues6,$ColValues7,$ColValues8,$ColValues9,$ColValues10,$ColValues11)
        }
    While ($Worksheet.Cells.Item($startRow,2).Value() -ne $null)

$Excel.Quit()

#connect to SQL Server and import the system.data.table
$SQLServerConnection = "Data Source=$serverName;Integrated Security=true;Initial Catalog=$databaseName;"
$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $SQLServerConnection
$bulkCopy.DestinationTableName = $tableName
$bulkCopy.WriteToServer($dt)


#Remove Veriables and com object 
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel)
Remove-Variable serverName
Remove-Variable databaseName
Remove-Variable tableName
Remove-Variable filepath
Remove-Variable excel
Remove-Variable workbook
Remove-Variable worksheet
Remove-Variable startRow
Remove-Variable dt
Remove-Variable bulkCopy
Remove-Variable sqlserverconnection

Thank you,

Wofen

Difster
  • 3,264
  • 2
  • 22
  • 32

1 Answers1

0

Do you need to save your changes before Quit()? This pattern works for me.

Powershell: Excel, save and close after run

Kory Gill
  • 6,993
  • 1
  • 25
  • 33