<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>question Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/question/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/question/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Wed, 05 Apr 2023 10:49:57 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>question Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/question/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>Answer the question!</title>
		<link>https://www.lucd.info/2015/10/02/answer-the-question/</link>
					<comments>https://www.lucd.info/2015/10/02/answer-the-question/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Fri, 02 Oct 2015 09:48:44 +0000</pubDate>
				<category><![CDATA[Background]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Question]]></category>
		<category><![CDATA[unmount]]></category>
		<category><![CDATA[Virtual Machine]]></category>
		<category><![CDATA[background]]></category>
		<category><![CDATA[cd]]></category>
		<category><![CDATA[dvd]]></category>
		<category><![CDATA[job]]></category>
		<category><![CDATA[question]]></category>
		<category><![CDATA[VM]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=4970</guid>

					<description><![CDATA[An automation scripts that prompts you is a letdown, to say the least. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>An automation scripts that prompts you is a letdown, to say the least.</p>
<p><a href="https://www.lucd.info/2015/10/02/answer-the-question/cdlock/" rel="attachment wp-att-4978"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-4978" src="https://lucd.info/wp-content/uploads/2015/10/cdlock.png" alt="cdlock" width="608" height="186" srcset="https://www.lucd.info/wp-content/uploads/2015/10/cdlock.png 608w, https://www.lucd.info/wp-content/uploads/2015/10/cdlock-300x92.png 300w" sizes="(max-width: 608px) 100vw, 608px" /></a></p>
<p>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 <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f641.png" alt="🙁" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>I have been looking for some time to come up with a solution for this automation &#8220;issue&#8221;. And finally I came up with a working solution <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /><br /><span id="more-4970"></span></p>
<h2>The Problem</h2>
<p>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.</p>
<pre class="urvanov-syntax-highlighter-plain-tag">$vmName = 'MyVM'
$vm = Get-VM -Name $vmName
$cd = Get-CDDrive -VM $vm

Set-CDDrive -CD $cd -NoMedia -Confirm:$false</pre>
<p>The following question will appear, provided you have the vSphere Client open.</p>
<p><a href="https://www.lucd.info/2015/10/02/answer-the-question/question/" rel="attachment wp-att-4976"><img decoding="async" class="alignnone size-full wp-image-4976" src="https://lucd.info/wp-content/uploads/2015/10/question.png" alt="question" width="536" height="191" srcset="https://www.lucd.info/wp-content/uploads/2015/10/question.png 536w, https://www.lucd.info/wp-content/uploads/2015/10/question-300x107.png 300w" sizes="(max-width: 536px) 100vw, 536px" /></a></p>
<p>My initial idea was to use asynchronous events on a <a href="https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx" target="_blank" rel="noopener noreferrer">Timer</a> object to answer the question. But that didn&#8217;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&#8217;t.</p>
<p>An alternative solution that I got working, was with a background job that is started just before the unmount is attempted.</p>
<h2>The Script</h2>
<pre class="urvanov-syntax-highlighter-plain-tag">function Set-CDDriveAndAnswer
{
&lt;#
.SYNOPSIS  Unmounts a CD/DVD from a VM and answers an outstanding question
.DESCRIPTION The function will unmount a CD/DVD from a DVD.
  If the unmount causes a question, the function will answer the question.
.NOTES  Author:  Luc Dekens
.PARAMETER Name
  The DisplayName of the VM
.PARAMETER Server
  The vSphere Server on which the VM is located.
  The default is $global:defaultviserver
.EXAMPLE
  PS&gt; Set-CDDriveAndAnswer -Name MyVM -Server $global:DefaultVIServer
.EXAMPLE
  PS&gt; Get-VM | Set-CDDriveAndAnswer
#&gt;
  [CmdletBinding()]
  param(
  [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
  [string[]]$Name,
  [PSObject]$Server
  )

  Begin
  {
    $cdQuestion = {
      param($VmName,$vServer)
    
      $maxPass = 5
      $pass = 0
    
      Add-PSSnapin VMware*
      Connect-VIServer -Server $vServer.Name  -Session $vServer.SessionSecret | Out-Null
      while($pass -lt $maxPass){
        $question = Get-VM -Name $VmName | Get-VMQuestion -QuestionText "*locked the CD-ROM*"
        if($question){
          Set-VMQuestion -VMQuestion $question -Option Yes -Confirm:$false
          $pass = $maxPass + 1
        }
        $pass++
        sleep 1
       }
    }

    if(!$Server){
      $Server = $global:DefaultVIServer
    }
  }

  Process
  {
    $Name | %{
      $vm = Get-VM -Name $_
      $cd = Get-CDDrive -VM $vm
      $job = Start-Job -Name Check-CDQuestion -ScriptBlock $cdQuestion -ArgumentList $_,$Server
      Set-CDDrive -CD $cd -NoMedia -Confirm:$false -ErrorAction Stop
    }
  }
}</pre>
<h3>Annotation</h3>
<p><strong>Line 20</strong>: the function can accept the VM(s) from the pipeline</p>
<p><strong>Line 27-45</strong>: the background job that will check if the unmount has caused a question. If yes, the background job will answer the question with &#8220;<em>Yes</em>&#8220;. The background job times out after <strong>5 seconds</strong>.</p>
<p><strong>Line 34</strong>: In a background job, you can reuse an open vSphere Server connection by using the <strong>SessionSecret</strong> property.</p>
<p><strong>Line 36-38</strong>: The function looks for an outstanding &#8220;<em>locked CD rom door</em>&#8221; question for the specific VM. If such a question is found, the function will reply with &#8220;Yes&#8221;</p>
<p><strong>Line 46-48</strong>: 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 <strong>$global:defaultviserver</strong></p>
<p><strong>Line 57</strong>: The actual unmount command. It will remove the mounted CD/DVD by using the <strong>NoMedia</strong> switch.</p>
<h2>Sample Usage</h2>
<p>The usage of the function is quite simple. You can call the function for a single VM</p>
<pre class="urvanov-syntax-highlighter-plain-tag">Set-CDDriveAndAnswer -Name LinuxBox</pre>
<p>This will unmount the CD/DVD from the specific VM. The VM needs to be on the currently connected vSphere Server ($global:defaultviserver)</p>
<p>Or you can use it in a pipeline construct.</p>
<pre class="urvanov-syntax-highlighter-plain-tag">Get-VM | where{Get-CDDrive -VM $_} | Set-CDDriveAndAnswer</pre>
<p>This will find all the VMs that have a CD/DVD mounted, and will unmount the CD/DVD</p>
<p>Enjoy!</p>

]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2015/10/02/answer-the-question/feed/</wfw:commentRss>
			<slash:comments>31</slash:comments>
		
		
			</item>
	</channel>
</rss>
