[AHV] Automation of Windows Server 2012 R2 using Powershell, AnswerFile and ACLI

Last week I shared my learnings on building Answer file. I want to share it for a reason as this post is built on it. As of now we have beginners understanding on how to create answer file which is sufficient to understand this post. Lets look at the way how to bring automation of Standard Operation Environment (SOE). My goal is to have OS Automation. At basic level, Hostname and IP Address are the bare minimum thing must be unique for the Server. For this post I’m focusing on how to automate these two parameters.

First the credits

  1. Jon (next.nutanix.com)
  2. Derek for his post on integrating VirtIO Drivers

Initially I aimed to achieve automation using powershell (Nutanix cmdlets) however information available in the help is very limited. Here is the approach, update the answer file, create ISO of answer file, upload Answer file ISO, attach OS ISO to the VM, attach Answer ISO file to VM and boot VM. For this automation I have already uploaded OS ISO.

Below is the work flow

2016-04-16_21-58-59

Except for Task of uploading ISO file and ensuring VM reads the answer file while booting, all other tasks were quite easy to achieve using powershell.

So I have to focus on script which will provide me a way to upload an answer file to AHV using cmdlets and attach it to VM. I figured out first part but second part was definitely was not coming through and at which point I reached out Jon. Jon dropped an simple hint and I realized what needs to done. Let me explain the complete script below.

First step is updating answer file. I already have standard answer file (refer previous post), all I need is ensure that it is unique per VM. Servername and IP Address are bare minimum unique attributes of any Server. So for answer file, I have provided two user inputs (highlighted line 6,7). These inputs, I’m further using it to create VM Name. After answer file is created I’m using OSCDIMG.exe tool to create answer file as shown below (highlighted line 19). This answer file, I’m copying to a location which is my webserver’s virtual directory.

Add-PSSnapin -Name NutanixCmdletsPSSnapin
Connect-NTNXCluster -server 192.168.1.190 -UserName admin -AcceptInvalidSSLCerts -ForcedConnection
$VMNamestr=read-host "Enter the name of virtual machine"
$IP=read-host "Please enter IP for this Machine"
#casting into strings
$VMName=[string]$VMNamestr
$IPaddress=[string]$IP
#FilePaths
$xmlsourcepath="C:\Automation\AHV\autounattend.xml"
$xmldestination="C:\AHVAnswer"
$answerISO="C:\ISO\$VMName.iso"
Remove-Item $xmldestination\*.xml
$xml = New-Object XML
$xml.Load($xmlsourcepath)
$xml.unattend.settings[1].component[7].Interfaces.Interface.UnicastIpAddresses.IpAddress.'#text'=$IPaddress
$xml.unattend.settings[1].component[0].ComputerName=$VMName
$xml.Save($xmlsourcepath)
Copy-Item $xmlsourcepath $xmldestination
&'C:\Program Files (x86)\Windows Kits\8.1\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg.exe' -n $xmldestination $answerISO

Line 2 I have not put password, you’re prompted for secure string password. I have to use -ForcedConnection switch as version of AHV and Nutanix cmdlet do not match. AcceptInvalidSSLCerts switch is needed when you are using self-signed certificates.

Why Am I using webserver? Well, I’m yet to figure out how to upload files into AHV using powershell. My script is using URL (highlighted line 4). Below is the snippet of the code. Here I’m intelligently putting name of ISO as name of the VM (highlighted line 6).

$imgCreateSpec = New-NTNXObject -Name ImageImportSpecDTO
$imgCreateSpec.containerId=21025
$imgCreateSpec.url="http://192.168.1.189/nutiso/$VMName.iso"
New-NTNXImage -Name $VMName -Annotation "$VMName Answer File" -ImageType ISO_IMAGE -ImageImportSpec $imgCreateSpec 
start-sleep 10 
.\plink.exe nutanix@192.168.1.190 -P 22 -pw nutanix/4u /tmp/createvm $VMName 

It is worth to know that ISO file in AHV are referred using UID and not name. This means you can have more than one ISO with same name.

Now that we have uploaded the ISO, all I need is to create VM with required specification and attach ISOs. I have kept standard specification 2vCPU, 4 GB RAM and 18 GB Storage. It can be easily changed if you wish to.  As mentioned above creating VM is quite simple in powershell but attaching ISO is not straight forward. In order to make things easier, I used plink.exe. Plink.exe will allow to run linux command remotely (shown above, highlighted line 6). I have created a script in controller VM and executed remotely. What is in the script? I must commend the Nutanix Dev team for creating such an intelligent scripting platform. You don’t have to learn anything except to know when to press Tab 🙂

/usr/local/nutanix/bin/acli vm.create $1 memory=1G num_vcpus=1
/usr/local/nutanix/bin/acli vm.nic_create $1 network=11
/usr/local/nutanix/bin/acli vm.disk_create $1 bus=scsi create_size=18G container=sss
/usr/local/nutanix/bin/acli vm.disk_create $1 cdrom=true clone_from_image=AHV
/usr/local/nutanix/bin/acli vm.disk_create $1 cdrom=true clone_from_image=$1
/usr/local/nutanix/bin/acli vm.on $1

AHV is name of ISO file I created using OSCDIMG.exe and following derek’s blog. $1 is argument. If you boot Windows2012R2 with VirtIO Tools, SCSI card, Nicard are not detected. VirtIO is vmware tools in AHV world. I found his blog post extremely helpful to integrate VirtIO driver. There is little error in his bat file batch and quite relevant if you use Index=3 or 4.

NB: I have stored this file in tmp directory, however if node is restarted this file is deleted.

Conclusion: It appears quite surprising and to most extend pleasing to have started with one intention in mind and ended up attaining completely different goal. Surprising part is I want to do mass provisioning of Windows 2012R2 in AHV but during my researching I realize it is simple as anything with Nutanix is. So I choose to focus on automation. The pleasing part is I learnt how answer file works and used that knowledge to build completely automated installation of Windows 2012R2 on AHV. Happy Learning. Below is the full powershell script for your reference. Shell script is already posted above. next post :), do same thing in VMware world.

 


Add-PSSnapin -Name NutanixCmdletsPSSnapin
#$pwd=read-host "Enter password for nutanix cluster" -AsSecureString
Connect-NTNXCluster -server 192.168.1.190 -UserName admin -AcceptInvalidSSLCerts -ForcedConnection
$VMNamestr=read-host "Enter the name of virtual machine"
$IP=read-host "Please enter IP for this Machine"
#casting into strings
$VMName=[string]$VMNamestr
$IPaddress=[string]$IP
#FilePaths
$xmlsourcepath="C:\Automation\AHV\autounattend.xml"
$xmldestination="C:\AHVAnswer"
$answerISO="C:\ISO\$VMName.iso"
Remove-Item $xmldestination\*.xml
$xml = New-Object XML
$xml.Load($xmlsourcepath)
$xml.unattend.settings[1].component[7].Interfaces.Interface.UnicastIpAddresses.IpAddress.'#text'=$IPaddress
$xml.unattend.settings[1].component[0].ComputerName=$VMName
$xml.Save($xmlsourcepath)
Copy-Item $xmlsourcepath $xmldestination
& 'C:\Program Files (x86)\Windows Kits\8.1\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg.exe' -n $xmldestination $answerISO
$imgCreateSpec = New-NTNXObject -Name ImageImportSpecDTO
$imgCreateSpec.containerId=21025
$imgCreateSpec.url="http://192.168.1.189/nutiso/$VMName.iso"
New-NTNXImage -Name $VMName -Annotation "$VMName Answer File" -ImageType ISO_IMAGE -ImageImportSpec $imgCreateSpec
start-sleep 10
.\plink.exe nutanix@192.168.1.190 -P 22 -pw nutanix/4u /tmp/createvm $VMName