London VMUG – My Presentation

Yesterday was a fantastic VMware user group, definitely my favourite one so far, lots of great content from some fantastic people and some real rockstars (I think that is the 2010 word for Guru’s) like Mike Laverick, Carter Shanklin (Carter USM), Stuart Radnidge and many many more.

I was privileged to open the show with a PowerCLI session, this is a pre-show session so wasn’t really part of the main VMUG, as such I was not expecting such a large crowd, if you came to the session then thanks very much, i thing there must have been around 40-45 people in there and I had a great time presenting this one.

We had a great mix of beginners to Pro’s and some great conversations about PowerCLI and what we could do to take the ESX install to the next level, one such example is in my script where we add an A host record to the DNS server as part of our deployment.

If you weren’t there or you would just like to re-live the presentation then please see below:

At the end of the slideshow there was also a pre-recorded video of the script in action which can be viewed here:

Please note that the code is not optimised for deployment, the code in this presentation was often put there to make a point and to show people from the beginners to the Pro’s how to deploy ESX.  If I was going to write this properly I would change the code a fair bit.

And of course I couldn’t possibly let the presentation end without paying Carter USM back for his amusing slideshow, thanks for that Carter.

Script Code:

connect-viserver -User root -Password ""
$VMHost = ($DefaultVIServer).Name

# Set the password
Write "Setting the Root Password"
Set-VMHostAccount -UserAccount root -password "T3sting123!" | Out-Null

# Set the Host IP Address
Write "Setting Host IP Address"
$device = "vmk0"
$nic = New-Object VMware.Vim.HostVirtualNicSpec
$nic.ip = New-Object VMware.Vim.HostIpConfig
$nic.ip.dhcp = $false
$nic.ip.ipAddress = ""
$nic.ip.subnetMask = ""
$nic.portgroup = "Management Network"
$netSys = Get-View (Get-VMHost | Get-View).ConfigManager.networkSystem
$netSys.UpdateVirtualNic($device, $nic)

# Disconnect from our host after IP change
Write "Disconnecting from Host"
$DefaultVIServer | Disconnect-VIServer -Confirm:$false

# Reconnect after the IP change
connect-viserver -User root -Password ""
$VMHost = ($DefaultVIServer).Name

# Rename the host to our given hostname
Write "Renaming Host"
$config = New-Object VMware.Vim.HostDnsConfig
$config.dhcp = $false
$config.hostName = "VIRTUESX4"
$config.domainName = "virtu-al.local"
$config.address = New-Object System.String[] (1)
$config.address[0] = ""
$config.searchDomain = New-Object System.String[] (1)
$config.searchDomain[0] = "virtu-al.local"
$netSys = Get-View (Get-VMHost | Get-View).ConfigManager.networkSystem

# Remove the default VM Network
Write "Removing Default VM Network on vSwitch0"
Get-VirtualSwitch -vmhost $VMHost -Name vSwitch0 | `
Get-VirtualPortGroup -Name "VM Network" | `
Remove-VirtualPortGroup -Confirm:$false

# Add an NTP Server
Write "Adding an NTP Server"
Add-VMHostNtpServer -VMHost $VMHost -NtpServer "" | Out-Null

# Add our standard vSwitches
Write "Creating vSwitch1"
$vSwitch1 = New-VirtualSwitch -Name "vSwitch1" -Nic "vmnic1"
$vSwitch2 = New-VirtualSwitch -Name "vSwitch2" -Nic "vmnic2"
$vSwitch3 = New-VirtualSwitch -Name "vSwitch3"

# Add our standard Portgroups
Write "Creating PortGroups"
$vSwitch1 | New-VirtualPortGroup -Name "192.168.0.x" | Out-Null
$vSwitch2 | New-VirtualPortGroup -Name "10.0.1.x" -VLanId 1001 | Out-Null
$vSwitch2 | New-VirtualPortGroup -Name "10.0.2.x" -VLanId 1002 | Out-Null
$vSwitch3 | New-VirtualPortGroup -Name "172.16.1.x" -VLanId 1601 | Out-Null

# Add a VMotion Portgroup
Write "Creating VMotion PG"
$VMotionIP = ""
$VMotionSubnet = ""
New-VMHostNetworkAdapter -PortGroup "VMotion" -VirtualSwitch "vSwitch1" -IP $VMotionIP -SubnetMask $VMotionSubnet -VMotionEnabled:$true | Out-Null

# Add iScsi settings and do a rescan
Write "Enabling iSCSI"
$hostView = Get-View -VIObject (Get-VMHost $VMhost)
$storageSystem = Get-View $hostView.configManager.storageSystem

Write "Setting up iSCSi"
$iscsiHba =(Get-VMHostHba | Where {$_.Type -eq "IScsi"}).Device
$iscsiServer = ""
$iscsiPort = 3260
$target = New-Object VMware.Vim.HostInternetScsiHbaSendTarget
$target.address = $iscsiServer
$target.port = $iscsiPort
$hostView = Get-View -VIObject (Get-VMHost $VMhost)
$storageSystem = Get-View $hostView.configManager.storageSystem
# Enable software iSCSI controller
# Add iSCSI Server for dynamic discovery
$storageSystem.AddInternetScsiSendTargets($iscsiHba, $target)

# Scan for iSCSI devices
Write "Scanning for storage"

# Add A DNS Entry for our server
$DNSSRV = "VIRTUDC.Virtu-al.local"
$DNSWMI = [wmiclass]"\\$DNSSRV\root\MicrosoftDNS:MicrosoftDNS_AType"
$zone = "virtu-al.local"
$name = "VIRTUESX4.virtu-al.local"
$class = 1
$ttl = 3600
$address = ""
$DNSWMI.CreateInstanceFromPropertydata($DNSSRV, $zone, $name, $class, $ttl, $address) | Out-Null

# Disconnect from our host
Write "Disconnecting from Host"
$DefaultVIServer | Disconnect-VIServer -Confirm:$false

# Connect to our vCenter server
Write "Connecting to vCenter"
Connect-VIServer virtuvc.virtu-al.local

# Add our host to our vCenter Server under a Test Cluster
Write "Adding New host to VCenter"
Add-VMHost virtuesx4.virtu-al.local -Location (Get-Cluster Test) -User root -Password "T3sting123!" -Force | Out-Null

# Vmotion a VM to our new host to make sure all is fine
Write "Testing Vmotion"
#Get-VM "VIRTUAPP01" | Move-VM -Destination (Get-VMHost virtuesx4.virtu-al.local)

7 thoughts on “London VMUG – My Presentation

  1. Josh Townsend

    Kind of an odd question: I was referencing your code above for renaming a host ($config = New-Object VMware.Vim.HostDnsConfig), but rather than renaming a host, I was changing the DNS server values (actually trying to set the DNS servers to null values). HostName and DomainName are required by UpdateDNSConfig, but I don’t want to change them. Any way to change other values while keeping existing HostName and DomainName the same? I tried to set them to variables ($hostname = Get-VMHost | Get-VMHostNetwork | select hostname), but got a MethodInvocationException.

    I would use Set-VMHostNetwork to set DNSAddress (Hostname/DomainName are not required by Set-VMHostNetwork), but Set-VMHostNetwork won’t let me pass null or invalid IP address values….


  2. Pingback: Virtu-Al | Virtually everything is poshable

  3. Nigel Boulton

    Excellent presentation, and a great script, thanks!

    I wanted to have it create a reverse DNS (PTR) record as well, so I did the following mod:

    # Add A DNS Entry for our server
    $DNSSRV = "VIRTUDC.Virtu-al.local"
    $DNSWMIA = [wmiclass]"\\$DNSSRV\root\MicrosoftDNS:MicrosoftDNS_AType"
    $DNSWMIPTR = [wmiclass]"\\$DNSSRV\root\MicrosoftDNS:MicrosoftDNS_PTRType"
    $zone = "virtu-al.local"
    $name = "VIRTUESX4.virtu-al.local"
    $class = 1
    $ttl = 3600
    $address = ""
    $octets = $address.split('.')
    $DNSWMIA.CreateInstanceFromPropertydata($DNSSRV, $zone, $name, $class, $ttl, $address) | Out-Null
    $DNSWMIPTR.CreateInstanceFromPropertydata($DNSSRV, $octets[0]+"", $octets[3]+"."+$octets[2]+"."+$octets[1]+"."+$octets[0]+"", $class, $ttl, $name) | Out-Null

    However, this doesn’t feel very tidy (and you need an extra WMI object), so I prefer to use CreateInstanceFromTextRepresentation to create the Resource Records, as shown below:

    # Add A DNS Entry for our server
    $DNSSRV = "VIRTUDC.Virtu-al.local"
    $DNSWMI = [wmiclass]"\\$DNSSRV\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord"
    $zone = "virtu-al.local"
    $name = "VIRTUESX4.virtu-al.local"
    $class = 1
    $ttl = 3600
    $address = ""
    $octets = $address.split('.')
    $FwdRR = $name + " IN A $address" # Forward Resource Record (A)
    $DNSWMI.CreateInstanceFromTextRepresentation($DNSSRV, $zone, $FwdRR) | Out-Null
    $ReverseRR = $octets[3]+"."+$octets[2]+"."+$octets[1]+" IN PTR $name" # Reverse Resource Record (PTR)
    $ReverseZone = $octets[0]+"" # Reverse Zone name
    $DNSWMI.CreateInstanceFromTextRepresentation($DNSSRV, $ReverseZone, $ReverseRR) | Out-Null

    Keep up the good work!

  4. Pingback: My first VMUG « Virtualised Reality

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.