Question

Creating VM with Guest Customization script using Powershell cmdlets v2.0

  • 18 June 2022
  • 0 replies
  • 694 views

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'

 


0 replies

Be the first to reply!

Reply