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!

31 Comments

    Scott

    Hi Luc,
    Got a related question for you… When I attempted to unmount an ISO from a Redhat PC recently, the entire time vCenter was asking me to “Answer Question” the CPU/RAM/HDD all dropped to 0. It was acting like the system was halted, but I could still ping it. As soon as I answered the question, the system was responsive again. Do you know if this is working as intended, or perhaps has anybody else in the comments ever encountered that? Your script could be a nice workaround for scenarios like this, but it feels like a bug to me not to be able to unmount a CD in a production environment.

      LucD

      If you do not unmount the CD first from within the Guest OS this can happen.
      And yes, afaik this is a known issue.

    JDMils

    Hi LucD, I tried the code and it did not work for me. I tried the following:
    set-cddriveandanswer -Name MyVMWithACDROM

    And it just sits there and in the vCenter, the VM is waiting for a response to the dreaded “Disconnect anyway and override the lock?” question.

    I then get the timeout error:
    Set-CDDrive : 5/04/2023 8:26:46 PM Set-CDDrive The operation for the entity “MyVMWithACDROM” failed with the following message: “Connection control operation failed for disk ‘ide1:0’.”. Connection control operation failed for disk
    ‘ide1:0’.
    At C:\Users\jmilano\Documents\PowerShell_Scripts\AnswerCDROMQuestion.ps1:52 char:1
    + Set-CDDrive -CD $cd -NoMedia -Confirm:$false -ErrorAction Stop
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Set-CDDrive], GenericVmConfigFault
    + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetCDDrive

    I also noticed your sample code used to run the script shows the command:
    Set-CDDriveAndAnswer -VM LinuxBox

    However the name of the VM to disconnect the CDROM is actually passed to the Name parameter so the command should be:
    Set-CDDriveAndAnswer -Name LinuxBox

    But I guess this is a result of lots of testing and code changes along the way. Something small.

      LucD

      Thanks for reporting that typo, I changed VM to Name.

      The error you are getting might be related to KB79306
      Can you try the Solution mentioned in that KB?

    Toni Polster

    Thank you, the function works great … if you use the option ‘button.yes’.
    Unfortunately, my script breaks permanently as soon as it is executed via the task scheduler.

    If you remove the line
    “Connect-VIServer -Server $vServer.Name -Session $vServer.SessionSecret | Out-Null”
    the script will run, but the vSphere question will not be captured/answered.

    For info:
    The try/catch variant from these threads does not work for me, as no exception is thrown.

    Is there a solution that can be used via the task scheduler?

      LucD

      Just to understand, are you referring to the Windows Task Scheduler?
      And how exactly do you schedule your PS code? User account, PS command line or script, …

    Jason

    Just FYI, I had to change sleep to 3 and increase $MaxPass = 10 for our larger vSphere environment. I think our vCenter is just a little slower to prompt the question and it seemed to timed out never answering the question. Also -option “button.yes” was the only way for it to work in the latest versions of vSphere 6.7.

      LucD

      Thanks for the feedback Jason

    Manuel Perrot

    try updating script line 38:
    Set-VMQuestion -VMQuestion $question -Option “button.yes” -Confirm:$false

    instead of :
    Set-VMQuestion -VMQuestion $question -Option Yes -Confirm:$false

    It looks like format has changed

      LucD

      Thanks for that.
      I’ll check if I can update that function.

    Ray

    I know this is an old thread, but none of this seems to work with vSphere 6.7.

      LucD

      Hi Ray,
      Sorry to hear that.

      Can you be a bit more specific?
      What kind of question is pending?
      Does the script return an error?

        Ray

        In vCenter I can see the task of reconfiguring the VM start, and then in the “All Issues” tab under Monitor, I can see the issue of “CD-ROM door is locked”. The HTML5 doesn’t create a popup window like the older clients. There is an “Answer Question” button at the top of the table that once pushed will create the popup window with the “yes or no” buttons for the question.

        There is an error created…(I’ll have to paraphrase because I don’t have the ability to copy the text fully): Set-CDDrive The operation for the entity “VM Name” failed with the following message: “Connection control operation failed for disk ‘sata0:0’.”. Set-CDDriveAndAnswer.ps1:56 char 12 [Set-CDDrive], GenericVmConfigFault

        If I manually answer the question while the loop is still active, everything completes without error.

          LucD

          Thanks, I’ll have a look

            Julian Milano

            I am experiencing the same issue as Ray.

            While watching the Summary screen of the VM, I run the script and I can see the prompt on the VM’s Summry screen:

            The guest operating system has locked the CD-ROM door and is probably using the CD-ROM, which can prevent the guest from recognizing media changes. If possible, eject the CD-ROM from inside the guest before disconnecting. Disconnect anyway and override the lock?

            The script then errors with:

            Set-CDDrive : 26/08/2022 3:12:30 PM Set-CDDrive The operation for the entity “MyVM” failed with the following
            message: “Connection control operation failed for disk ‘sata0:0’.”. Connection control operation failed for disk ‘sata0:0’.
            At C:\Users\MyUserName\Documents\Powershell_Scripts\Datastore Migrations- MyVCenter\AnswerCDROMQuestion.ps1:52 char:7
            + Set-CDDrive -CD $cd -NoMedia -Confirm:$false -ErrorAction Stop
            + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            + CategoryInfo : NotSpecified: (:) [Set-CDDrive], GenericVmConfigFault
            + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCo
            re.Cmdlets.Commands.VirtualDevice.SetCDDrive

            Has anyone found a solution to this?

            vCenter 6.7U3

              Julian Milano

              I’m trying to figger out how LucD’s code works and I guess it tries to disconnect the CDROM then launches another process to answer the question?

              The reason I ask this is that when I run the command:
              Set-CDDrive -CD $cd -NoMedia -Confirm:$false

              The VM Summary screen shows the question and is waiting for an answer.

              I then opened another PowerShell command dialog, connected to the same vCenter as the VM in question, and ran the following:

              PS C:\Users\MyUsername> get-vmquestion

              Text Options Default
              —- ——- ——-
              The guest operating system has locked the CD-ROM {button.yes, button.no
              door and is probably using the CD-ROM, which can button.no}
              prevent the guest from recognizing media changes.
              If possible, eject the CD-ROM from inside the
              guest before disconnecting. Disconnect anyway and
              override the lock?

              PS C:\Users\MyUsername>

              I then ran this command and the CDROM unlocked:
              Set-VMQuestion -VMQuestion $question -Option “button.yes” -Confirm:$false

              So I guess if I could load a new process which delays by 10 seconds, then queries the question on the VM and then answers it with “button.yes” and shuts down, this would work, but I’m not sure how to code this.

              The VM I am testing with has the following properties set:
              Connected To: Host Image File
              File: [] MyImageFile.iso

                LucD

                That is a valid alternative.

                In fact, my solution does something similar, it runs code in the background that polls for the question, and then tries to answer it.
                Sometimes that background task might fail I noticed.

            Mohamed

            Hi LucD, have you found a solution for this? I have the same issue Ray describe

              LucD

              I’m afraid not yet.

    jason street

    Hi

    I found this script looking for an answer to the question problem. Great script but i found it didnt always work.
    After a bit of poking about I noticed the sleep (updated to start-sleep) command should be in the while loop.
    Thanks
    Jase

      LucD

      Thanks for noticing that Jason.
      I updated the code.

    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 *

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.