To sysprep properly I made an edit to the script and created a second script to execute on the VM itself to sysprep the VM. below is a simple explanation.
I edited the Ghettoclone script so that it places the computername into the VM's vmx file during the cloning process. This is done by having the script enter a line setting machine.id equal to the VM's name. I added the following line to line 175 of the script
sed -i "s/^machine.id =.*$/machine.id = \"${CLONED_VM_NAME}\"/g" "${CLONED_VM_VMX}"
Though I haven't tested it yet you should be able to add the following to line 221 of the linked clones script to achieve the same result.
sed -i 's/machine.id = "'${GOLDEN_VM_NAME}'"/machine.id = "'${FINAL_VM_NAME}'"/' ${STORAGE_PATH}/$FINAL_VM_NAME/$FINAL_VM_NAME.vmx
A second script running from within the VM pulls the computername from the VMX file, inserts it into the Sysprep.inf file and then starts Sysprep. The VM is then renamed and added to the domain. below is the script
Dim getid
'#### Set sysprep pathsSource = "C:\sysprep"
'#### perform query for sysprep folder. Do not execute if folder does not existSet objFSO = CreateObject("Scripting.FileSystemObject")If objFSO.FolderExists(sSource) Then
set WshShell = createobject("wscript.shell")
'#### set executable statement variablesvmexec = "vmwareservice.exe -cmd machine.id.get"spexec = "c:\sysprep\sysprep.exe -mini -reseal -activated -reboot -quiet"
'### query for machine name from VM's VMX file and store it as a variable set getid = WshShell.exec("%ComSpec% /c """ & vmexec & """")compname = getid.StdOut.readall
'#### write variable value into the c:\sysprep\sysprep.infConst ForReading = 1Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile("C:\sysprep\sysprep.inf", ForReading)
strText = objFile.ReadAllobjFile.ClosestrNewText = Replace(strText, "ComputerName=", "ComputerName=" & compname & "")
Set objFile = objFSO.OpenTextFile("C:\sysprep\sysprep.inf", ForWriting)objFile.WriteLine strNewTextobjFile.Close
'#### launch Sysprep.exe using edited sysprep.infWshShell.exec("%ComSpec% /c """ & spexec & """")
End If
There are a few couple of caveats
1. I had to reset the local administrator's password to blank. Sysprep hangs during the process. I think there is another solution, but I haven't found it yet.
2. the second script needs to be run on the VM when it first boots. The only way that I can find to do this is to set the script as a computer startup script in AD. The script will only run if the Sysprep folder exists on the VM and the sysprep process removes the folder after it runs. So the script can be set at the domain level.
3. The template VM must be acessible by the ESX(i) host the script is being executed on. Thus it can be done via local storage, but with multiple ESX(i) hosts you need some sort of mechanism to distribute the template such as writing a bash script to copy the template. Maybe that's another post.
4. The VMs must be manually powered on after VM cloning for sysprep to run. The script can be editted to do this automatically, but I haven't determined the best place to set the command in the ghetto clones script yet.
5. and I can't recall if I said this already. but the a folder called sysprep needs to be placed on the c: drive of the template VM with the sysprep files and the sysprep vbs.

No comments:
Post a Comment