<?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>harleystagner.com &#187; PowerShell</title>
	<atom:link href="http://www.harleystagner.com/category/powershell/feed" rel="self" type="application/rss+xml" />
	<link>http://www.harleystagner.com</link>
	<description></description>
	<lastBuildDate>Fri, 18 Jun 2010 01:39:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Better ESX Maintenance Mode with PowerShell</title>
		<link>http://www.harleystagner.com/powershell/better-esx-maintenance-mode-with-powershell.php</link>
		<comments>http://www.harleystagner.com/powershell/better-esx-maintenance-mode-with-powershell.php#comments</comments>
		<pubDate>Fri, 09 Oct 2009 19:32:09 +0000</pubDate>
		<dc:creator>Harley Stagner</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[VMware]]></category>

		<guid isPermaLink="false">http://www.harleystagner.com/?p=163</guid>
		<description><![CDATA[Maintenance mode on ESX hosts is great. As long as DRS is set to automatic and VMotion is working, all of the VM&#8217;s will be evacuated from the host. However, I always seem to run into one VM that still has a CDROM drive connected to it. This causes the VMotion to stop until the [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Maintenance mode on ESX hosts is great. As long as DRS is set to automatic and VMotion is working, all of the VM&#8217;s will be evacuated from the host. However, I always seem to run into one VM that still has a CDROM drive connected to it. This causes the VMotion to stop until the CDROM drive is disconnected.</p>
<p>Since, this happened quite frequently, I decided to do maintenance mode through PowerShell to automate the process <img src='http://www.harleystagner.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  . The below script will:</p>
<ul>
<li>Search for any VM&#8217;s that have CDROM&#8217;s connected</li>
<li>Disconnect the CDROM&#8217;s from said VM&#8217;s</li>
<li>VMotion the VM&#8217;s to the remaining ESX hosts (there is no way that I know of to automatically VMotion the VM&#8217;s by just putting the host into maintenance mode using PowerShell)</li>
<li>Put the target host into maintenance mode</li>
</ul>
<p>A special thanks goes to Steve Beaver. His post &#8220;<a href="http://www.thevirtualblackhole.com/virtual-tech/working-with-maintenance-mode-in-powershell">Working with Maintenance Mode in PowerShell</a>&#8221; helped me figure out the automatic VMotion to the remaining hosts.</p>
<p>So, here it is:</p>
<pre class="brush: powershell;"># Name: esx-maintenance-prep.ps1
# Purpose: Disconnects all CDROM Drives on VM's from a chosen host. It will VMotion the
#          VM's on that host. Then, it will put the host into maintenance mode.
#
# Created: 09/11/2009
# Author: Harley Stagner
# Version: 1
#
# TODO:
#  -Wrap work into functions
#  -Use if statement to account for VM's that are powered off
################################################################

#Connect to your vCenter Server
Connect-VIServer

Write-Host &quot;Choose your server from the list below:&quot;

# Set up variables for the Host Selection
$colESX = Get-VMHost
$selectionNumber = 0

#HashTable
$colHostSelection = @{} 

#Array
$colSelection = @() 

# Create the menu choices for the hosts
$colESX | sort Name | ForEach-Object {

	$selectionNumber = $selectionNumber + 1

	$colHostSelection.Add(&quot;$selectionNumber&quot;, &quot;$_&quot;)

	Write-Host &quot;$selectionNumber - $_&quot;
}

# Needed to match the option for the switch statement
$colHostSelection.Keys | ForEach-Object{
$colSelection += $_
}

# Have the VM Admin make the host selection
$VMHostSelection = Read-Host &quot;Please choose an option between&quot; $colSelection[0] &quot;and&quot; $colSelection[-1]
$VMHost = $colHostSelection[$VMHostSelection]

# Switch statement to get the appropriate host to put into maintenance mode.
switch ($colSelection[0]..$colSelection[-1]){

	{$_ -eq $VMHostSelection} {

# Disconnect the CDRom Drives from the VM's.
		Get-VMHost $VMHost | Get-VM | Get-CDDrive | Set-CDDrive -Connected $false -Confirm:$false

			ForEach ($ESX in $colESX){

				If ($ESX -cnotmatch $VMHost){

# vMotion the VM's to the other hosts in the cluster.
          			Get-VMHost -Name $VMHost | Get-VM | Move-VM -Destination (Get-VMHost $ESX)
    			}
			}

# Put the chosen host into Maintenance Mode.
	Get-VMHost -Name $VMHost | Set-VMHost -State Maintenance | ForEach-Object { &quot;Entering maintenance mode on '&quot; + $VMHost + &quot;'&quot;} ; continue
	}
}

Disconnect-VIServer -Confirm:$False

#END SCRIPT#</pre>
<p>As you can see, I still have a couple items in my to do list for this script. However, it is very useable as it stands.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.harleystagner.com/powershell/better-esx-maintenance-mode-with-powershell.php/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
