4

#PSTip Ejecting and closing CDROM drive–the PowerShell way!

Note: This tip requires PowerShell 2.0 or above.

I came across a project on Github (nothing related to PowerShell though) that provides a web-based UI for running system commands. This is based on Node.js implementation and does not support Windows platform yet. So, I was thinking about adding some PowerShell support.

I started discussing this with a few folks and they suggested an alternative for running system commands on Windows. While looking at that, I stumbled upon the CDROM eject and close commands supported by this 3rd-party tool. I wanted to see how easy that is to do in PowerShell. So, here is what I came up with.

Add-Type -TypeDefinition  @'
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace CDROM
{
    public class Commands
    {
        [DllImport("winmm.dll")]
        static extern Int32 mciSendString(string command, string buffer, int bufferSize, IntPtr hwndCallback);

        public static void Eject()
        {
             string rt = "";
             mciSendString("set CDAudio door open", rt, 127, IntPtr.Zero);
        }

        public static void Close()
        {
             string rt = "";
             mciSendString("set CDAudio door closed", rt, 127, IntPtr.Zero);
        }
    }
}
'@

[CDROM.Commands]::Eject()
[CDROM.Commands]::Close()

The above snippet uses the .NET interop namespace to invoke a Win32 API for working with multimedia devices. Btw, I came across several blog posts on using WScript Shell and Windows Media Player OCX to achieve this task. But, like I mentioned, this is more PowerShell way of doing it. Drop a comment if you are aware of any alternative methods.

Filed in: Columns, Tips and Tricks Tags: , ,

3 Pingbacks/Trackbacks

4 Responses to "#PSTip Ejecting and closing CDROM drive–the PowerShell way!"

  1. pamarths says:

    Another alternative is to use IMAPI interface. There are COM objects related to Image Writing which we can leverage to Eject or close a CD Drive. http://techibee.com/powershell/eject-or-close-cddvd-drive-using-powershellalternative-to-windows-media-objects/2176

Leave a Reply

Submit Comment

© 2014 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: