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