Solved

Powershell clone from ADSF


Badge +3
Hi,

We just got a nutanix cluster and i'm trying to write a script that will migrate a machine from ESXi to AHV using powershell.
I'm at the last piece and i can't get it to work.... i'm stuck at adding disk. I'm trying to do a "Clone from ADSF file" in powershell but i can't figure out. Here's my code:

#Get VM id
$vminfo = Get-NTNXVM | where {$_.vmName -eq "server1"}$vmId = ($vminfo.vmid.split(":"))[2]

$diskCloneSpec = New-NTNXObject -Name VMDiskSpecCloneDTO
$diskCloneSpec.imagepath = "/vm-general/server1/server1-flat.vmdk"
$vmDisk = New-NTNXObject -Name VMDiskDTO$vmDisk.vmDiskClone = $diskCloneSpec

Add-NTNXVMDisk -Vmid $vmId -Disks $vmDisk

And i get this error:

Add-NTNXVMDisk : The remote server returned an error: (500) Internal Server Error.At line:1 char:1+ Add-NTNXVMDisk -Vmid $vmId -Disks $vmDisk+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (Nutanix.Prism.Common.NutanixCluster:NutanixCluster) [Add-NTNXVMDisk], WebException+ FullyQualifiedErrorId : {"message":"At least one of Image path or VM Disk UUID arerequired."},Nutanix.Prism.PS.Cmds.Acropolis.AddVMDisk

Any ideas?

I will share my script once done.
Thanks
icon

Best answer by sandeepmp 25 May 2017, 16:01

AlexR

Try below.

#Get VM id$vmuuid = (Get-NTNXVM | where {$_.vmName -eq "server1"}).uuid$diskCloneSpec = New-NTNXObject -Name VMDiskSpecCloneDTO$diskCloneSpec.ndfs_filepath = "/vm-general/server1/server1-flat.vmdk"$vmDisk = New-NTNXObject -Name VMDiskDTO$vmDisk.vmDiskClone = $diskCloneSpecAdd-NTNXVMDisk -Vmid $vmuuid -Disks $vmDisk

View original

6 replies

Userlevel 7
Badge +35
Thanks for sharing AlexR -- I wonder if Sandeep can get some eyes on it to help out.
Badge +4
AlexR Could you please email me and I will see who can assist you further. - Sandeep@Nutanix.com
Userlevel 4
Badge +19
AlexR

As a workaround, you can add the below "Rest API" call in your script. Will revert shortly with the Powershell code.

#=======================Input section=======================
$server = "Cluster_IP"$username = "username"$password = "password"$memoryGB = "integer"$mem_mb = [int]$memoryGB*1024$vmname = "vmname"$vcpu = "integer"$cores_cpu = "integer"$desc = "string"$imgpath = "image_path"#example#$imagepath = "/container/filename.ext"
#=======================Main code=======================
$body = "{""name"": ""$vmname"",""memoryMb"": ""$mem_mb"",""numVcpus"": ""$vcpu"",""numCoresPerVcpu"": ""$cores_cpu"",""description"": ""$desc"",""vmDisks"": [{""vmDiskClone"": {""ndfs_filepath"": ""$imgpath""}}]}"

$Uri = "https://${server}:9440/PrismGateway/services/rest/v1/vms/"$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))}

#=======================Execution=======================
Invoke-RestMethod -Method POST -Uri $Uri -Headers $Header -Body $body -ContentType application/json
Userlevel 4
Badge +19
AlexR

Try below.

#Get VM id$vmuuid = (Get-NTNXVM | where {$_.vmName -eq "server1"}).uuid$diskCloneSpec = New-NTNXObject -Name VMDiskSpecCloneDTO$diskCloneSpec.ndfs_filepath = "/vm-general/server1/server1-flat.vmdk"$vmDisk = New-NTNXObject -Name VMDiskDTO$vmDisk.vmDiskClone = $diskCloneSpecAdd-NTNXVMDisk -Vmid $vmuuid -Disks $vmDisk
Badge +3
sandeepmp

The API stuff is what I ended doing but i was having problem when adding multiple NICs and HDDs...
The powershell stuff.... WORKS!
I will share my script once i'm done with it 🙂

Thanks a lot!
Badge +4
Nice work AlexR and thanks for sharing.

Reply