|
|
I've a excel spreadsheet that contains computer data on 1st column. AFter reading all rows and exit, the Excel.exe was not removed from the taskbar.
=================================================================== Add-PsSnapin Quest.ActiveRoles.ADManagement
function release-ref($ref) { [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ref) [system.gc]::collect() [system.gc]::WaitForPendingFinalizers() }
$xl = new-object -comobject excel.application $xl.Visible = $false $filename = "c.xls"
$wb = $xl.Workbooks.open("d:\$filename")
$ws = $wb.worksheets.item(1)
$row = 1 while ($true) { $computername = $ws.Cells.Item($row,1).text
if ($computername.length -eq 0) {exit} else {get-qadcomputer $computername" } $row++ }
## below does not remove excel.exe ## release-ref $ws release-ref $wb $xl.quit() release-ref $xl Remove-Variable xl
## below does not remove excel.exe ## $obj = gwmi -computer . -query "select * from win32_process where name='excel.exe'" $obj $obj.terminate()
## below does not remove excel.exe ## get-process EXCEL | stop-process
|
|
On Dec 16, 2:31 am, "IT Staff" <jkk...[ at ]hotmail.com> wrote:
[Quoted Text] > I've a excel spreadsheet that contains computer data on 1st column. AFter > reading all rows and exit, the Excel.exe was not removed from the taskbar.. > > =================================================================== > Add-PsSnapin Quest.ActiveRoles.ADManagement > > function release-ref($ref) > { > [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ref) > [system.gc]::collect() > [system.gc]::WaitForPendingFinalizers() > > } > > $xl = new-object -comobject excel.application > $xl.Visible = $false > $filename = "c.xls" > > $wb = $xl.Workbooks.open("d:\$filename") > > $ws = $wb.worksheets.item(1) > > $row = 1 > while ($true) > { > $computername = $ws.Cells.Item($row,1).text > > if ($computername.length -eq 0) {exit} else {get-qadcomputer > $computername" } > $row++ > > } > > ## below does not remove excel.exe ## > release-ref $ws > release-ref $wb > $xl.quit() > release-ref $xl > Remove-Variable xl > > ## below does not remove excel.exe ## > $obj = gwmi -computer . -query "select * from win32_process where > name='excel.exe'" > $obj > $obj.terminate() > > ## below does not remove excel.exe ## > get-process EXCEL | stop-process
Alex, over at the Power Shell group in Google talked about Excel not shutting down when told.
Excel is generally the worst offender this way - there have been problems with it not shutting down when told to that go back a very long time - but there definitely are other ActiveX applications that exhibit this kind of behavior. Since ActiveX applications are generally rare and each have their own quirks, I usually test whether they shut down in different situations when using them from PowerShell by keeping an eye on the process list, then release the object if they don't shut down correctly. Unfortunately, this isn't all that useful if you're keeping an application around to use during a PowerShell session. In those cases, I've noticed that even some of the better-behaved ActiveX applications don't work the way we might want. For example, some of the better-behaved ActiveX applications will indeed shut down automatically if they don't have an open, modified document when you close PowerShell by typing exit, but will hang around if you close PS with the "X" at the top of the shell window. My general rule of thumb is to always unhide ActiveX applications and minimize them. This at least ensures that I can see and close them if I exit PS the wrong way or lose connection to the app somehow.
One way in Power Shell to kill the process is;
Stop-Process -name EXCEL
Another is to try to release the com object after quiting:
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
Or you can just add these two lines to the end of your script:
$xl.quit() spps -n excel
|
|
IT Staff wrote:
[Quoted Text] > I've a excel spreadsheet that contains computer data on 1st column. AFter > reading all rows and exit, the Excel.exe was not removed from the taskbar. > > =================================================================== > Add-PsSnapin Quest.ActiveRoles.ADManagement > > > function release-ref($ref) > { > [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ref) > [system.gc]::collect() > [system.gc]::WaitForPendingFinalizers() > } > > > $xl = new-object -comobject excel.application > $xl.Visible = $false
If instead, you try making it visible, are you prompted in any way on the screen/console?
What version of Excel?
Marco
-- *Microsoft MVP - Admin Frameworks https://mvp.support.microsoft.com/profile/Marco.Shaw *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition (due December 15th, 2008) *PowerShell Co-Community Director - http://www.powershellcommunity.org *Blog - http://marcoshaw.blogspot.com
|
|
Even if excel 2003 (office 2003) is *visible*, it does not close.
Note that i run the script from command prompt.
|
|
IT Staff wrote:
[Quoted Text] > Even if excel 2003 (office 2003) is *visible*, it does not close. > > Note that i run the script from command prompt. > >
But you're not getting any kind of prompt right? I still want to make 100% sure...
Try something much simpler like just opening and then closing Excel to see if you can use your function to release excel.exe.
Marco
-- *Microsoft MVP - Admin Frameworks https://mvp.support.microsoft.com/profile/Marco.Shaw *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition (due December 15th, 2008) *PowerShell Co-Community Director - http://www.powershellcommunity.org *Blog - http://marcoshaw.blogspot.com
|
|
I perform this scenario
a) In powershell screen or command prompt executing the script. (unable to close from task list from my codes) b) run excel.exe manually which opens a typical excel workbook. (closing it remove it from task list)
When both above are running, i ran a command prompt
tlist "excel.exe" | find /i "excel.exe", results shown below
5276 EXCEL.EXE CmdLine: "C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE" /automation -Embedding (this is the powershell call) 0x30000000 EXCEL.EXE 5080 EXCEL.EXE Microsoft Excel - Book1 (this is the normal excel callup) CmdLine: "C:\PROGRA~1\MICROS~2\OFFICE11\EXCEL.EXE" 0x30000000 EXCEL.EXE
Any ideas ?
|
|
Works like a charm, thanks OldDog.
$obj.Quit() spps -n excel
"OldDog" wrote:
[Quoted Text] > On Dec 16, 2:31 am, "IT Staff" <jkk...[ at ]hotmail.com> wrote: > > I've a excel spreadsheet that contains computer data on 1st column. AFter > > reading all rows and exit, the Excel.exe was not removed from the taskbar.. > > > > =================================================================== > > Add-PsSnapin Quest.ActiveRoles.ADManagement > > > > function release-ref($ref) > > { > > [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ref) > > [system.gc]::collect() > > [system.gc]::WaitForPendingFinalizers() > > > > } > > > > $xl = new-object -comobject excel.application > > $xl.Visible = $false > > $filename = "c.xls" > > > > $wb = $xl.Workbooks.open("d:\$filename") > > > > $ws = $wb.worksheets.item(1) > > > > $row = 1 > > while ($true) > > { > > $computername = $ws.Cells.Item($row,1).text > > > > if ($computername.length -eq 0) {exit} else {get-qadcomputer > > $computername" } > > $row++ > > > > } > > > > ## below does not remove excel.exe ## > > release-ref $ws > > release-ref $wb > > $xl.quit() > > release-ref $xl > > Remove-Variable xl > > > > ## below does not remove excel.exe ## > > $obj = gwmi -computer . -query "select * from win32_process where > > name='excel.exe'" > > $obj > > $obj.terminate() > > > > ## below does not remove excel.exe ## > > get-process EXCEL | stop-process > > Alex, over at the Power Shell group in Google talked about Excel not > shutting down when told. > > Excel is generally the worst offender this way - there have been > problems > with it not shutting down when told to that go back a very long time - > but > there definitely are other ActiveX applications that exhibit this kind > of > behavior. Since ActiveX applications are generally rare and each have > their > own quirks, I usually test whether they shut down in different > situations > when using them from PowerShell by keeping an eye on the process list, > then > release the object if they don't shut down correctly. > Unfortunately, this isn't all that useful if you're keeping an > application > around to use during a PowerShell session. In those cases, I've > noticed that > even some of the better-behaved ActiveX applications don't work the > way we > might want. For example, some of the better-behaved ActiveX > applications > will indeed shut down automatically if they don't have an open, > modified > document when you close PowerShell by typing exit, but will hang > around if > you close PS with the "X" at the top of the shell window. My general > rule of > thumb is to always unhide ActiveX applications and minimize them. This > at > least ensures that I can see and close them if I exit PS the wrong way > or > lose connection to the app somehow. > > One way in Power Shell to kill the process is; > > Stop-Process -name EXCEL > > Another is to try to release the com object after quiting: > > [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) > > Or you can just add these two lines to the end of your script: > > $xl.quit() > spps -n excel >
|
|
On Dec 30, 1:58 pm, gordo <go...[ at ]discussions.microsoft.com> wrote:
[Quoted Text] > Works like a charm, thanks OldDog. > > $obj.Quit() > spps -n excel > > > > "OldDog" wrote: > > On Dec 16, 2:31 am, "IT Staff" <jkk...[ at ]hotmail.com> wrote: > > > I've a excel spreadsheet that contains computer data on 1st column. AFter > > > reading all rows and exit, the Excel.exe was not removed from the taskbar.. > > > > =================================================================== > > > Add-PsSnapin Quest.ActiveRoles.ADManagement > > > > function release-ref($ref) > > > { > > > [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ref) > > > [system.gc]::collect() > > > [system.gc]::WaitForPendingFinalizers() > > > > } > > > > $xl = new-object -comobject excel.application > > > $xl.Visible = $false > > > $filename = "c.xls" > > > > $wb = $xl.Workbooks.open("d:\$filename") > > > > $ws = $wb.worksheets.item(1) > > > > $row = 1 > > > while ($true) > > > { > > > $computername = $ws.Cells.Item($row,1).text > > > > if ($computername.length -eq 0) {exit} else {get-qadcomputer > > > $computername" } > > > $row++ > > > > } > > > > ## below does not remove excel.exe ## > > > release-ref $ws > > > release-ref $wb > > > $xl.quit() > > > release-ref $xl > > > Remove-Variable xl > > > > ## below does not remove excel.exe ## > > > $obj = gwmi -computer . -query "select * from win32_process where > > > name='excel.exe'" > > > $obj > > > $obj.terminate() > > > > ## below does not remove excel.exe ## > > > get-process EXCEL | stop-process > > > Alex, over at the Power Shell group in Google talked about Excel not > > shutting down when told. > > > Excel is generally the worst offender this way - there have been > > problems > > with it not shutting down when told to that go back a very long time - > > but > > there definitely are other ActiveX applications that exhibit this kind > > of > > behavior. Since ActiveX applications are generally rare and each have > > their > > own quirks, I usually test whether they shut down in different > > situations > > when using them from PowerShell by keeping an eye on the process list, > > then > > release the object if they don't shut down correctly. > > Unfortunately, this isn't all that useful if you're keeping an > > application > > around to use during a PowerShell session. In those cases, I've > > noticed that > > even some of the better-behaved ActiveX applications don't work the > > way we > > might want. For example, some of the better-behaved ActiveX > > applications > > will indeed shut down automatically if they don't have an open, > > modified > > document when you close PowerShell by typing exit, but will hang > > around if > > you close PS with the "X" at the top of the shell window. My general > > rule of > > thumb is to always unhide ActiveX applications and minimize them. This > > at > > least ensures that I can see and close them if I exit PS the wrong way > > or > > lose connection to the app somehow. > > > One way in Power Shell to kill the process is; > > > Stop-Process -name EXCEL > > > Another is to try to release the com object after quiting: > > > [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl) > > > Or you can just add these two lines to the end of your script: > > > $xl.quit() > > spps -n excel- Hide quoted text - > > - Show quoted text -
Always a pleasure
|
|
|