Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

how can i eject my cd-rom

Closed Thread
 
Thread Tools Display Modes
Old 11-15-2006   #1 (permalink)
9nevermind9
Guest


 

how can i eject my cd-rom

is it possible to eject my cd when my program finishes?
Old 11-15-2006   #2 (permalink)
Adam Milazzo
Guest


 

Re: how can i eject my cd-rom

9nevermind9 wrote:
> is it possible to eject my cd when my program finishes?

In short, yes.

But what program?
Old 11-16-2006   #3 (permalink)
dreeschkind
Guest


 

RE: how can i eject my cd-rom

"9nevermind9" wrote:

> is it possible to eject my cd when my program finishes?


sure:

##############
# start_and_eject.ps1
##############
notepad
$processToWatch = get-process notepad
$processToWatch.waitforexit()

$sa = new-object -com Shell.Application

# the next line depends on the language of your Windows
# see context menu of the drive (note the '&' for the accelerator key)

# english:
$sa.Namespace(17).ParseName('D:\').InvokeVerb("E&ject")

# german:
#$sa.Namespace(17).ParseName('D:\').InvokeVerb("A&uswerfen")

##############

--
greetings
dreeschkind
Old 11-16-2006   #4 (permalink)
fixitchris
Guest


 

RE: how can i eject my cd-rom

can you elaborate on the meaning of this line...

$sa.Namespace(17).ParseName('D:\').InvokeVerb("E&ject")
Old 11-16-2006   #5 (permalink)
Jean
Guest


 

Re: how can i eject my cd-rom

> is it possible to eject my cd when my program finishes?
ie using Media Player OCX :

#---8<---Eject_CDS.ps1---
$cds=(New-Object -com 'WMPlayer.OCX').cdromCollection
for($i=0;$i -lt $cds.Count;$i++)
{$cds.Item($i).Eject()}
#---8<---Eject_CDS.ps1---

Regards,

--
Jean - JMST
Belgium


Old 11-16-2006   #6 (permalink)
dreeschkind
Guest


 

RE: how can i eject my cd-rom

"fixitchris" wrote:

> can you elaborate on the meaning of this line...
>
> $sa.Namespace(17).ParseName('D:\').InvokeVerb("E&ject")


Sure.

As you can see in the line before, $sa is a variable that contains a COM
object("Shell.Application").

This COM object has some methods:

$sa | get-member

The method Namespace($id) returns a windows shell namespace by that ID.

To get all IDs you can use something like this (or websearch for
Shell.Application Namespace):

0..61 | ForEach { $sa.Namespace($_).Title + ' : ' + $_ }

Ok, now we know that 17 is "My Computer". This Namespace object has some
more methods:

$sa.Namespace(17) | gm

Then we use the "Parse()" method get the right object for your CD/DVD drive
(mine is on D:\).

$sa.Namespace(17).ParseName('D:\') | gm

We see the Method 'Verbs()" which will return all possible actions for this
item:

$sa.Namespace(17).ParseName('D:\').Verbs()

Using the method "InvokeVerb($verbname)" you can trigger that action by name:

$sa.Namespace(17).ParseName('D:\').InvokeVerb("E&ject")

The web is full of VBscript examples for COM scripting. Most of the scripts
can easily be transformed to PowerShell scripts.

Some good resources:
http://msdn2.microsoft.com/en-us/library/ms630423.aspx
http://www.microsoft.com/technet/scr....mspx?mfr=true

--
greetings
dreeschkind
Old 11-16-2006   #7 (permalink)
Jean
Guest


 

Re: how can i eject my cd-rom

> Using the method "InvokeVerb($verbname)" you can trigger that action by
> name:


Note here that verbs are localized strings, for this reason it seems to
me that the use of wmplayer ocx is more reliable for this action.

Regards,

--
Jean - JMST
Belgium


Old 11-16-2006   #8 (permalink)
dreeschkind
Guest


 

Re: how can i eject my cd-rom

"Jean" wrote:

> Note here that verbs are localized strings, for this reason it seems to
> me that the use of wmplayer ocx is more reliable for this action.


Yeah, I noticed that too (therefore I gave two localized examples in my
first post).
I'd appreciate it if anyone could tell me how to avoid this localization
issue with ".InvokeVerb()".
The reason that I like this approach is because you can use this for so much
more. You can for instance empty the recycle bin using the right namespace
and the right verb. I tried to get the correct localized verb string from the
..Verbs() array first (based on a fixed array index). But the problem is that
this order is not fixed because of custom context menu extensions.

I agree that the cd eject solution using wmplayer ocx is probably more
reliable for this specific action. (If I had known this solution before, then
I would have posted this one of course ;-)
However, I still think that wmplayer ocx will not work on all windows
editions. At least here in Europe Microsoft has been forced to sell "Windows
XP N" editions that don't ship with Windows Media Player...

--
greetings
dreeschkind
Old 11-16-2006   #9 (permalink)
Jean
Guest


 

Re: how can i eject my cd-rom

> "Jean" wrote:
>
>> Note here that verbs are localized strings, for this reason it seems to
>> me that the use of wmplayer ocx is more reliable for this action.

>
> Yeah, I noticed that too (therefore I gave two localized examples in my
> first post).
> I'd appreciate it if anyone could tell me how to avoid this localization
> issue with ".InvokeVerb()".


No localized enumarations are available, so ...

Sometimes it could be possible to retrieve a verb name used in the user
context by reading some file content (exe,ini, ...).
I had done that some times ago ... but I don't remember exaclty what
I'd done ... if I retrieve ...

> The reason that I like this approach is because you can use this for so much
> more. You can for instance empty the recycle bin using the right namespace
> and the right verb. I tried to get the correct localized verb string from the
> .Verbs() array first (based on a fixed array index). But the problem is that
> this order is not fixed because of custom context menu extensions.
>


Absolutely.
It's why this could be only for personal use (or a well known context).
And probably more as for security reasons shell.application is often
disable by admins.

> I agree that the cd eject solution using wmplayer ocx is probably more
> reliable for this specific action. (If I had known this solution before, then
> I would have posted this one of course ;-)
> However, I still think that wmplayer ocx will not work on all windows
> editions. At least here in Europe Microsoft has been forced to sell "Windows
> XP N" editions that don't ship with Windows Media Player...


It's true ... but here I haven't yet seen an XP N box to sell.
Reality is imho that nobody want to buy that ... but law is law :-)

Regards,

--
Jean - JMST
Belgium


Old 11-16-2006   #10 (permalink)
9nevermind9
Guest


 

RE: how can i eject my cd-rom

thanks, this example + explanation brightens my view about powerscript.
greetings
9nevermind9

"dreeschkind" wrote:

> "9nevermind9" wrote:
>
> > is it possible to eject my cd when my program finishes?

>
> sure:
>
> ##############
> # start_and_eject.ps1
> ##############
> notepad
> $processToWatch = get-process notepad
> $processToWatch.waitforexit()
>
> $sa = new-object -com Shell.Application
>
> # the next line depends on the language of your Windows
> # see context menu of the drive (note the '&' for the accelerator key)
>
> # english:
> $sa.Namespace(17).ParseName('D:\').InvokeVerb("E&ject")
>
> # german:
> #$sa.Namespace(17).ParseName('D:\').InvokeVerb("A&uswerfen")
>
> ##############
>
> --
> greetings
> dreeschkind

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: CD will not eject PvdG42 .NET General 0 07-19-2008 10:33 PM
Computer won't eject CDs Gizzy Gizbit Vista General 5 04-09-2008 05:53 PM
CD won't eject !!!!!!!!!! ttroy Vista General 3 08-21-2007 11:39 AM
Eject a Zip Disk Mesan PowerShell 1 07-25-2007 02:08 PM
CD Eject Problem Jassim Rahma Vista hardware & devices 3 06-24-2006 06:08 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50