===================================================================
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
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
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.
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 ?
$obj.Quit()
spps -n excel
"OldDog" wrote:
> On Dec 16, 2:31 am, "IT Staff" <jkk...@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..
Always a pleasure
EXCEL stays in Task Manager, but if I then manually type "spps -n excel" in
the open shell it works. I don't get it.
I call my script from the Run line: powershell.exe -noexit
c:\PStest\read.ps1, with the closing lines in the script
$xl.Quit()
spps -n excel
Everything in the script does as intended (thaks to your cookbook and blog!)
but it isn't until after I manually type the spps cmdlet that the process
ends.
What are your thoughts on this, and do you have any solution suggestions?
Thanks,
Gordo
> On Dec 30, 1:58 pm, gordo <go...@discussions.microsoft.com> wrote:
> > Works like a charm, thanks OldDog.
> >
> > $obj.Quit()
> > spps -n excel
> Always a pleasure
I created the problem with this:
if ($Project.length -eq 0) {exit}
$xl.Quit()
spps -n excel
And corrected it with this:
if ($Project.length -eq 0)
{
$xl.Quit()
spps -n excel
exit
I take it that it's working now?
Now, I just need to get my array of arrays working :)
>
> I take it the it's working now?
>