Solved

Cannot create UUID from string. New-NTNXVirtualMachine

  • 13 May 2019
  • 2 replies
  • 2496 views

I'm trying to create a vm from our CentOS75 gold image. Pallavi Akella from Nutanix created the CentOS gold image using Disk and not an ISO. When I provision vms using powershell script.
  1. update the Clone.csv where I enter the following info name,VlanID,VMRam,Vcpu,cores,VMIP,clone,Image
name,VlanID,8,2,2,VMIP,CentOS75, and save the file.


  1. Next I run cloneNTNX.ps1 script to provision the vm. This script also runs connect-Nutainx.ps1 and Create-NTNXVM.ps1 script. When I run the cloneNTNX.ps1 script I get below error.
PS C:\Users\srananavre-a> cd C:\_Mis\NTNX\scripts
PS C:\_Mis\NTNX\scripts> .\cloneNTNX.ps1
Not connected to any server. Please connect to a server and try again.
Nutanix Cluster:
Username for :
Password for
[Warning]: Cluster version[5.10.1.1] and Cmdlets version[5.5] do not match. This might cause some cmdlets to not functin correctly. Do you still want to continue [Y/N]?: y
Connected to
Checking if VM already exists...
No data found.
Creating name on
Cannot create UUID from string 'af2c1ae1-7f53-476e-8d0a-014baaa32b99 363fc90a-0d38-48e5-ac56-4b7fddf4a4e7'
New-NTNXVirtualMachine : The remote server returned an error: (500) Internal Server Error.
At C:\_Mis\NTNX\scripts\Create-NTNXVM.ps1:144 char:20
+ ... eateJobID = New-NTNXVirtualMachine -MemoryMb $ramMB -Name $VMName -Nu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Nutanix.Prism.Common.NutanixCluster:NutanixCluster) [New-NTNXVirtualMa
chine], WebException
+ FullyQualifiedErrorId : {"message":"Cannot create UUID from string 'af2c1ae1-7f53-476e-8d0a-014baaa32b99 363fc90
a-0d38-48e5-ac56-4b7fddf4a4e7'"},Nutanix.Prism.PS.Cmds.Vmmanagement.NewVirtualMachine

WARNING: Couldn't create name on , exiting

Here is the line where I get an error.
$createJobID = New-NTNXVirtualMachine -MemoryMb $ramMB -Name $VMName -NumVcpus $VMVcpus -NumCoresPerVcpu $VMCoresPerVcpu -VmNics $nicSpec -VmDisks $vmDisk -Description $Description -ErrorAction Continue

Here is the first script:
# Environment Setup
$InputFile = "C:\_Mis\NTNX\scripts\clone.csv"

# Read the input file which is formatted as name,type,address with a header row
$records = Import-CSV $InputFile

#dot source Connect-Nutanix.ps1 and connect to the cluster
. .\Connect-Nutanix.ps1
if ($ClusterName){ $connection = (Connect-Nutanix -ClusterName $ClusterName) }
else {
$connection = (Connect-Nutanix)
$ClusterName = (Get-NutanixCluster).server
}
if (!$connection){
Write-Warning "Couldn't connect to a Nutanix Cluster"
exit 1
}

#Peramiters
ForEach ($record in $records) {

# Capture the record contents as variables
$recordName = $record.name
$recordVlanID = $record.VlanID
$recordVMRAM = $record.VMRam
$recordVcpu = $record.Vcpu
$recordCores = $record.cores
$recordVMIP = $record.VMIP
$recordClone = $record.clone

# Build and ADD command syntax

$createVM = .\Create-NTNXVM.ps1 -VMName $recordName -VMVLANID $recordVlanID -VMRAMGB $recordVMRAM -VMVcpus $recordVcpu -VMCoresPerVcpu $recordCores -VMIP $recordVMIP -CloneExistingVMDisk -ExistingVMName $recordClone

Write-Host “Building VM: $recordName"
#Invoke-Expression
$createVM
}
Here is the second script:
#Create-NTNXVM.ps1
# Copyright 2016 NetVoyage Corporation d/b/a NetDocuments.
param(
[Parameter(mandatory=$true)][String]$VMName,
[Parameter(mandatory=$true)][Int]$VMVLANID,
[Parameter(mandatory=$true)][Int64]$VMRAMGB,
[Parameter(mandatory=$false)][Int]$VMVcpus = 1, #default to 1
[Parameter(mandatory=$false)][Int]$VMCoresPerVcpu = 1, #default to 1
[Parameter(mandatory=$false)][String]$VMIP,
[Parameter(ParameterSetName='Image')][Switch]$UseImageStore,
[Parameter(ParameterSetName='Image')][String]$ImageName,
[Parameter(ParameterSetName='CloneVM')][Switch]$CloneExistingVMDisk,
[Parameter(ParameterSetName='CloneVM')][String]$ExistingVMName,
[Parameter(ParameterSetName='BlankVM')][Switch]$UseBlankDisk,
[Parameter(ParameterSetName='BlankVM')][Int]$DiskSizeGB = 20, #default to 20GB if not specified
[Parameter(ParameterSetName='BlankVM')][Switch]$MountISO,
[Parameter(ParameterSetName='BlankVM')][String]$ISOName,
[Parameter(mandatory=$false)]$AdditionalVolumes, #pass an array of key:values
[Parameter(mandatory=$false)][Switch]$noPowerOn,
[Parameter(mandatory=$false)][String]$ClusterName,
[Parameter(mandatory=$false)][String]$Description
)
#dot source Connect-Nutanix.ps1 and connect to the cluster
. .\Connect-Nutanix.ps1
if ($ClusterName){ $connection = (Connect-Nutanix -ClusterName $ClusterName) }
else {
$connection = (Connect-Nutanix)
$ClusterName = (Get-NutanixCluster).server
}
if (!$connection){
Write-Warning "Couldn't connect to a Nutanix Cluster"
exit 1
}
#connection to cluster is all set up, now move on to the fun stuff
Write-Host "Checking if VM already exists..."
if (!(Get-NTNXVM -SearchString $VMName).vmid){
#convert GB to MB for RAM
$ramMB = ($VMRAMGB * 1024)
#setup the nicSpec
$nicSpec = New-NTNXObject -Name VMNicSpecDTO
#find the right network to put the VM on
$network = (Get-NTNXNetwork | ?{$_.vlanID -eq $VMVLANID})
if($network){$nicSpec.networkuuid = $network.uuid}
else{
Write-Warning "Specified VLANID: $VMVLANID, does not exist, it needs to be created in Prism, exiting"
Break
}
#request an IP, if specified
if($VMIP){$nicSpec.requestedIpAddress = $VMIP}
#setup the VM's disk
$vmDisk = New-NTNXObject -Name VMDiskDTO
if($UseImageStore -and $ImageName){
#setup the image to clone from the Image Store
$diskCloneSpec = New-NTNXObject -Name VMDiskSpecCloneDTO
#check to make sure specified Image exists in the Image Store
$diskImage = (Get-NTNXImage | ?{$_.name -eq $ImageName})
if($diskImage){
if($diskImage.Length -gt 1){
$diskToUse = $diskImage[0]
foreach($disk in $diskImage){
if($disk.updatedTimeInUsecs -gt $diskToUse.updatedTimeInUsecs){ $diskToUse = $disk }
}
$diskImage = $diskToUse
}
$diskCloneSpec.vmDiskUuid = $diskImage.vmDiskId
}
else{
Write-Warning "Specified Image Name: $ImageName, does not exist in the Image Store, exiting"
Break
}
#setup the new disk from the Cloned Image
$vmDisk.vmDiskClone = $diskCloneSpec
}
elseif($CloneExistingVMDisk -and $ExistingVMName){
#setup the image to clone from the Existing VM
$diskCloneSpec = New-NTNXObject -Name VMDiskSpecCloneDTO
#check to make sure specified Existing VM Exists
$diskToClone = ((Get-NTNXVMDisk -Vmid (Get-NTNXVM -searchstring $ExistingVMName).vmId) | ? {!$_.isCdrom})
if($diskToClone){$diskCloneSpec.vmDiskUuid = $diskToClone.VmDiskUuid}
else{
Write-Warning "Specified Existing VM Name: $ExistingVMName, does not exist, exiting"
Break
}
#setup the new disk from the Cloned Existing VM
$vmDisk.vmDiskClone = $diskCloneSpec
}
elseif($UseBlankDisk){
#setup the new disk on the default container
$diskCreateSpec = New-NTNXObject -Name VmDiskSpecCreateDTO
$diskCreateSpec.containerUuid = (Get-NTNXContainer -SearchString "default").containerUuid
$diskCreateSpec.sizeMb = $DiskSizeGB * 1024
#create the Disk
$vmDisk.vmDiskCreate = $diskCreateSpec
if($AdditionalVolumes -or $MountISO){$vmDisk = @($vmDisk)}
if($MountISO){
#setup the ISO image to clone from the Image Store
$diskCloneSpec = New-NTNXObject -Name VMDiskSpecCloneDTO
#check to make sure specified ISO exists in the Image Store
$ISOImage = (Get-NTNXImage | ?{$_.name -eq $ISOName})
if($ISOImage){
$diskCloneSpec.vmDiskUuid = $ISOImage.vmDiskId
#setup the new ISO disk from the Cloned Image
$vmISODisk = New-NTNXObject -Name VMDiskDTO
#specify that this is a Cdrom
$vmISODisk.isCdrom = $true
$vmISODisk.vmDiskClone = $diskCloneSpec
$vmDisk = @($vmDisk)
$vmDisk += $vmISODisk
}
else{
Write-Warning "Specified ISO Image Name: $ISOName, does not exist in the Image Store, skipping ISO mounting"
}
}
}
else{
if($UseImageStore -and !$ImageName){
Write-Warning "Must specify an ImageName when using -UseImageStore, exiting"
Break
}
elseif($CloneExistingVM -and !$ExistingVMName){
Write-Warning "Must specify an ExistingVMName to clone when using -CloneExistingVM, exiting"
Break
}
else{
Write-Warning "No source for $VMName's disk, must specify one of the following (UseImageStore, CloneExistingVM, UseBlankDisk), exiting"
Break
}
}
#adds any AdditionalVolumes if specified
if($AdditionalVolumes){
if(!($vmDisk[1])){$vmDisk = @($vmDisk)}
foreach($volume in $AdditionalVolumes){
$diskCreateSpec = New-NTNXObject -Name VmDiskSpecCreateDTO
$diskCreateSpec.containerUuid = (Get-NTNXContainer -SearchString "default").containerUuid
$diskCreateSpec.sizeMb = $volume.Size * 1024
$AdditionalvmDisk = New-NTNXObject -Name VMDiskDTO
$AdditionalvmDisk.vmDiskCreate = $diskCreateSpec
$vmDisk += $AdditionalvmDisk
}
}

#Create the VM
Write-Host "Creating $VMName on $ClusterName..."
$createJobID = New-NTNXVirtualMachine -MemoryMb $ramMB -Name $VMName -NumVcpus $VMVcpus -NumCoresPerVcpu $VMCoresPerVcpu -VmNics $nicSpec -VmDisks $vmDisk -Description $Description -ErrorAction Continue
if($createJobID){Write-Host "Created $VMName on $ClusterName" -ForegroundColor Green}
else{
Write-Warning "Couldn't create $VMName on $ClusterName, exiting"
Break
}
#now wait for the VM to be created and then power it on, unless noPowerOn, then we are done
if(!$noPowerOn){
$count = 0
#wait up to 30 seconds, trying every 5 seconds, for the vm to be created
while (!$VMidToPowerOn -and $count -le 6){
Write-Host "Waiting 5 seconds for $VMName to finish creating..."
Start-Sleep 5
$VMidToPowerOn = (Get-NTNXVM -SearchString $VMName).vmid
$count++
}
#now power on the VM
if ($VMidToPowerOn){
Write-Host "Powering on $VMName on $ClusterName..."
$poweronJobID = Set-NTNXVMPowerOn -Vmid $VMidToPowerOn
if($poweronJobID){Write-Host "Successfully powered on $VMName on $ClusterName" -ForegroundColor Green}
else{
Write-Warning "Couldn't power on $VMName on $ClusterName, exiting"
Break
}
}
else {
Write-Warning "Failed to Get $VMName after creation, not powering on..."
Break
}
}
}
else{
Write-Host "$VMName already exists on $ClusterName, exiting"
Break
}
icon

Best answer by JeremyJ 10 March 2020, 18:50

View original

This topic has been closed for comments

2 replies

Userlevel 3
Badge +4

I noticed your exit warning message:
Write-Warning "Couldn't create $VMName on $ClusterName, exiting"
… didn’t pick up any VMName or ClusterName variables so it’s just stating “Couldn’t create name on ,” 
There seems to be an issue with parsing the variables. 
The string is also two UUID’s separated by a space:
'af2c1ae1-7f53-476e-8d0a-014baaa32b99 363fc90a-0d38-48e5-ac56-4b7fddf4a4e7'

I’d suggest to alter the script somewhat, have it push the API call to a file or echo it to the screen, then review the API to see if it makes sense. If it does, then try it manually yourself and see what is returned from Prism when you send that API.

Userlevel 6
Badge +5

Hi @srananavre, I understand that it’s been some time. Let me know if you were able to find the culprit of the problem. Happy to try and help you on this one. Otherwise, if you would share the working version of the script with your notes here in the community that would be very valuable.