Attaching an installation CD to a VM using Power CLI (VMware)

Posted in software by Christopher R. Wirz on Thu Feb 08 2018

PowerCLI is VMware's client automation library for Windows PowerShell. When testing a recent software build, a VM should begin at a known state (provided by a snapshot) and use the latest installation materials. Installing the files onto a VM can be accomplished over a network share, but if the VM is not part of the network domain, an attached CD drive must be used. Using VMware, the contents of a CD can be mounted in ISO format, and this requires a small amount of Power CLI scripting to accomplish this.

Note: In this article, all commands must be issued in PowerShell, not from the command prompt. It is also assumed that PowerShellGet is installed.

Using PowerShellGet from the PowerShell prompt, PowerCLI can be installed as follows:

	Install-Module -Name VMware.PowerCLI -RequiredVersion 

Now that VMware.PowerCLI is installed, the parameters can be set and the local ISO can be mounted using the following commands:

$viServer = ''
$viServer_username = 'username'
$viServer_password = 'password'
$vm_name = "myVM"
$vm_username = "vmusername"
$vm_password = "vmpassword"
$install_iso_path = "C:\MyDisc.iso"
$install_script_on_iso = "\install.bat"
$snapshot_name = 'test'

# Do not participate in giving feedback
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -Confirm:$false

# Login
$server = Connect-VIServer -Server $viServer -User $viServer_username -Password $viServer_password -Verbose

# Get the VM
$vm = Get-VM -Name $vm_name 

# Only modify the VM if it is Powered Off (no activity / not in use)
if ($vm.PowerState -eq "PoweredOff") {  #Similar to saying (-not ($vm.PowerState -eq "PoweredOn"))

    # Get the latest snapshot (assumed working)
    $snap = Get-Snapshot -VM $vm | Sort-Object -Property Created -Descending | Select -First 1

    # Revert to snapshot 
    # this may return "Current license or ESXi version prohibits execution of the requested operation"
    Set-VM -VM $vm -SnapShot $snap -Confirm:$false
	# Mount an ISO image
	$cd = New-CDDrive -VM $vm -ISOPath $install_iso_path -Confirm:$false
	# if it fails because it's mounted, this will get it
	$cd = Get-CDDrive -VM $vm
	# Start the vm
    # this may return "Current license or ESXi version prohibits execution of the requested operation"
    Start-VM -VM $vm
	# Optionally, create a snapshot
	# $vm | New-Snapshot -Name $snapshot_name  -Description 'Sample snapshot description' -Quiesce -Memory
	# Get the path on the CD drive
	$driveLetter = ((Invoke-VMScript -VM $vm -ScriptText '(Get-WmiObject Win32_CDROMDrive).Drive' -GuestUser $vm_username -GuestPassword $vm_password -ScriptType Powershell).ScriptOutput -split '\n')[0].Trim()

	# Run the install
	$call  = 'call ' + $driveLetter + $install_script_on_iso
	Invoke-VMScript -VM $vm -ScriptText $call  -GuestUser $vm_username -GuestPassword $vm_password -ScriptType Bat

	# Remove the CD drive
	Stop-VM -VM $vm
	Set-CDDrive -CD $cd -NoMedia -Confirm:$false
	# and rollback to a previous snapshot
	$snap = Get-Snapshot -vm $vm -name $snapshot_name 
    Set-VM -VM $vm -SnapShot $snap -Confirm:$false

# Logout of the server
Disconnect-VIServer -Server $server -Force -Confirm:$false # Disconnect-VIServer -Server * -Force -Confirm:$false

Looking for a job?