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 192.168.0.6 -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 = "192.168.0.6"
$nic.ip.subnetMask = "255.255.255.0"
$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 192.168.0.6 -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] = "192.168.0.1"
$config.searchDomain = New-Object System.String[] (1)
$config.searchDomain[0] = "virtu-al.local"
$netSys = Get-View (Get-VMHost | Get-View).ConfigManager.networkSystem
$netSys.UpdateDnsConfig($config)

# Remove the default VM Network
Write "Removing Default VM Network on vSwitch0"
Get-VirtualSwitch -vmhost $VMHost -Name vSwitch0 | <code>
Get-VirtualPortGroup -Name &quot;VM Network&quot; | </code>
Remove-VirtualPortGroup -Confirm:$false

# Add an NTP Server
Write &quot;Adding an NTP Server&quot;
Add-VMHostNtpServer -VMHost $VMHost -NtpServer &quot;ntp.mycompany.com&quot; | Out-Null

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

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

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

# Add iScsi settings and do a rescan
Write &quot;Enabling iSCSI&quot;
$hostView = Get-View -VIObject (Get-VMHost $VMhost)
$storageSystem = Get-View $hostView.configManager.storageSystem
$storageSystem.UpdateSoftwareInternetScsiEnabled($true)
$storageSystem.UpdateSoftwareInternetScsiEnabled($false)

Write &quot;Setting up iSCSi&quot;
$iscsiHba =(Get-VMHostHba | Where {$_.Type -eq &quot;IScsi&quot;}).Device
$iscsiServer = &quot;192.168.0.15&quot;
$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
$storageSystem.UpdateSoftwareInternetScsiEnabled($true)
# Add iSCSI Server for dynamic discovery
$storageSystem.AddInternetScsiSendTargets($iscsiHba, $target)

# Scan for iSCSI devices
Write &quot;Scanning for storage&quot;
$storageSystem.RescanHba($iscsiHba)

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

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

# Connect to our vCenter server
Write &quot;Connecting to vCenter&quot;
Connect-VIServer virtuvc.virtu-al.local

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

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

7 thoughts on “London VMUG – My Presentation”

  1. 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….

    Thanks!

  2. 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 = "192.168.0.6"
    $octets = $address.split('.')
    $DNSWMIA.CreateInstanceFromPropertydata($DNSSRV, $zone, $name, $class, $ttl, $address) | Out-Null
    $DNSWMIPTR.CreateInstanceFromPropertydata($DNSSRV, $octets[0]+".in-addr.arpa.", $octets[3]+"."+$octets[2]+"."+$octets[1]+"."+$octets[0]+".in-addr.arpa", $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 = "192.168.0.6"
    $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]+".in-addr.arpa." # Reverse Zone name
    $DNSWMI.CreateInstanceFromTextRepresentation($DNSSRV, $ReverseZone, $ReverseRR) | Out-Null

    Keep up the good work!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.