Answer the question!

An automation scripts that prompts you is a letdown, to say the least.

cdlock

A typical example of such an event is the question you get when you try to unmount a CD or DVD drive from a VM. Some Linux guest OS will place a lock on the CD or DVD, and vSphere will ask you if you want to bypass this lock. And your script just hangs there, waiting for you to reply 🙁

I have been looking for some time to come up with a solution for this automation “issue”. And finally I came up with a working solution 🙂

The Problem

The problem is quite simple to recreate. Execute the following code for a VM that is running a Linux guest OS, and that has an ISO mounted.

The following question will appear, provided you have the vSphere Client open.

question

My initial idea was to use asynchronous events on a Timer object to answer the question. But that didn’t work, since for the action code attached to such a Timer event to fire, the code has to pass through the .Net  framework. Which in case of such an outstanding question, it didn’t.

An alternative solution that I got working, was with a background job that is started just before the unmount is attempted.

The Script

Annotation

Line 20: the function can accept the VM(s) from the pipeline

Line 27-45: the background job that will check if the unmount has caused a question. If yes, the background job will answer the question with “Yes“. The background job times out after 5 seconds.

Line 34: In a background job, you can reuse an open vSphere Server connection by using the SessionSecret property.

Line 36-38: The function looks for an outstanding “locked CD rom door” question for the specific VM. If such a question is found, the function will reply with “Yes”

Line 46-48: If no vSphere Server was passed on the Server parameter, the script will use the default vSphere Server connection that is to be found in $global:defaultviserver

Line 57: The actual unmount command. It will remove the mounted CD/DVD by using the NoMedia switch.

Sample Usage

The usage of the function is quite simple. You can call the function for a single VM

This will unmount the CD/DVD from the specific VM. The VM needs to be on the currently connected vSphere Server ($global:defaultviserver)

Or you can use it in a pipeline construct.

This will find all the VMs that have a CD/DVD mounted, and will unmount the CD/DVD

Enjoy!

11 Comments

    Bjørn-Ove

    Nice script…but I’m having trouble with the “start-job” and running the scriptblock $cdQuestion in some environments. Don’t know why…but that made me think, it must be another way…and it is. Used “Try” and “Catch” – see the below snippet from my script.

    PS! The “-option” in the command line “Set-VMQuestion -VMQuestion $question -Option Yes -Confirm:$false” have changed value from “Yes” to “button.yes”.

    Script snippet:
    $Chk_CDROM = ($VM | Get-CDDrive).IsoPath
    if ($Chk_CDROM -match $VM.Name)
    {
    Write-Host -NoNewLine -Fore Yellow “Unmounting CD-ROM – ”

    Try
    {
    $VM | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$false -ErrorAction Stop | out-null
    }
    Catch
    {
    Write-Host -NoNewLine “Unmount failed…answering question… – ”
    $a = 0
    while ($a -lt 5)
    {
    #Write-Host $a
    $question = $VM | Get-VMQuestion #| ? {$_.Text -match “locked the CD-ROM”})
    if($question)
    {
    $question | Set-VMQuestion -Option “button.yes” -Confirm:$false
    }
    $a++
    sleep 1
    }
    }
    }

      LucD

      Nice solution, perhaps the catch could be more specific (declare the Exception explicitely on the Catch).

    Joaquim Castro

    Ok! i used this instead and it works 🙂

    Process
    {
    $Name | %{
    $vm = Get-VM -Name $_
    $cd = Get-CDDrive -VM $vm
    $job = Start-Job -Name Check-CDQuestion -ScriptBlock $cdQuestion -ArgumentList $_,$Server
    $vm | Dismount-Tools
    }
    }

    Joaquim Castro

    Hey LucD,

    First thank you for sharing your know how 🙂
    Your examples helped me tons of times!

    This Prompt issue is really annoying; I’ve tried your script and it unmounts the VMware tools installer CD (my linux test vm started to complain that the CD is accessible).
    However when I right click the VM to mount the tools again, it still says “End VMware Tools”.
    I also get the Vmware tools instalation warning when i try to edit the VM…

    and to think this should be as simple as dismount-tools -confirm:$true..

    Steve Schofield

    Ironically, I have a script that runs weekly to disconnect cd-roms. The challenge I ran into where I’ve yet to find an answer is

    “Where do I find a log that shows what permissions are needed to execute a tasks”. In my case, it was through trial and error.
    Is there a log somewhere (probably vCenter) showing what permissions were needed. Do I need to increase verbose logging somewhere?

    Here is the blog post showing the code I’m using.

    PowerCLI, Powershell to disconnect CD-Rom

      LucD

      Indeed, there is no easy way to find required permissions afaik.

      Could be the subject for a future post 🙂

      Jason

      if you don’t have access to disconnect a CD-ROM, you may not have permission to modify the Role you are in. But if you do, here is the privilege to be checked in Role. Virtual Machine.Interaction.Configure CD Media
      I usually run my scripts with Admin (or elevated) privileges to avoid errors.
      ~good luck!

        LucD

        Thanks for sharing that Jason.

    Clint

    You seem to have set-set-cddrive…. Double “set-” in the sample usage.

      LucD

      Thanks Clint, nice catch 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Buy the Book