A. Mikkelsen

VMware ESX scripts, commands, tools and other nice to know things that will make your virtualization days easier!!!!

Ever wanted to turn any PC into a View client?

Then take a look at this webbased View Client ISO creator – TinyCore Builder for VMware View (http://repurpose.vmwaredotg.com/).

  • Create ISO
  • Burn ISO

If you want to boot from USB, I have great success using UNetbootin (http://unetbootin.sourceforge.net/), to convert ISO images to USB.

A. Mikkelsen

When connecting some storage systems to vSphere, vSphere sometime doesn’t select the most optimal multipathing policy.

I came across the above problem when adding an EMC VPLEX storage system to a  large vSphere 4.1 installation.
vSphere selected the Fixed path multipathing policy. In smaller environments this isn’t normally a problem , but when a Fixed policy is selected in a large environment with multiple datastores. vSphere selects the first path for each datastore, this results in almost all datastores uses the same path. The result is that the path will be overloaded.

EMC’s best practices for EMC VMAX/VPLEX is to use Fixed policy with static load balancing or to use EMC PowerPath.
In our environment Fixed or PowerPath wasn’t an option as we have multiple hosts and datastores, so in close communication with EMC we chose to change the multipathing policy to Round Robin..
EMC has once now informed us that RR is only an option (not the best) if PowerPath or Fixed is not an option.

Changing the policy for a few LUN’s won’t take long, if you only have a few hosts and a few datastores, but if you have multiple hosts with multiple datastores, the task is massive.

Using Powershell and EsxCli the task is very easy and extremely fast.
Thanks to Arnim van Lieshout (http://www.van-lieshout.com/2011/01/esxcli-powercli/) for the basic script.

The below script illustrates how to set RoundRobin and not Fixed, this will be updated ASAP.
Until then take a look at this post from LucD http://communities.vmware.com/message/1774139 

<pre>function FuncMail {
 param($To, $From, $Subject, $Body, $smtpServer)
 $msg = new-object Net.Mail.MailMessage
 $smtp = new-object Net.Mail.SmtpClient($smtpServer)
 $msg.From = $From
 $msg.To.Add($To)
 $msg.Subject = $Subject
 $msg.IsBodyHtml = 1
 $msg.Body = $Body
 $smtp.Send($msg)
}

#load Vmware Module
if ((Get-PSSnapin | Where-Object { $_.Name -eq "VMware.VimAutomation.Core" }) -eq $null) { Add-PSSnapin VMware.VimAutomation.Core }

# ----------- Variables ---------------
#vCenter
$vcServer = "vcenterserver"
$vCenterUser = "vcenterusername"
$vCenterPWD = "vcenterpassword"
$DC = "*" # Use * for all DC else replace * with datacenter name
$cluster = "*" # Use * for all Clusters in DC else replace * with cluster name

# LUN settings
$LUNType = "EMC Fibre Channel Disk*"
$psp = "VMW_PSP_RR"
$satp = "VMW_SATP_INV"
$iops = 10

# ESX
$esxUser = "esxuser-root"
$esxPWD = "esxuserpassword"

# Email
$strEmailTo = "to@mail.com"
$strEmailFrom = "from@mail.com"
$strEmailSubject = "PSP info '$vcServer' "
$strEmailSMTP = "smtpserver.com"

# HTML
$strHeadHTML = "<STYLE TYPE='text/css'>"
$strHeadHTML += "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$strHeadHTML += "TH{border-width: 1px;padding: 10px;border-style: solid;border-color: black; background-color:thistle}"
$strHeadHTML += "TD{border-width: 0px;padding: 0px;padding-right: 5px;padding-left: 5px;border-style: solid;border-color: black}"
$strHeadHTML += "</STYLE>"

$strBodyHTMLStart = "<H3> Setting PSP multipath & IOPS : "
$strBodyHTMLStart += Get-Date -Format g
$strBodyHTMLStart += "</H3>"
$strBodyHTMLStart += "<TABLE><TR> <TH>vCenter</TH> <TH>Datacenter</TH> <TH>Cluster</TH> <TH>Host</TH> <TH>Device</TH> <TH>SATP</TH> <TH>Old PSP</TH> <TH>New PSP</TH> <TH>IOPS</TH> <TH>Old Paths</TH> <TH>New Paths</TH></TR>"

$strBodyHTMLinfoStart = "<H3> PSP multipath & IOPS not set : "
$strBodyHTMLinfoStart += Get-Date -Format g
$strBodyHTMLinfoStart += "</H3>"
$strBodyHTMLinfoStart += "<TABLE><TR> <TH>vCenter</TH> <TH>Datacenter</TH> <TH>Cluster</TH> <TH>Host</TH> <TH>Device</TH> <TH>SATP</TH> <TH>PSP</TH> <TH>Paths</TH></TR>"

# ---------- Logic (Don't Change) --------------------

$strBODYHTML = ""
$strBodyExcludeHTML = ""

#Connect to vCenter
Connect-VIServer $vcServer -User $vCenterUser -Password $vCenterPWD| Out-Null

#Get Datacenter Clusters
$arrDC = Get-Datacenter -Name $DC | Sort Name
if($arrDC){
 foreach($objDC in $arrDC){

 #Get Cluster list
 $arrCluster = Get-Cluster -Location $objDC -Name $cluster | Sort name
 if($arrCluster){
 foreach($objCluster in $arrCluster){

 #Connect to ESX hosts in cluster
 foreach ($esx in Get-VMHost -Location $objCluster | Sort Name) {

 Connect-VIServer $esx -User $esxUser -Password $esxPWD | Out-Null

#Retrieve the esxcli instances and loop through them
 foreach($esxcli in Get-EsxCli -Server $esx.name) {

 #Write-Host $esx.Name -BackgroundColor Red

 # Change PSP for EMC VPLEX/VMAX devices
 $arrDevice = $esxCli.nmp.device.list() | where {$_.PathSelectionPolicy -ne $psp -and $_.DeviceDisplayName -like $LUNType}
 if($arrDevice){
 foreach($myDevice in $arrDevice){
 #Write-Host "Updating $($myDevice.Device)" -ForegroundColor green
 $esxCli.nmp.device.setpolicy($null, $myDevice.Device, $psp)
 $esxcli.nmp.roundrobin.setconfig(0,$myDevice.device,[long]$iops,"iops",$false)

 $newPSP = $esxCli.nmp.device.list($myDevice.device)
 $newIOPS = $esxcli.nmp.roundrobin.getconfig($myDevice.device)
 $strBODYHTML += "<TR> <TD>$($vcServer)</TD> <TD>$($objDC.Name)</TD> <TD>$($objCluster.Name)</TD> <TD>$($esx.Name)</TD> <TD>$($myDevice.Device)</TD> <TD>$($myDevice.StorageArrayType)</TD> <TD>$($myDevice.PathSelectionPolicy)</TD> <TD>$($($newPSP[0]).PathSelectionPolicy)</TD> <TD>$($newIOPS.IOOperationLimit)</TD> <TD>$($myDevice.WorkingPaths)</TD> <TD>$($($newPSP[0]).WorkingPaths)</TD></TR>"
 }
 }

 # Changes not set on
 $arrInfoDevice = $esxCli.nmp.device.list() | where {$_.PathSelectionPolicy -ne $psp -and $_.DeviceDisplayName -like $LUNType}
 if($arrInfoDevice){
 foreach($myInfoDevice in $arrInfoDevice){
 $strBODYHTMLinfo += "<TR> <TD>$($vcServer)</TD> <TD>$($objDC.Name)</TD> <TD>$($objCluster.Name)</TD> <TD>$($esx.Name)</TD> <TD>$($myInfoDevice.Device)</TD> <TD>$($myInfoDevice.StorageArrayType)</TD> <TD>$($myInfoDevice.PathSelectionPolicy)</TD> <TD>$($myInfoDevice.WorkingPaths)</TD></TR>"
 }
 }

 #Change the default PSP for my SATP
 $esxcli.nmp.satp.setdefaultpsp($psp,$satp) | Out-Null
 }
 Disconnect-VIServer $esx.name -Confirm:$false
 }
 }
 }
 }
}
#Disconnect from vCenter
Disconnect-VIServer $vcServer -Confirm:$false | Out-Null

$strBodyHTMLEnd = "</TABLE>"
$strBodyHTMLinfoEnd = "</TABLE>"

# Collect the HTML
$strHTML = "<HTML><HEAD>"
$strHTML += $strHeadHTML
$strHTML += "</HEAD><BODY>"
$strHTML += $strBodyHTMLStart
$strHTML += $strBODYHTML
$strHTML += $strBodyHTMLEnd
$strHTML += $strBodyHTMLinfoStart
$strHTML += $strBODYHTMLinfo
$strHTML += $strBodyHTMLinfoEnd
$strHTML += "</BODY></HTML>"

# Email the collected data
FuncMail -To $strEmailTo -From $strEmailFrom -Subject $strEmailSubject -Body $strHTML -smtpServer $strEmailSMTP

Script explained

Line 20-24:
Add you vCenter server name and the logon credentionls.

Line 27-30:
LUNTYPE, is the storage type you want to change PSP and SATP for (you can set this to *, if you want you set the PSP and SATP for all datastores.
PSP, is the multipathing policy you want to change to.
SATP, is the storage array type you want to change the default to.
IOPS, is the number of IO’s to be send, before switching to the next path. vSphere default is 1000, EMC recommends 1, but I found 10 to work for me.
Stephen Foskett has explained what is PSP and SATP in plain English  http://blog.fosketts.net/2011/06/06/vmware-esx-vsphere-satp-psp-support-matrix/.

Line 33-34:
Change the user to a user with root privileges.
Change the password to correspond with the user.

Line 37-40:
Change the info to receive an report of what has been changed.

Line 92-93:
Sets the PSP and IOPS for each datastore.

Line 95-96:
Retrieves the new settings (for verification) for  each datastore.

Line 101-107:
Generates a list of datastores, where the PSP setting wasn’t able to be set.
Run the script again to set them.

Line 111:
Sets the hosts default datastore PSP and SATP.
If your hosts connect to multiple different storage systems, I wouldn’t recommend setting this, but it’s to you.

If you want to see an output on the screen of the progress of the script, uncomment the lines 85, 91

Download the full RoundRobin script here.

An exampel of what a report can look like:

Post is updated after input from Josh Coen, www.valcolabs.com.

Yesterday I was supposed to receive last months billing reports from our vCloud Director environment.
But for some reason this didn’t happen :-(

I tried to logon to the VCCB web front end, but that wasn’t possible – the page just timed out.
Then I checked the VCCB  services and all was running.
Just to be sure I rebooted the server, with no luck.

Then I started to be scared…..

I checked the database login credentials, and the were correct.
So what happend?

After a quick google  I found a community thread about changing the IP for the back end database, and from this I located the config files containing the database connection information.

  1. Installation_Directory\apache-tomcat-6.0.18\webapps\vCenter-CB\WEB-INF\classes\hibernate.cfg.xml
  2. Installation_Directory\DataCollector-Embedded\classes\hibernate.cfg.xml

Locate the line

<property name="connection.url">jdbc:sqlserver://10.0.0.100;databaseName=vcenter_chargeback;integratedSecurity=false</property>

Here you can change the connection string as you see fit.
The connection string is based on Microsoft JDBC, so you are able to change it to fit you needs, choosing from all JDBC parameters. http://support.microsoft.com/kb/313100

I solved my problem by adding the port number of the MSSQL server to the connection string.

10.0.0.100:14331

I also found that you need to update 2 other config files.

  1. Installation_Directory\Config.xml
  2. Installation_Directory\DataCollector-Embedded\Config.xml

Locate the line

<hostport>10.0.0.100</hostport>

Here you just need to update the IP and/or port number.
I needed to add the portnumber.

<hostport>10.0.0.100:14331</hostport>

As you can see in the config.xml you are also able to change other parameters like

  • Databasename
  • username
  • Password
    This is encrypted so you need to update this through the supplied VCCB tool.
    Start -> All Programs -> VMware -> VMware vCenter Chargeback -> vCenter Chargeback Tools -> Update vCenter Chargeback Database Credentials

When you have updated all 4 configuration files, restart all services or restart the server.

Wait a few minutes and then login to the VCCB web front end….

All is now working :-)

In April VMware released their hardening guide for vSphere 4.1 (http://communities.vmware.com/docs/DOC-15413) , now the have also released a free tool to check your vSphere installations against their hardening guidelines.

The tool is called “VMware Compliance Checker for vSphere” – http://www.vmware.com/products/datacenter-virtualization/vsphere-compliance-checker/overview.html

If you haven’t read or read about the must have PowerCLI book “VMware vSphere PowerCLI Reference: Automating vSphere Administration“, by Luc Dekens, Alan Renouf, Glen Sizemore, Arnim van Lieshout and Jonathan Medd, then you need to check it out.

The book will show you how to automate your VMware infrastructure from vCenter to VM’s.

  • Automate installations
  • Create and configure VM’s
  • Secure your environment
  • Create reports

and much more.

Read a few chaphers from the book or buy the book (like I did :-) ) at:
http://eu.wiley.com/WileyCDA/WileyTitle/productCd-0470890797,miniSiteCd-SYBEX,descCd-description.html

Download the PowerCLI examples from each chapter:
http://eu.wiley.com/WileyCDA/WileyTitle/productCd-0470890797,miniSiteCd-SYBEX,descCd-DOWNLOAD.html

If you have your hosts connected to a Cisco network infrastructure, you can see a hosts CDP information directly from within the VI Client.

As you can see in the exampels below it’s quite impossible to get the full CDP picture if you have many hosts with multiple NICs.
So I created a script that retrieves all CDP info from all your hosts (even across multiple vCenters) and displays it as a webpage.
Now it’s possible to search and share the information :-)

The script is build up by a few functions

  • Retrieve the vCenter servers to retrieve host from.
    $objvCenterServer = Import-Csv -Path $strvCenterFilePath -Delimiter ";" | sort vCenter
    foreach($strvCenterServer in $objvCenterServer){
    # Check if VC is uncommented
    if ((!($strvCenterServer.vCenter.Contains("#"))) -and ($strvCenterServer.vCenter.Length -gt 0)){
    # Connect to vCenter Server
     Connect-VIServer -Server $strvCenterServer.vCenter -User $strvCenterServer.UserName -Password $strvCenterPWD
    
    # Add logic
    
    # Disconnect from vCenter server
    DisConnect-VIServer -Confirm:$false
    }}
    
  • Retrieve hosts from clusters.
    $arrDC = Get-Datacenter | Sort
    foreach($objDC in $arrDC){
     $arrCluster = Get-Cluster -Location $objDC | Sort
     # Only proceed if the Cluster isn't blank
     if ($($arrCluster | Measure-Object).count -gt 0){
     foreach($Cluster in $arrCluster){
     $vmhosts = Get-VMHost -Location $Cluster | Sort Name | Where-Object {$_.State -eq "Connected"} | Get-View
     #Only proceed if any hosts in cluster
     if ($vmhosts.Count -gt 0){
     foreach ($vmhost in $vmhosts){
    
    # Add logic for each host
    
    }}}}}
    
  • Retrieve hosts NICs CDP info.
    foreach ($vmhost in $vmhosts){
     $networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem
    
     foreach($pnic in $networkSystem.NetworkConfig.Pnic | Sort Device){
     $pnicInfo = $networkSystem.QueryNetworkHint($pnic.Device)
    
     foreach($Hint in $pnicInfo){
    
     # LinkSpeed & MAC
     $record = 0
     $tmpSpeed = ""
     $tmpMAC = ""
     Do{
     If ($Hint.Device -eq $vmhost.Config.Network.Pnic[$record].Device){
     $tmpSpeed = $vmhost.Config.Network.Pnic[$record].LinkSpeed.SpeedMb
     $tmpMAC = $vmhost.Config.Network.Pnic[$record].Mac
     }
     $record ++
     }
     Until ($record -eq ($vmhost.Config.Network.Pnic.Length))
    
     # Duplex
     $tmpDuplex = ""
     if($Hint.ConnectedSwitchPort.FullDuplex -eq $true){
     $tmpDuplex = "Full"
     }
     if($Hint.ConnectedSwitchPort.FullDuplex -eq $false){
     $tmpDuplex = "Half"
     }
    
     # Status
     $tmpStatus = ""
     If (($tmpSpeed -ge 1000) -and ($tmpDuplex -eq "Full")){
     $tmpStatus = "OK"
     }else{
     If (($tmpSpeed -gt 0) -and ($tmpDuplex -eq "")){
     $tmpStatus = "CDP not working"
     }
     elseif (($tmpSpeed -gt 0) -and ($tmpDuplex -eq "Half")){
     if ($tmpStatus -ne ""){$tmpStatus += " / "}
     $tmpStatus = "Duplex config error"
     }
     elseif (($tmpSpeed -gt 0) -and ($tmpSpeed -lt 1000)){
     if ($tmpStatus -ne ""){$tmpStatus += " / "}
     $tmpStatus = "Speed config error"
     }
     else{
     $tmpStatus = "Link Down"
     }}}}
    
  • Seperate information included/excluded.
    $isExcluded = func_exclude_from_list -ExcludeHost $vmhost.Name -ExcludeVMNIC $Hint.Device -FromObj $objExcludeList
    
  • Generate html outputfile.
    Add-Content -Path $($strOutputPath + $strOutputFileName + ".TMP") -Value $strHTML
    if(Test-Path -Path $($strOutputPath + $strOutputFileName + ".htm")){
     Copy-Item -Path $($strOutputPath + $strOutputFileName + ".htm") -Destination $($strOutputPath + $strOutputFileName + $(Get-Date -uformat "%Y%m%d") + ".htm") -Force
    }
    Copy-Item -Path $($strOutputPath + $strOutputFileName + ".TMP") -Destination $($strOutputPath + $strOutputFileName + ".htm") -Force
    Remove-Item -Path $($strOutputPath + $strOutputFileName + ".TMP") -Force
    

vCenter servers are added/removed from the file “_All_vCenter_Hosts_.csv” .
A hosts NICs are moved to the exclude section, by adding it to the host exclude list “host_CDP_exclude.csv”.

The CDP information is sorted and saved in a HTML output file. If the script has been run before and an older version of the output file exist, it’s renamed and the new is saved (This supply you with a sort of history).

Get all script files here.
_All_vCenter_Hosts_.csv
host_CDP_exclude.csv
host_cdp_info_v01.ps1

————————-

For CDP information to be visible  in the VI client, CDP must also be enabled/configured on your physical Cisco switch – see example.
http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1003885

To enable/change the CDP information on a host you have several options.

  1. If you are using dvSwitches in vSphere 4.x you can enable it from GUI .

    • Connect to vCenter using VI Client
    • From Home click Networking
    • Right click your dvSwitch and select Edit Settings
    • Under the Properties tab select Advanced
    • Check Cisco Discovery Protocol
    • Set Operation to Both
    • Click OK
  2. Using ESX Command line
    http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1003885
    (exchange vSwitch1 with the name of your vSwitch)

    • Show current setting
      esxcfg-vswitch -b vSwitch1
    • Set the new status (down, listen, advertise, both)
      esxcfg-vswitch -B both vSwitch1
    • Verify new settings
      esxcfg-vswitch -b vSwitch1
  3. Using vMA
    http://spininfo.homelinux.com/news/vSphere_PowerCLI/2010/03/24/Enable_CDP_on_vSwitch_on_all_hosts

     vicfg-vswitch --server <vcenter.domain.com> -h <esxi.domain.com> -B both <vSwitch1>

To view your CDP info you also a few options
See all options here:
http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1007069

  1. GUI

    • Connect to vCenter or ESX using VI Client
    • Select a ESX host
    • Click the Configuration tab
    • Select Networking
    • Click the Info icon to the right of the vSwitch
    • A tool tip opens with CDP information for the selected physical network interface
  2. PowerShell
     Get-VMHost | Where-Object {$_.State -eq "Connected"} |
     %{Get-View $_.ID} |
     %{$esxname = $_.Name; Get-View $_.ConfigManager.NetworkSystem} |
     %{ foreach($physnic in $_.NetworkInfo.Pnic){
        $pnicInfo = $_.QueryNetworkHint($physnic.Device)
        foreach($hint in $pnicInfo){
           Write-Host $esxname $physnic.Device
           if( $hint.ConnectedSwitchPort ) {
              $hint.ConnectedSwitchPort
           }
           else {
              Write-Host "No CDP information available."; Write-Host
           }
        }
     }
    

See the full keynote from EMC Worls 2011, staring Poul Maritz – VMware CEO

http://www.youtube.com/watch?v=Ac9jQZi_lz8&feature=youtu.be&goback=%252Egde_1800113_member_55803385

After I upgraded vSphere vCenter & Client to 4.1 258902, I been getting an error in the vSphere Client.

A internal error occured in the vSphere Client. Details: Object reference not set to an instant object.

The solution is provided by VMware.
http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1033560

It’s just replacing a DLL file :-)

The error is only with version 4.1 258902 and not 4.1 Update 1.

Hey all,
Just to let you know that I haven’t forgotten about you, but the last 6 months has been hectic.

In October I changed job from Logica to NNIT.
The change has meant that I haven’t had much the time to blog. This is no excuse, but now I’m back J

Over the next weeks I’ll post some of the PowerShell scripts that I have created months ago. I hope you can use them.

A. Mikkelsen

Last year I was asked to reduce the time spend on installing and configuring our ESX hosts.  Because we weren’t using Enterprise Plus licenses, we didn’t have Host Profiles.
I came up with a simple two-step process based on the EDA appliance and a custom PowerShell script.

  • Install the host from PXE.
    Only setting the minimum configuration, so it’s as versatile as possible.

    • Disk layout
    • Network teaming
    • FQDN and IP
  • Configure the host using a custom PowerShell script.
    Based on the Datacenter and Cluster the host is to be added to.

    • Add to vCenter
    • Set COS memory
    • Enable VMotion
    • License host
    • And much more…

A lot of blog posts are available on the net, on how to setup and install an ESX host using the EDA appliance so I won’t trouble you with this.

The PowerShell script is divided into several sections, I won’t explain everyone, only the most relevant, the rest is documented in the full script.
All steps in the script are logged to a host specific log file.

  • User input – Only 3 things are asked for when running the script, the rest is stored in the scripts Static section.
    • FQDN of the host (must be configured in DNS to work)
    • The environment – what environment is the host to be placed in, like PROD, DMZ, etc.
    • The hosts VMotion IP.
  • Add the host to the right cluster.
    You must have get the Datacenter/Cluster variable prior to adding the host, else it will just be placed in the first datacenter (See script for more info).

     add-vmhost $strHost -location $strvCenterDatacenter -user $strHostUser -password $strHostUserPWD -force: $true
     
  • Set the host in maintenance mode
     Get-VMHost -Name $strHost | Set-VMHost -State maintenance
     
  • License host.
    As you can see below, you have to supply the full name of the license you are adding, not just the license key.

     $targethostMoRef = (get-VMHost $strHost | get-view).MoRef
     $si = Get-View ServiceInstance
     $LicManRef=$si.Content.LicenseManager
     $LicManView=Get-View $LicManRef
     $licassman = (Get-View $LicManView.LicenseAssignmentManager)
    
     #$licassman.UpdateAssignedLicense($targethostMoRef.value,”YOUR LIC KEY”,”vSphere4 Enterprise Plus (1-12 cores per CPU”)
     $licassman.UpdateAssignedLicense($targethostMoRef.value,”YOUR LIC KEY”,”vSphere4 Enterprise (1-6 cores per CPU”)
     
  • Set the correct time zone.
     $strTimeZone = "Europe/Copenhagen"
     $tmpHost = Get-VMHost $strHost | get-view
     $tmpDTSystem =  $tmpHost.ConfigManager.DateTimeSystem
     $tmpMoRef = Get-View  $tmpDTSystem
     $tmpDateConfig = New-Object      Vmware.Vim.HostDateTimeConfig
     $tmpDateConfig.timeZone = $strTimeZone
     $tmpMoRef.updateDateTimeConfig($tmpDateConfig)
     
  • Add NTP servers.
    Use an array of NTP servers like
    $arrNTPServer = @(“dk.pool.ntp.org”,”de.pool.ntp.org”,”us.pool.ntp.org”,”clock.cimat.ues.edu.sv”,”ntp1.gbg.netnod.se”,”ntp1.theremailer.net”)

     Add-VMHostNtpServer -VMHost $strHost -NtpServer $arrNTPServer -Confirm:$false
     

    If you want you can restart the NTP service – only do it if you don’t plan on restarting the host after configuring.

     Restart-VMHostService $ntpd -Confirm:$false
     
  • Open firewall rules
    I always open the following firewall rules so that NTP and the SSH is working

     Get-VmhostFirewallException -VMHost $strHost -Name "NTP Client" | Set-VMHostFirewallException -enabled:$true
     Get-VmhostFirewallException -VMHost $strHost -Name "SSH Client" | Set-VMHostFirewallException -enabled:$true
     [/powershel]</li>
    	<li>Set the correct DNS servers
     Use an array of DNS servers, this way you can add multiple DNS servers at      once.
     @("10.10.10.40","10.10.10.30") or @("10.10.10.40")
     1
     $strHost = @("10.10.10.40","10.10.10.30")
     Get-VMHost -Name $strHost | Get-View | %{$tmpNS = Get-View -Id $_.configManager.networkSystem
     $tmpDNS = $tmpNS.NetworkConfig.DnsConfig
     $tmpDNS.domainName = $strHostDomain
     $tmpDNS.address = $arrDNSsrv
     $tmpDNS.searchDomain = $strSearchDomain
     $tmpNS.updateDnsConfig($tmpDNS)}
     
  • Configuring network, adding network including VMotion.
    See the script for the full script.

     $vSwitch = Get-VirtualSwitch $strHost -Name vSwitch0
     #Set-VirtualSwitch -VirtualSwitch $vSwitch -Nic vmnic1
     New-VirtualPortGroup -VirtualSwitch $vSwitch -Name "VMOTION" -VLanId 0
     New-VMHostNetworkAdapter -VMHost $strHost -PortGroup "VMOTION" -VirtualSwitch $vSwitch -IP $strVMotion -SubnetMask $strSubnetMask -VMotionEnabled $true
     $strNetConfig = Get-View (Get-VMHost $strHost | Get-View).ConfigManager.NetworkSystem
     $strIPRoute = New-Object VMware.Vim.HostIpRouteConfig
     $strIPRoute.defaultGateway = $strGateway
     $strNetConfig.UpdateIpRouteConfig($strIPRoute)
     New-VirtualPortGroup -VirtualSwitch $vSwitch -Name "PROD" -VLanId 200
     New-VirtualPortGroup -VirtualSwitch $vSwitch -Name "PXE" -VLanId 0
     

    If needed you can remove the default VM network

     $vSwitch = Get-VirtualSwitch $strHost -Name vSwitch0
     $vmnetwork = Get-VirtualPortGroup -VirtualSwitch $vSwitch -Name "VM Network"
     Remove-VirtualPortGroup -VirtualPortGroup $vmnetwork -Confirm:$false}
     
  • Reboot the host J
     Get-VMHost -Name $strHost | %{Get-View $_.ID} | %{$_.RebootHost_Task($TRUE)}
     
  • If you want your script to wait for the host to be rebooted before continuing, you can do it with these two loops.
     do{
     $result = Get-VMHost $strHost
     Start-Sleep 10 # Wait 10 sec
     }
     Until($result.State -ne "Maintenance")
     # Maintenance - because host entered Maintenence mode earlier
    
     do{
     $result =  Get-VMHost $strHost
     Start-Sleep 10 # Wait 10 sec
     }
     Until($result.State -eq "Maintenance")
     
  • Finally exit the host from maintenance mode.
     Get-VMHost -Name $strHost | Set-VMHost -State connected
     

If everything is configured correctly, DRS should start migrating VM’s to the newly added host.

This script helped reducing the time spend on installing and configuring a host from 90 minutes to about 18 minutes. That’s a reduction of more than 500%, time you can use on other cool PowerShell tasks. J

By using a script to customize our hosts, we also gained two other benefits.

  1. Compliance – a host is always installed and configured the exact same way each time.
  2. Each step is documented in a host specific log file.

The full script can be downloaded here.