<?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>5.x Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/5-x/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/5-x/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Mon, 25 Nov 2013 19:52:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.9</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>5.x Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/5-x/</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>Rename an ESXi 5.x host</title>
		<link>https://www.lucd.info/2013/10/27/rename-esxi-5-x-host/</link>
					<comments>https://www.lucd.info/2013/10/27/rename-esxi-5-x-host/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sun, 27 Oct 2013 13:27:27 +0000</pubDate>
				<category><![CDATA[ESXi]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[rename]]></category>
		<category><![CDATA[5.x]]></category>
		<category><![CDATA[PowerCLI]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=4587</guid>

					<description><![CDATA[Another one from the PowerCLI Community. User internetrush1 wanted to know how he [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Another one from the <a href="https://communities.vmware.com/community/vmtn/automationtools/powercli/content?filterID=contentstatus[published]~objecttype~objecttype[thread]" target="_blank">PowerCLI Community</a>. User <a href="https://communities.vmware.com/people/internetrush1" target="_blank">internetrush1</a> wanted to know how he could <a href="https://communities.vmware.com/message/2304203#2304203" target="_blank">rename</a> an ESXi server through PowerCLI.</p>
<p><a href="https://www.lucd.info/2013/10/27/rename-esxi-5-x-host/esxi-rename/" rel="attachment wp-att-4589"><img fetchpriority="high" decoding="async" class="alignnone  wp-image-4589" alt="esxi-rename" src="https://lucd.info/wp-content/uploads/2013/10/esxi-rename.png" width="410" height="222" srcset="https://www.lucd.info/wp-content/uploads/2013/10/esxi-rename.png 512w, https://www.lucd.info/wp-content/uploads/2013/10/esxi-rename-300x162.png 300w" sizes="(max-width: 410px) 100vw, 410px" /></a></p>
<p>In fact I was amazed there obviously is no cmdlet nor scripts available to perform this rather basic task. Luckily I had some code snippets laying around that did the job. Basically, I followed <a href="https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&amp;cmd=displayKC&amp;externalId=1010821" target="_blank">KB1010821</a> to do the ESXi 5.x host rename. But I decided it would be better to provide a real function to do the job.</p>
<p><span id="more-4587"></span></p>
<h2>The Script</h2>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">function Rename-VMHost {
 
&lt;#  
.SYNOPSIS  Renames an ESXi server   
.DESCRIPTION The function will rename an ESXi 5.x server.
  The function follows VMware KB1010821
.NOTES  Author:  Luc Dekens  
.PARAMETER VMHost
  The ESXi server to be renamed
.PARAMETER Credential
  The credentials to connect to the ESXi 5.x server
.PARAMETER NewName
  The new FQDN 
.PARAMETER Location
  By default the ESXi server will stay in its current location.
  You can specify a Datacenter or Cluster for the newly named
  ESXi 5.x server.
.PARAMETER MaintenanceMode
  Although not required by KB1010821, by default the function
  places the ESXi 5.x server in maintenance mode, before doing
  the rename.
.PARAMETER DnsCheck
  When this switch is set, the function will first check if the
  new name for the ESXi host can be resolved with DNS.
.EXAMPLE
  PS&gt; Rename-VMHost -VMHost $esx -NewName &quot;newname.lucd.info&quot;
#&gt;
 
  [CmdletBinding()] 
  param(
    [PSObject]$VMHost,
    [System.Management.Automation.PSCredential]$Credential,
    [string]$NewName,
    [PSObject]$Location,
    [Switch]$MaintenanceMode = $true,
    [Switch]$DnsCheck
  )
 
  Process {
    if($DnsCheck){
      Try {
        [System.Net.Dns]::GetHostEntry($NewName).HostName | Out-Null
      }
      Catch {
        Write-Error &quot;No DNS entry for $NewName&quot;
        return
      }
    }
    $vCenterName = $global:DefaultVIServer.Name
     
    if($VMHost -is [System.String]){
      $VMHost = Get-VMHost -Name $VMHost
    }
    if($Location){
      if($Location -is [System.String]){
        $Location = Get-Inventory -Name $Location
      }
    }
    else {
      $Location = $VMHost.Parent
    }
    if($Location -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl] -and
       $Location -isnot [VMware.VimAutomation.ViCore.Impl.V1.Inventory.DatacenterImpl]){
      Write-Error &quot;Location $($Location.Name) must be a datacenter or a cluster&quot;
      return
    }
     
    if($MaintenanceMode){
      $previousState = $VMHost.State
      Set-VMHost -VMHost $VMHost -State Maintenance | Out-Null
    }
    Set-VMHost -VMHost $VMHost -State Disconnected -Confirm:$false | 
    Remove-VMHost -Confirm:$false | Out-Null
 
    $esxServer = Connect-VIServer -Server $VMHost.Name -Credential $Credential
    $esxcli = Get-EsxCli -Server $esxServer
    $esxcli.system.hostname.set($null,$NewName,$null)
    Disconnect-VIServer -Server $esxServer -Confirm:$false
 
    Connect-VIServer -Server $vCenterName | Out-Null
    $VMHost = Add-VMHost -Name $NewName -Location $Location -Credential $Credential -Force -Confirm:$false
 
    if($MaintenanceMode){
      Set-VMHost -VMHost $VMHost -State $previousState -Confirm:$false | Out-Null
    }
  }
}</pre><p></p>
<h4><span style="font-size: large;"><strong><span style="color: #00ff00;">Annotations</span></strong></span></h4>
<p><strong>Line 40-48</strong>: The function checks if the new hostname can be resolved. If not the function returns.</p>
<p><strong>Line 51-53</strong>: Cheap <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI55/html/about_obn.html" target="_blank">Object By Name</a> (OBN) implementation</p>
<p><strong>Line 54-66</strong>: The ESXi will be added to the location specified in the $Location variable. This can be a Datacenter or a cluster. If no Location value is passed, the host is added to the same location where it was before the rename.</p>
<p><strong>Line 68-71</strong>: Although not required (see also <a href="https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&amp;cmd=displayKC&amp;externalId=1010821" target="_blank">KB1010821</a>), the host that will be renamed is placed in maintenance mode by default. The switch to maintenance mode will vMotion all VMs to other nodes in the cluster.</p>
<p><strong>Line 72-73</strong>: The ESXi host is disconnected and removed from the vCenter.</p>
<p><strong>Line 75-78</strong>: The function uses the <a href="https://www.vmware.com/support/developer/PowerCLI/PowerCLI55/html/Get-EsxCli.html" target="_blank">Get-EsxCli</a> cmdlet to be able to call the rename method. Note that the function at this point will close the vCenter connection, and connect directly to the ESXi host.</p>
<p><strong>Line 80-81</strong>: After the rename, the ESXi host is added to the vCenter in it&#8217;s old location or the location that was explicitely specified on the function call.</p>
<p><strong>Line 83-85</strong>: When required, the ESXi host is taken out of maintenance mode, and placed in the mode it was in originally.</p>
<h2>Sample Usage</h2>
<p>The use of this function is quite simple.</p>
<p>In it&#8217;s easiest form, you provide the VMHost, the new name and the credentials.</p>
<p></p><pre class="urvanov-syntax-highlighter-plain-tag">$oldName = &quot;esx1.local.test&quot;
$newName = &quot;esx2.local.test&quot;

$username = &quot;root&quot;
$pswd = &quot;password&quot;

$pswdSecure = ConvertTo-SecureString -String $pswd -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$pswdSecure

Rename-VMHost -VMHost $oldName -NewName $newName -Credential $cred</pre><p></p>
<p>The ESXi host, called <strong>esx1.local.test</strong> will be renamed to <strong>esx2.local.test</strong>. The function will test if the new name is resolvable by <strong>DNS</strong>, and it will place the ESXi host in <strong>maintenance mode</strong> before the rename is done.</p>
<p>Note that I only tested this function on <strong>ESXi 5.x</strong> hosts.</p>
<p>Enjoy !</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2013/10/27/rename-esxi-5-x-host/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
	</channel>
</rss>
