Automation

 View Only
  • 1.  Add Existing Virtual Disk to VM

    Posted Oct 25, 2021 11:50 AM

    Hey guys I'm looking for a script that will add ten existing virtual disks to a VM where the location of the disks is known on several different datastores.  I'd also like to specify which scsi controller they get put on as well.

    Thanks,

    Pete

    I'm playing the the command below.  What format does the datatore and existing disk name need to be in after -DiskPath

    I keep getting New-HardDisk : 10/25/2021 7:42:04 AM New-HardDisk No matching datastore found.  When I use 'datastore name space vmname/vmdk name

     

    New-Harddisk -VM -Controller -Diskpath 



  • 2.  RE: Add Existing Virtual Disk to VM
    Best Answer

    Posted Oct 25, 2021 12:03 PM

    Have a look at Example 6 of the New-Harddisk cmdlet for the format of the disk path.

    The Controller parameter takes an object returned by Get-ScsiController or New-ScsiController



  • 3.  RE: Add Existing Virtual Disk to VM

    Posted Oct 25, 2021 12:20 PM

    Figured out the format for the the DiskPath parameter.  Needs to be exactly as you see it in the VM's settings '[Datastore Name]  VM/VMDK'  

     

    Trying to figure out what it's looking for for the -Controller Parameter.   If I want to place each disk on a specific controller and scsi id.



  • 4.  RE: Add Existing Virtual Disk to VM
    Best Answer

    Posted Oct 25, 2021 12:30 PM

    Not with a cmdlet afaik, but it can be done via the API.

    See for example Re: Automate add Windows Hard Disk



  • 5.  RE: Add Existing Virtual Disk to VM

    Posted Oct 25, 2021 12:39 PM

    I got this to work just now.  It would be nice if I could specify the scsi id for the disk to land on ... on each controller.  But this will work for what I need it to do.

     

    New-HardDisk -VM srmtest2 -Controller 'SCSI controller 0' -Persistence Persistent -DiskPath '[Datastore Name] SRMTEST2/SRMTEST2_3.vmdk'
    New-HardDisk -VM srmtest2 -Controller 'SCSI controller 1' -Persistence Persistent -DiskPath '[Datastore Name] SRMTEST2/SRMTEST2_5.vmdk'
    New-HardDisk -VM srmtest2 -Controller 'SCSI controller 1' -Persistence Persistent -DiskPath '[Datastore Name] SRMTEST2/SRMTEST2_7.vmdk'



  • 6.  RE: Add Existing Virtual Disk to VM

    Posted Oct 25, 2021 08:56 PM

    Yeah, it would be nice, but it requires perform a separate ReconfigVM_Task() to specify.

    This changes the 4th VMDK to SCSI ID  3:

      $VMObj = Get-VM -Name <VM Name>
      $NewTargetId = 3
      $DiskObjs = Get-HardDisk -VM $VMObj
      $DiskFileNames = $DiskObjs.FileName | Group-Object Length | Sort-Object Name | ForEach-Object { $_.group | Sort-Object }
      $DiskObj = Get-HardDisk -VM $VMObj | Where-Object { $_.Filename -eq $DiskFileNames[3] }
      $NewSpec = New-Object -Type VMware.Vim.VirtualMAchineConfigSpec
      $NewSpec.DeviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
      $NewSpec.DeviceChange[0].Operation = "edit"
      $NewSpec.DeviceChange[0].Device = $DiskObj.ExtensionData
      $NewSpec.DeviceChange[0].Device.UnitNumber = $NewTargetId
      $TaskMoRef = $VMObj.ExtensionData.ReconfigVM_Task($NewSpec)
      $DiskObj = Get-HardDisk -VM $VMObj | Where-Object { $_.Filename -eq $DiskFileNames[3] }

    What's even more annoying is that modifying the SCSI controller or target ID of a VMDK will change the Name property of the impacted disk and any subsequent disks. If there's 8 disks, modifying "Hard Disk 5" will rename it to "Hard Disk 8" and the previously named "Hard Disk [6-8]" will be renamed to "Hard Disk [5-7]" and all of them will get new ID numbers. The only thing that doesn't change is the filenames of the VMDKs. Because all the properties change, you have to perform a Get-HardDisk after each time you modify a disk, as illustrated above.

    The whole thing becomes such a complicated mess that I created a Update-VMScsiController function for the team that deals with VM hardware so they'd have a much easier time of it. I'd copy it here, but all the error handling and such caused it to be over 500 lines long.