How to close the CD drawer in Powershell

I am writing a script that will prevent a user from logging off until they have removed any CDs that are in the machine. The script ejects the CD to make it convenient for the user to pick up (and make it blatantly obvious what the problem is).

My problem is that once I have told the CD to eject, I am unable to check and see if the user did in fact, remove the CD. I.e., I can't close the drawer or get a good status on it while it is open.

Please be gentle. I am not a programmer and so my code is likely not too efficient. But in case it is necessary:

# Displays the attention message box & checks to see if the user clicks the ok button.
function Show-MessageBox ($title, $msg)

 [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
 [Windows.Forms.MessageBox]::Show($msg, $title, [Windows.Forms.MessageBoxButtons]::OK,
    [System.Windows.Forms.MessageBoxIcon]::Warning, [System.Windows.Forms.MessageBoxDefaultButton]::Button1,
    [System.Windows.Forms.MessageBoxOptions]::DefaultDesktopOnly) | Out-Null 
}
$computer = "LocalHost"
$namespace = "root\CIMV2"
# Check to see if a disk is in the CD drive
$CDROM_Present = Get-WmiObject -Class Win32_CDROMDrive -property "MediaLoaded" -computername $computer -namespace $namespace
#  Get the drive letter for the CD drive
$CDROM_Letter = Get-WmiObject -class Win32_CDROMDrive -property "Drive" -computername $computer -namespace $namespace
$count = 0
while ($CDROM_Present.MediaLoaded -eq "True")
    {
    Write-Host -NoNewline "`a`a`a`a`a" # Alert tone (5)
 
 # Checks whether this is the first time the message box is displayed.
 if ($count -lt 1)
       {
        # Eject the CD
        if ($cdrom_present.medialoaded -eq "True")
           {
           $sa = new-object -com Shell.Application
           $sa.Namespace(17).items() | ForEach {If ($_.Name -match $CDROM_Letter) {$_.InvokeVerb("E&ject")}}
           [System.Runtime.Interopservices.Marshal]::ReleaseComObject($sa)
           }
      Show-MessageBox "ATTENTION: Removable Media in Drive" "`nThere is a CDROM present. Please remove then click OK."
    $count++
    }
    # If it's not the first time then display a different message
 else
       { Show-MessageBox "ATTENTION: Removable Media in Drive" "`nThe CDROM was not removed. Please remove then click OK."
       }
    # Re-check for CDROM, then continue while loop if applicable
    $CDROM_Present = Get-WmiObject -Class Win32_CDROMDrive -property "MediaLoaded" -computername $computer -namespace $namespace 
    }

 

  • Edited by Teejay58 Wednesday, September 22, 2010 12:35 AM scripting language not specified
September 22nd, 2010 12:34am

If you cannot find a way to do it directly in PowerShell then you could invoke one of the many eject tools that float about, e.g. this one:

eject.exe
EJECT v2.02 - Removable Media Eject/Load
Copyright (c) 1998-2001 WebGeek, Inc.

Free Windows Admin Tool Kit Click here and download it now
September 22nd, 2010 5:53am

code to invoke it would be nice. I have no idea how to do what you suggest. It does not answer my question.
September 25th, 2010 7:25pm

Modified an old script from MOW this creates a function that can open/close the Cd-ROM (cannot test the close part though, only have laptops right here)

 

$MemDef = @"
[DllImport("winmm.dll", CharSet = CharSet.Ansi)]
   public static extern int mciSendStringA(
   string lpstrCommand,
   string lpstrReturnString,
   int uReturnLength,
   IntPtr hwndCallback);
"@

$winnm = Add-Type -memberDefinition $MemDef -ErrorAction 'SilentlyContinue' -passthru -name mciSendString
function Set-CDAudioDoor {
 Param(
  [ValidateSet("open", "closed")][String[]]$Mode = 'open'
 )
 $winnm::mciSendStringA("set cdaudio door $mode", $null, 0,0)}

 Set-CDAudioDoor closed

Free Windows Admin Tool Kit Click here and download it now
September 25th, 2010 9:44pm

Thanks! I'll test it when I get to work Monday.
September 25th, 2010 9:46pm

Well shoot. This code is a different language; I'd guess vbs. Unfortunately, I don't know how to combine vbs and powershell just yet. Looks like this is not going to be an easy answer. Giving up on it for now. Thanks anyway.
Free Windows Admin Tool Kit Click here and download it now
September 28th, 2010 7:32pm

The code is Powershell....

 

 One thing I have noticed if you are using PowerGUI, is that when you copy/paste Here-Strings, it sometimes wont parse them as Here-Strings, It seems as if it does not see the end "@ of the here-string

 

PS

 

Here-String is a multiline string that starts with @" and ends with "@

October 5th, 2010 8:08pm

Time and experience. I can now fully appreciate the code that Xenophane provided. And I made it work. :) Thanks for your contributions, guys.

  • Edited by Teejay58 11 hours 30 minutes ago
Free Windows Admin Tool Kit Click here and download it now
June 11th, 2015 3:39pm

Time and experience. I can now fully appreciate the code that Xenophane provided. And I made it work. :) Thanks for your contributions, guys.

  • Edited by Teejay58 Thursday, June 11, 2015 7:39 PM
June 11th, 2015 7:35pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics