| | | |

How to add a CD-ROM drive and mount an ISO file via powershell to an AHV-hosted VM

4 min read

PowershellAnother powershell blog? Yeah, I guess it’s that kind of a month :). After writing my previous blog on How to add a NIC via Powershell to an AHV-hosted VM I got talking to one of our Services resources about a customer trying to run Citrix PVS with BDM. I figured I could easily modify the script to add a NIC so I could add a CD-ROM drive and mount an ISO file.

Another good usecase would be the VM prep for an automated deployment with the Microsoft Deployment Toolkit for example.

To recap (again) the following interfaces are available:

  • REST API
  • HTML5 GUI
  • CLI – ACLI & NCLI
  • Scripting interfaces (PowerShell)

The following definitions are from the Nutanix Bible as Steve Poitras already explained this:

REST API
The REST API exposes every capability and data point of the Prism UI and allows for orchestration or automation tools to easily drive Nutanix action.  This enables tools like Saltstack, Puppet, vRealize Operations, System Center Orchestrator, Ansible, etc. to easily create custom workflows for Nutanix. Also, this means that any third-party developer could create their own custom UI and pull in Nutanix data via REST.

HTML5 GUI (Prism UI)
The HTML5 UI is a key part to Prism to provide a simple, easy to use management interface.  However, another core ability are the APIs which are available for automation.  All functionality exposed through the Prism UI is also exposed through a full set of REST APIs to allow for the ability to programmatically interface with the Nutanix platform.  This allow customers and partners to enable automation, 3rd-party tools, or even create their own UI.

CLI – ACLI & NCLI
The Acropolis CLI (ACLI) is the CLI for managing the Acropolis portion of the Nutanix product.  These capabilities were enabled in releases after 4.1.2. The Nutanix CLI is the CLI for managing the Nutanix product and is more heterogeneous across hypervisors.

PowerShell
Windows PowerShell is a powerful shell (hence the name ;P) and scripting language built on the .NET framework.  It is a very simple to use language and is built to be intuitive and interactive.

As a reminder, when creating a VM via ACLI all you’d need is a few lines to create a VM, assign a nic to the VM on a specific network and do the same with a disk (create/assign) and lastly add a CD-ROM drive and mount the ISO file

vm.disk_update KBTestVM99 clone_from_adsf_file=/VDI/LiteTouchPE_x64.iso

Here’s how I managed to do it with Powershell with some error handling on the parameters (i.e. quit the script when the VM can’t be found):


# kees@nutanix.com
# @kbaggerman on Twitter
# https://blog.myvirtualvision.com
# Created on March, 2019



# Setting parameters for the connection
[CmdletBinding(SupportsShouldProcess = $False, ConfirmImpact = "None") ]
 
Param(
    # Nutanix cluster IP address
    [Parameter(Mandatory = $true)]
    [Alias('IP')] [string] $nxIP,   
    # Nutanix cluster username
    [Parameter(Mandatory = $true)]
    [Alias('User')] [string] $nxUser,
    # Nutanix cluster password
    [Parameter(Mandatory = $true)]
    [Alias('Password')] [System.Security.SecureString] $nxPassword
)
 
 Function write-log {
    <#
       .Synopsis
       Write logs for debugging purposes
       
       .Description
       This function writes logs based on the message including a time stamp for debugging purposes.
    #>
  param (
  $message,
  $sev = "INFO"
  )
  if ($sev -eq "INFO"){
    write-host "$(get-date -format "hh:mm:ss") | INFO | $message"
  } elseif ($sev -eq "WARN"){
    write-host "$(get-date -format "hh:mm:ss") | WARN | $message"
  } elseif ($sev -eq "ERROR"){
    write-host "$(get-date -format "hh:mm:ss") | ERROR | $message"
  } elseif ($sev -eq "CHAPTER"){
    write-host "`n`n### $message`n`n"
  }
} 


# Adding PS cmdlets
$loadedsnapins=(Get-PSSnapin -Registered | Select-Object name).name
if(!($loadedsnapins.Contains("NutanixCmdletsPSSnapin"))){
   Add-PSSnapin -Name NutanixCmdletsPSSnapin 
}

if ($null -eq (Get-PSSnapin -Name NutanixCmdletsPSSnapin -ErrorAction SilentlyContinue))
{
    write-log -message "Nutanix CMDlets are not loaded, aborting the script"
    break
}

 
# Connecting to the Nutanix Cluster
$nxServerObj = Connect-NTNXCluster -Server $nxIP -UserName $nxUser -Password $nxPassword -AcceptInvalidSSLCerts

if ($null -eq (get-ntnxclusterinfo))
{
    write-log -message "Cluster connection isn't available, abborting the script"
    break
}

## VM Creation
# Setting Variables
$VMPrefix = Read-Host -Prompt 'Input your VM Prefix including the wildcard (*)'
$ISO = Read-Host -Prompt 'Input the name of the ISO file'



# Searching for the VM defined
write-log -message "Searching for VMs with the prefix $VMPrefix"
$vminfo = Get-NTNXVM | Where-Object {$_.vmName -like $VMPrefix}

# Looping thru all VMs starting with the prefix and adding a disk
Foreach ($vm in $vminfo) {
        Try
        {
        $vmId = ($vm.vmid.split(":"))[2]
        write-log -message "VM found matching the naming: $($vm.vmName) and VM ID $($vmID)"
            ## Disk Creation
            Try
            {
               # Creating the Disk
                # Definition of the used ISO
                $diskCloneSpec = New-NTNXObject -Name VMDiskSpecCloneDTO
                $ISOImage = (Get-NTNXImage | Where-Object {$_.name -eq $ISO})
                $diskCloneSpec.vmDiskUuid = $ISOImage.vmDiskId
                # Setup the new ISO
                $vmISODisk = New-NTNXObject -Name VMDiskDTO
                # Configuring the drive to be a CD-ROM, passing along the specs
                $vmISODisk.isCdrom = $true
                $vmISODisk.vmDiskClone = $diskCloneSpec


            # Adding the CD-ROM and ISO to the VM
            Add-NTNXVMDisk -Vmid $vmId -Disks $vmISODisk | out-null

            write-log -message "Adding a cd-rom drive to $($vm.vmName)"
            }
            Catch
            {
            write-log -message "Failed to add a cd-rom drive to $vm.vmName"
            }
        }
        Catch 
        {
        write-log -message "Could not find a VM with the name $($vm.vmName)"
        }

}

 Disconnect-NTNXCluster *
 write-log -message "Disconnecting from the cluster"
Running the script
The following two tabs change content below.

Kees Baggerman

Kees Baggerman is Senior Technical Director — Performance & Solutions Engineering R&D at Nutanix, where he leads a global team responsible for defining how enterprise applications are delivered on the Nutanix platform. A former Citrix Technology Professional and NVIDIA Enterprise Platform Advisor, he has spent 15+ years driving EUC strategy and technical direction across architecture, product, and customer success. He has been writing here since 2011 — sharing what he learns at the intersection of platform engineering and enterprise IT.
Kees Baggerman

Kees Baggerman

Senior Technical Director at Nutanix - Former Citrix CTP - NVIDIA Enterprise Platform Advisor - 15+ years in EUC

Kees Baggerman is Senior Technical Director — Performance & Solutions Engineering R&D at Nutanix, where he leads a global team responsible for defining how enterprise applications are delivered on the Nutanix platform. A former Citrix Technology Professional and NVIDIA Enterprise Platform Advisor, he has spent 15+ years driving EUC strategy and technical direction across architecture, product, and customer success. He has been writing here since 2011 — sharing what he learns at the intersection of platform engineering and enterprise IT.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

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