Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: General » microsoft.public.windows.powershell
Thread: No way to close Excel. Any reason ?

HTVi
TV Discussion Newsgroups

No way to close Excel. Any reason ?
"IT Staff" <jkklim[ at ]hotmail.com> 12/16/2008 8:31:48 AM

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


Re: No way to close Excel. Any reason ?
OldDog <mikef2691[ at ]comcast.net> 12/16/2008 2:39:05 PM
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
Re: No way to close Excel. Any reason ?
"Marco Shaw [MVP]" <marco.shaw[ at ]_NO_SPAM_gmail.com> 12/16/2008 8:40:18 PM
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
Re: No way to close Excel. Any reason ?
"IT Staff" <jkklim[ at ]hotmail.com> 12/17/2008 1:12:14 AM
Even if excel 2003 (office 2003) is *visible*, it does not close.

Note that i run the script from command prompt.


Re: No way to close Excel. Any reason ?
"Marco Shaw [MVP]" <marco.shaw[ at ]_NO_SPAM_gmail.com> 12/17/2008 2:50:02 AM
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
Re: No way to close Excel. Any reason ?
"IT Staff" <jkklim[ at ]hotmail.com> 12/17/2008 6:03:56 AM
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 ?


Re: No way to close Excel. Any reason ?
gordo 12/30/2008 7:58:06 PM
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
>
Re: No way to close Excel. Any reason ?
OldDog <mikef2691[ at ]comcast.net> 12/30/2008 10:41:41 PM
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

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen