VMware Cloud Community
jeep2004
Enthusiast
Enthusiast

add Exists vmdk to new vm with powerCLI

HI

I have read alot off post here ,but I miss something 

I need to removed and  addd all the  56  disks VMDK from  old VM  to new VM.

(note: the remove disk I will do with GUI...) 

I need to do it with exists Scsi ID and number ID 

I see here all command here but I need to do it with import and Export - its fast 🙂 

this what I see and need to fix the export to new vm with the number ID ? 

Get-VM "vmname" | Get-HardDisk |
Select @{N='VM';E={$_.Parent.Name}},Name,FileName,@{N='SCSIid';E={
$hd = $_
$ctrl = $hd.Parent.Extensiondata.Config.Hardware.Device | where{$_.Key -eq $hd.ExtensionData.ControllerKey}
"$($ctrl.BusNumber):$($_.ExtensionData.UnitNumber)" }} |
Export-Csv -Path .\disknames.csv -UseCulture -NoTypeInformation

#####################


Import-Csv -Path .\disknames.csv -UseCulture |
ForEach-Object -Process {
$newFileName = $_.FileName -replace "-\d{6}\.","."
$vm = Get-VM -Name $_.VM
$scsiid = $_.SCSIid
New-HardDisk -VM $vm -DiskPath $newFileName -Controller "Scsi controller 3" 

0 Kudos
7 Replies
LucD
Leadership
Leadership

What do you mean with the ID?
Is that the SCSI ID, like for example SCSI 2:0?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast

yes 

this is my export separately

VM, Name, Filename, SCSIID

servernameXX, Hard disk2, storage\servernameXX\servernamexx_1.vmdk  3:04

I need to export the SCSIID and UnitNumber separately and to import to new vm 

 

thank you 

Tags (1)
0 Kudos
LucD
Leadership
Leadership

Do the controllers already exist, or do they need to be created as well?

In any case, this will not work with a cmdlet but will require using the ReconfigVM_Task method.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast

The controllers already exist

but I can deleted before 

 

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership

Keep the SCSI controllers.
Something like this should work.

$vmName = 'TargetVM'

$vm = Get-VM -Name $vmName

$deviceKey = -1
$spec = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec

Import-Csv -Path .\disknames.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
  $ctrl, $unit = $row.SCSIid.Split(':')
  $device = New-Object -TypeName VMware.Vim.VirtualDeviceConfigSpec
  $disk = New-Object -TypeName VMware.Vim.VirtualDisk
  $disk.UnitNumber = $unit
  $disk.ControllerKey = (Get-ScsiController -VM $vm).where{$_.Name -match "$($ctrl)$"}.Key
  $disk.Key = $deviceKey
  $deviceKey+=-1
  $deviceInfo = New-Object -TypeName VMware.Vim.Description
  $deviceInfo.Label = $row.Name
  $deviceInfo.Summary = $row.Name
  $disk.DeviceInfo = $deviceInfo

  $backing = New-Object -TypeName VMware.Vim.VirtualDiskFlatVer2BackingInfo
  $backing.FileName = $row.FileName
  $backing.DiskMode = [VMware.Vim.VirtualDiskMode]::persistent
  $disk.Backing = $backing

  $device.Device = $disk
  $device.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add
  $spec.DeviceChange += $device
}

$vm.ExtensionData.ReconfigVM($spec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jeep2004
Enthusiast
Enthusiast

thank yoy its work fine

but how I do the import  from one VM to  other 

If yo can do to Separate the script to two it will be excellent

 

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership

The 'targetVM' is the new VM onto which you attach the VMDK.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos