Quantcast
Channel: i'm all vIRTUAL » pagefile
Viewing all articles
Browse latest Browse all 2

Keeping your VM Swapfile and Guest OS virtual memory away from everything – Part 4

$
0
0

automationIn the last part for this series I will be focusing on automating the deployment process using PowerCLI and VB script combination.

 

 

 

 

 

You are probably familiar with the fact that after deploying a Windows VM from template, the hard drive letters layout is back to default.

After deploying the template I’ve configured in part 3 with its virtual memory configuration, I get the Windows yelling at me that a temporary paging file has been created.

temporary paging file has been created

The reason is that all my drive letters layout returned to default.

drive letter layout returned to default

So, my goal is to simplify the deployment process as much as possible while taking the following objectives under consideration:

  • Deploy a VM from the W2008-Profile-Template using PowerCLI script.
  • Make sure all VMDKs are in their dedicated datastores.
  • Make sure the hard drive letters layout are still in place after the deployment is finished using a VBscript (C, P, and X)
  • Get a nice message telling me the VM is good to go.
  • Make sure the VM is in compliant to its VM storage profiles.

Disclaimer: Unfortunately, VMware is not yet to release the Profile-Driven Storage PowerCLI cmdlets so the part of assigning VM storage profile to each hard disk is still done manually.

Preparations (All scripts can be found in this ISO file)

  • I have created to following VB scripts and registry key which will handle the drive letter assignment and the message box notification. Make sure you copy the file to C:\Temp in your template.

DriveLetter.vbs

' Script that changes drive letters
' Note: Do NOT use it on SYSTEM or BOOT partition drive letters !!!

set objShell = CreateObject("WScript.Shell")
objShell.Run("regedit /s C:\Temp\MsgBox.reg")

sComputer = "."
Const HKLM = &H80000002

' from/to
If ChangeDrvLetter("E:", "X:") Then
End If

If ChangeDrvLetter("D:", "P:") Then
End If

Function ChangeDrvLetter(sSourceDrive, sTargetDrive)
 bOK = True ' Init value
 Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
 & sComputer & "\root\default:StdRegProv")

sKeyPath = "SYSTEM\MountedDevices"
 sSrc = "\DosDevices\" & UCase(sSourceDrive)

iRC = oReg.GetBinaryValue(HKLM, sKeyPath, sSrc, sValue)

If iRC = 0 Then
 sTrg = "\DosDevices\" & UCase(sTargetDrive)

iRC = oReg.SetBinaryValue(HKLM, sKeyPath, sTrg, sValue)

If iRC = 0 Then
 oReg.DeleteValue HKLM, sKeyPath, sSrc
 Else
 bOK = False
 End If
 Else
 bOK = False
 End If
 ChangeDrvLetter = bOK
End Function
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"

MsgBox.vbs

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile "C:\Temp\DriveLetter.vbs", True
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile "C:\Temp\MsgBox.reg", True

Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
r = msgbox("Virtual Machine is ready, enjoy!")

MsgBox.reg

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]
"MsgBox"="C:\\Temp\\MsgBox.vbs"
  • I’ve created a new customization specification named “Windows Server 2008 with Profiles and Scripts”. In the Run Once part I pointed the answer file to the script created earlier.

customization specification

  • For the deployment process itself I’ve created the following PowerCLI script
#Creted for imallvirtual.com
#Deploy VM From template with svMotion to a dedicated datastore
#Created on: 10/04/2013

Connect-VIServer "Enter your vCenter IP\name"
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
 Add-PSSnapin VMware.VimAutomation.Core
}
# Map Deployment Variables
 $VMName = "Name of the VM"
 $Template = "Template Name"
 $ResourcePool = "Resource Pool Name (ifany)"
 $NumCpu = 4
 $MemoryMB = 4096
 $Customization = "Name of the customization specification"
 $OS_Datastore = "OS Datastore name"
 $PageFile_vDISK = "Hard disk 2"
 $PageFile_Datastore = "PageFile Datastore name"
 $Folder = "VM folder name"

# Start the deploy and VM configurations process
New-VM -Name $VMName -OSCustomizationSpec $Customization -ResourcePool $ResourcePool -Location $Folder -Template $Template -Datastore $OS_Datastore -Confirm:$false -ea stop | Out-Null
Set-VM -VM $VMName -NumCpu $NumCpu -MemoryMB $MemoryMB -Confirm:$false

# 1. Select the VM pagefile virtual vDisk and svMotion it to the dedicated PageFile datastore
# 2. Start the VM
Get-VM -Name $VMName | Get-HardDisk | Where {$_.Name -eq $PageFile_vDISK} | `
 % {Set-HardDisk -HardDisk $_ -Datastore $PageFile_Datastore -Confirm:$false}
Start-VM $VMName -Confirm:$false

Now that we have finished with preparations, edit the PowerCLI script according to your environment variables and hit the play button.

VM Automated Deployment Process HLD

After the VM will finish its customization process you will get this cool message box and the drive letters layout will be in place.

cool message box

drive letters layout will be in place

  • Now all we have to do in order for the VM to be in compliant mode with the datastores storage profile we need to configure its VM storage profiles the same way we did in part 3 and we are done!

and we are done

The post Keeping your VM Swapfile and Guest OS virtual memory away from everything – Part 4 appeared first on i'm all vIRTUAL.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images