PowerCLI

  • 1.  Assign SCSI Target # "UnitNumber" during a new VMDK creation

    Posted Feb 18, 2025 02:46 PM

    Hi,

     

    I already have working code to add new or existing VMDKs to specific VM SCSI Controllers and even create brand new controllers if I need to. What I'm looking to do is assign the disk Unit number during the new-harddisk. example scsi 3:6. I haven't seen any syntax in new-hardisk to do this.

     

    From research I've done, people have said do the new-harddisk the regular way and then do a reconfigure with change information about the unitnumber. But it appears that way doesn't work if the VM is running. I did borrow/write code that works when the VM is down. But I really want the unitnumber assigned or updated while its running.

     

    Here is the code I have currently.

    $vmobj = get-vm "VM Xyz"
    $scsicontroller = 2
    $scsitarget = 5
    $controllers = get-scsicontroller -vm $vmobj | Where-object {$_.Key -eq 1000 + $scsicontroller -or $_key -eq $scsicontroller}
            if(-not $controllers) {
                $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
                $device = New-Object VMware.Vim.VirtualDeviceConfigSpec
                $device.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add
                $device.Device = New-Object VMware.Vim.ParaVirtualScsiController
                $device.Device.BusNumber = $scsicontroller
                $spec.DeviceChange += $device
                #write-output("Adding SCSI Controller $scsicontroller")
                $vmobj.ExtensionData.ReconfigVM($spec)
                $vmobj.ExtensionData.UpdateViewData()
            } else {
                #write-output("Found SCSI Controller $scsicontroller")
                $foundcontroller = $true
            }
    $newdisk = new-harddisk -persistence "Persistent" -vm $vmobj -storageformat "Thick" -Datastore "DatastoreXYZ" -capacitygb 10 -disktype "flat"
    if(-not $newdisk) {
        write-error("Disk was not created")
        write-output("INFO: status=1")
        exit(1)
    }
    if($scsitarget -ne -1) {
    	$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    	$device = New-Object VMware.Vim.VirtualDeviceConfigSpec
    	$device.operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::edit
    	$device.device = $newdisk.extensiondata
    	$device.device.unitnumber = -($scsitarget)
    	$spec.DeviceChange += $device
    	$vmobj.ExtensionData.ReconfigVM($spec)
    }
    


  • 2.  RE: Assign SCSI Target # "UnitNumber" during a new VMDK creation

    Posted Feb 18, 2025 03:07 PM

    Actually, it looks like what I thought was working code to change scsi id on a down VM doesn't work either. The question is still about adding a VMDK with scsi unitnumber set, but here is the error I get when trying to change the unitnumber for an existing disk.

    $device.device.unitnumber = -($scsitarget)
    InvalidOperation: The property 'unitnumber' cannot be found on this object. Verify that the property exists and can be set.
    I also tried $device.device.unitnumber = ($scsitarget)