Creating VM with Guest Customization script using Powershell cmdlets v2.0 | Nutanix Community
Skip to main content

I’ve tried to script out bulk creating VM from csv files using PS v2.0 cmdlets.

However, unable to spot how to code the equivalent of ‘Guest Customization’, in particular cloud-init ‘custom script’ for Linux and sysprep ‘Guided Script’ for Windows.

Is this available through Powershell?

#field inputs: ClusterName,Name,NumVcpus,MemoryMb,Description,VmDisks,network,image,ipaddress,osfamily
$VMs = Import-Csv -Path "D:\Projects\Technical\nutanix\import.csv"

#Default values
$NumCoresPerVcpu = 1
$secure_boot = $true
$taskuuid = @()
####
$VMs | ft

ForEach ($VM in $VMs)
{
##Storage Section
#~~~~~~~~~~~~~~~#
$ImageId = (Get-Image | ?{$_.name -eq $VM.image}).uuid
$vm_disk_id = (Get-Image $ImageId -IncludeVmDiskId).vm_disk_id
#Create VMDiskAddress
$cloneDiskAddress =New-NutanixObject VMDiskAddress
$cloneDiskAddress.vmdisk_uuid =$vm_disk_id
#Create VMDiskSpecClone
$vmDiskClone =New-NutanixObject VMDiskSpecClone
$vmDiskClone.disk_address =$cloneDiskAddress
#Create VMDisk Object
$vmDisk =New-NutanixObject VMDisk
$vmDisk.is_cdrom =$false
$vmDisk.vm_disk_clone =$vmDiskClone
#~~~~~~~~~~~~~~~~#

##Boot section
#~~~~~~~~~~~~~~~~#
$boot =New-NutanixObject BootConfig
$boot.uefi_boot =$true
$boot.secure_boot =$secure_boot
$boot.boot_device_type ='DISK'
$boot.disk_address =$vmDisk 0].vm_disk_clone.disk_address
$boot.disk_address.device_index = 0
$boot.disk_address.device_bus ='SCSI'

##Network section
#~~~~~~~~~~~~~~~~#
$VMNicSpec = New-NutanixObject VMNicSpec
$VMNicSpec.ip_address = $VM.ipaddress
$VMNicSpec.network_uuid = (Get-Network | ?{$_.name -in $VM.network -and $_.cluster_name -in $VM.ClusterName}).uuid
$VMNicSpec.is_connected = $true

##Guest Customization section
#~~~~~~~~~~~~~~~~#
if ($VM.osfamily -eq 'windows')
{
#?
''
}
elseif ($VM.osfamily -eq 'linux')
{
#?
''
}
else
{
Write-Warning -Message "OS Family $($VM.osfamily) not supported. skipping..."
break
}

##Create section
#~~~~~~~~~~~~~~~~#
Write-Output -Message "Creating VM $($VM.Name)"
$taskuuid += (New-VM -ClusterName ($VM.ClusterName) -Name ($VM.name) -NumVcpus ($VM.NumVcpus) -MemoryMb ($VM.MemoryMb) -Description ($VM.Description) -VmDisks $VMDisk -VmNics $VMNicSpec -NumCoresPerVcpu $NumCoresPerVcpu -BootConfig $boot).task_uuid
}

$task = $true
While ($task)
{
$task = ($taskuuid | %{Get-Task -TaskId $_ | ?{$_.percentage_complete -ne 100}} ).uuid
'Still executing tasks...'
$task
Start-Sleep -seconds 3
}
$taskuuid | %{Get-Task -TaskId $_ -IncludeCompleted}
Write-Output 'Task Completed'

 

I know this is a bit old, but I’m new to Nutanix and would like to ask if you could explain your script for the vmdisk in the csv file. Is that a disk uuid of the image or how big I want the drive to be? Also, if you have any other powershell scripts you could share or if you have completed this one I would love to see them. I’m trying to build a script to do exactly what you have here. The only other thing I have a question on would be the disk type in the script, how would I change the boot to legacy bios would it be something like this below:

##Boot section
#~~~~~~~~~~~~~~~~#
$boot =New-NutanixObject BootConfig
$boot.uefi_boot =$false
$boot.secure_boot =$legacy_bios
$boot.boot_device_type ='DISK'
$boot.disk_address =$vmDiskm0].vm_disk_clone.disk_address
$boot.disk_address.device_index = 0
$boot.disk_address.device_bus ='SCSI'

Anything else I need to change in your script to make this work? I did change the option to $secure_boot to false at the top. Thank you in advance for any help you can be.


That was a while ago, so I don’t recall exactly what all the PS Module options were.

The conclusion of this was that it was better to use the http api interface, as the Powershell module is too limited. It’s worth checking exactly what you need it to do as there are some differences (limitations) between the v2 and v3 version of the api. In particular we ended up utilising v2 for setting VM power state and getting storage container lists as there was more functionality in v2 for those purposes, at the time of writing.


Reply