VMware {code}

 View Only
  • 1.  Add Hard Disk to scsi controller

    Posted Jun 22, 2022 01:34 PM

    Good morning,
    i am trying to make a script that will allow me to add a new hd to a set of vms.
    My difficulty is that I am being asked to make sure that the new hd is on a particular predetermined scsi channel, in particular it must use controller 0:13.
    To clarify, those parameters that under the web interface are named "Virtual Device Node" and via cli, I believe they are named "ExtensionData.BusNumber and $_.ExtensionData.UnitNumber".

    The controller already exists and does not need to be added

    Can someone please give me some suggestions?

    thanks in advance
    Daniel



  • 2.  Re: Add Hard Disk to scsi controller

    Posted Jun 23, 2022 08:32 AM
    Below is the sample java code to set the particular controller in adding VirtualDisk. 
     
    *unitNumber = The unit number of this device on its controller. 

     

    .
    .
    int ckey = 0;
    VirtualDisk disk = new VirtualDisk(); 
    VirtualDevice[] vDevice = vmConfigInfo.getHardware().getDevice();
    for (int i = 0; i < vDevice.length; i++) {
       if (vDevice[i].getDeviceInfo().getLabel().equalsIgnoreCase("SCSI Controller 0")) 
       {
         ckey = vDevice[i].getKey();
         break;
       }
    }
    disk.setControllerKey(ckey);
    disk.setUnitNumber(13); //SCSI Controller 0 - 0:13.
    .
    .

     

     

     

     

     



  • 3.  Re: Add Hard Disk to scsi controller

    Posted Jun 24, 2022 12:00 AM

    It's doable if the VM is powered down.  But you'd have to think there's a way to do this at the time the hard disk is added, like you can specify in the vSphere Client.  Anyway, for the less elegant way of adding a hard disk and setting the SCSI ID on a powered off VM...

    Before...disk added and reconfigured...and after:

    PS C:\> $VM=Get-VM -Name CMOXYTESTWV0504
    PS C:\> $VM | Get-HardDisk | Select-Object @{N='VM';E={$_.Parent.Name}},Name,@{N='SCSIid';E={$hd=$_;$ctrl=$hd.Parent.Extensiondata.Config.Hardware.Device | Where-Object {$_.Key -eq $hd.ExtensionData.ControllerKey};"$($ctrl.BusNumber):$($_.ExtensionData.UnitNumber)"}}
    
    VM              Name        SCSIid
    --              ----        ------
    CMOXYTESTWV0504 Hard disk 1 0:0
    CMOXYTESTWV0504 Hard disk 2 0:1
    
    
    PS C:\> $NewHD=New-HardDisk -VM $VM -CapacityGB 10 -Persistence Persistent -Controller "SCSI controller 0"
    PS C:\> $VMCfgSpec=New-Object VMware.Vim.VirtualMachineConfigSpec
    PS C:\> $VMCfgSpec.ChangeVersion=$VM.ExtensionData.Config.ChangeVersion
    PS C:\> $VMCfgSpec.DeviceChange+=New-Object VMware.Vim.VirtualDeviceConfigSpec
    PS C:\> $VMCfgSpec.DeviceChange[0].Device=$NewHD.ExtensionData
    PS C:\> $VMCfgSpec.DeviceChange[0].Device.UnitNumber=13
    PS C:\> $VMCfgSpec.DeviceChange[0].Operation="Edit"
    PS C:\> $VM.ExtensionData.ReconfigVM($VMCfgSpec)
    PS C:\> $VM | Get-HardDisk | Select-Object @{N='VM';E={$_.Parent.Name}},Name,@{N='SCSIid';E={$hd=$_;$ctrl=$hd.Parent.Extensiondata.Config.Hardware.Device | Where-Object {$_.Key -eq $hd.ExtensionData.ControllerKey};"$($ctrl.BusNumber):$($_.ExtensionData.UnitNumber)"}}
    
    VM              Name        SCSIid
    --              ----        ------
    CMOXYTESTWV0504 Hard disk 1 0:0
    CMOXYTESTWV0504 Hard disk 2 0:1
    CMOXYTESTWV0504 Hard disk 3 0:13
    
    
    PS C:\>

     



  • 4.  Re: Add Hard Disk to scsi controller
    Best Answer

    Posted Jun 24, 2022 06:27 AM

    Hi    ,

    thank you very much for your help.
    In fact I had then managed to find a way to do it, very similar to yours.
    It works with VM switched off.
    When you add a "hard disk" with VM on, it is assigned to the first free "scsi" channel.
    And afterwards it is not possible to make the channel change while the vm is switched on.

    Unfortunately I could not find any way to assign the channel when creating the new disk.

    This annoys me a bit because you can do this by hand on vsphere and not via powercli...

    And since I need to add a disk to about 50 machines (which I can't shut down) on which data will have to be backed up based on hard disk scsi number.

    Present disks remain under backup and this new one must be filtered instead.

    The backup software (commvault) can only make a difference on scsi channels or disk names. But even that is not editable. They are always called "Hard Disk n".
    Does anyone have any suggestions?

    Thanks for your help.
    I appreciate it.
    Daniel