VMware Cloud Community
sg0133
Contributor
Contributor
Jump to solution

setting a scsi id for a newly added disk for VMs through script

Hi Experts ,

I was using the below scripts to add multiple disks to multiple VMs
http://blog.mattvogt.net/2013/03/14/powercli-mass-add-hard-disks/

But currently our requirement change like if we need to add 3 disks of 2 gb , the disks must the controller Use SCSI (1:0) - (1:2)

And next bunch of disks for example should use Use SCSI (2:0) - (2:2).

Is there any option for setting this SCSI ids while adding new disks.
Please let me know if there is any way we can accomplish through scripts.

Thanks in Advance.

Regards,
Sourav

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try this new version, it works for me in my test environment

### Get VM/Disk Count/Datastore information ### 
$vmname = Read-Host "VM Name to add disks to"
$num_disks = Read-Host "number of disks to add"
$ds = "Oracle DB Farm Datastore Group"
$format = Read-Host "Disk Format (thin, thick, EagerZeroedThick)"
$size = Read-Host "Disk Size (GB)"

$vm = Get-VM $vmname
$datastore = Get-DatastoreCluster -Name $ds

### Add $num_disks to VM
1..$num_disks | %{
 
Write-Host "Adding disk $_ size $size GB and format $format to $($vm.Name) on datastore $datastore"

 
if($_ -eq 1){
     
$hd = New-HardDisk -vm $vm -CapacityGB $size -Datastore $datastore -StorageFormat $format
     
$hd = Get-HardDisk -VM $vm | Where {$_.ExtensionData.Backing.UUid -eq $hd.ExtensionData.Backing.Uuid}
     
$ctrl = New-ScsiController -Type Paravirtual -HardDisk $hd
  }
 
else{
     
$hd = New-HardDisk -vm $vm -CapacityGB $size -Datastore $datastore -StorageFormat $format -Controller $ctrl
   
$hd = Get-HardDisk -VM $vm | Where {$_.ExtensionData.Backing.UUid -eq $hd.ExtensionData.Backing.Uuid}
  }
}


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

View solution in original post

0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

See my reply in 1.  Re: Specify SCSI ID when adding new RDMs?


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

sg0133
Contributor
Contributor
Jump to solution

Hi Luc ,

Thanks for the link. I modified the script accordingly but getting the below error. Also my intension was to add
the new disks to the new controller but it is getting added in continuation of the old controller.For example
disk 3====scsi(1:0)
disk 4====scsi(1:1)
disk 5====scsi(1:2)
But for me it is getting added to below format :
disk 3===scsi(0:3)
disk 4===scsi(0:4)
disk 5===scsi(0:5)


New-ScsiController : Cannot validate argument on parameter 'HardDisk'. The argument is null. Supply a non-null argument and try the command again.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\diskadd.ps1:13 char:37
+ $ctrl = New-ScsiController -HardDisk <<<<  $hd -Type Paravirtual
    + CategoryInfo          : InvalidData: (:) [New-ScsiController], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewScsiController

New-ScsiController : Cannot validate argument on parameter 'HardDisk'. The argument is null. Supply a non-null argument and try the command again.


Adding 2 VMDK to cts-prod-vm-1 on datastore Oracle DB Farm  Datastore Group
Exception calling "ReconfigVM" with "1" argument(s): "The attempted operation cannot be performed in the current state (Powered on)."
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\diskadd.ps1:25 char:29
+ $vm.ExtensionData.ReconfigVM <<<< ($spec)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Adding 2 VMDK to cts-prod-vm-1 on datastore Oracle DB Farm Datastore Group
Exception calling "ReconfigVM" with "1" argument(s): "Cannot complete operation due to concurrent modification by another operation."
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\diskadd.ps1:25 char:29
+ $vm.ExtensionData.ReconfigVM <<<< ($spec)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Adding 2 VMDK to cts-prod-vm-1 on datastore Oracle DB Farm Datastore Group
Exception calling "ReconfigVM" with "1" argument(s): "Cannot complete operation due to concurrent modification by another operation."
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\diskadd.ps1:25 char:29
+ $vm.ExtensionData.ReconfigVM <<<< ($spec)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try it like this

### Get VM/Disk Count/Datastore information ### 
$vmname = Read-Host "VM Name to add disks to"
$num_disks = Read-Host "number of disks to add"
$ds = "Oracle DB Farm Datastore Group"
$format = Read-Host "Disk Format (thin, thick, EagerZeroedThick)"
$size = Read-Host "Disk Size (GB)"

$vm = Get-VM $vmname
$datastore = Get-DatastoreCluster -Name $ds
$x = 0

### Add $num_disks to VM
while ($x -lt $num_disks){
 
Write-Host "Adding $size VMDK to $vm on datastore $datastore"
 
$hd = New-HardDisk -vm $vm -CapacityGB $size -Datastore $datastore -StorageFormat $format
 
$hd = Get-HardDisk -VM $vm |
 
Where {$_.ExtensionData.Backing.UUid -eq $hd.ExtensionData.Backing.Uuid}

 
if($x -eq 0){
   
$ctrl = New-ScsiController -HardDisk $hd -Type Paravirtual
  }

 
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
 
$spec.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion

 
$disk = New-Object VMware.Vim.VirtualDeviceConfigSpec
 
$disk.device = $hd.ExtensionData
 
$disk.device.UnitNumber = -($x + 1)
 
$disk.operation = "edit"
 
$spec.DeviceChange += $disk
 
$x++
 
}

$vm.ExtensionData.ReconfigVM($spec)

It creates the new controller when the 1st harddisk has been created.

The new harddisks are create in 1 call to the ReconfigVM method.


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

0 Kudos
sg0133
Contributor
Contributor
Jump to solution


Hi Luc ,

I got an error specifying that new-scsicontroller needs to added while powering off the VM, so i couldn't tested it as there was no downtime. Will this can't be added online ...

One more error came for ReconfigVM , couldn't understand why it was gving that error "Cannot complete operation due to concurrent modification".

Regards ,

Sourav

Please find the errors below :

Disk Size (GB): 7
Adding 7 VMDK to cts-prod-vm-1 on datastore Oracle DB Farm Datastore Group

New-ScsiController : 11/6/2013 8:33:53 AM    New-ScsiController        The VM must be in the following state: PoweredOff.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\diskadd.ps1:19 char:31
+     $ctrl = New-ScsiController <<<<  -HardDisk $hd -Type Paravirtual
    + CategoryInfo          : InvalidOperation: (PoweredOn:PowerState) [New-ScsiController], ViError
    + FullyQualifiedErrorId : Common_SharedParameterHelper_VerifyVmIsInState_VmNotInState,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewScsiController

Adding 7 VMDK to cts-prod-vm-1 on datastore Oracle DB Farm Datastore Group

Adding 7 VMDK to cts-prod-vm-1 on datastore Oracle DB Farm Datastore Group

Exception calling "ReconfigVM" with "1" argument(s): "Cannot complete operation due to concurrent modification by another operation."
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\diskadd.ps1:32 char:29
+ $vm.ExtensionData.ReconfigVM <<<< ($spec)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is a known issue, the cmdlet insists that the VM be powered off, but that is in fact not needed.

Instead of using the New-ScsiController cmdlet, you can add the controller through the ReconfigVM method.


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

0 Kudos
sg0133
Contributor
Contributor
Jump to solution

Hi Luc ,

After powering off the VM i tried to add the disk in new controller, the disk & controller gets added but it starts with scsi(1:1). I am getting the below errors :

Adding 1 VMDK to cts-prod-vm-1 on datastore datastore Oracle DB Farm Datastore Group

Exception calling "ReconfigVM" with "1" argument(s): "Invalid configuration for device '0'."

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\diskadd.ps1:32 char:29

+ $vm.ExtensionData.ReconfigVM <<<< ($spec)

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

Regards,

Sou

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try changing this line

$disk.device.UnitNumber = -($x + 1)


into this


$disk.device.UnitNumber = - $x


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

0 Kudos
sg0133
Contributor
Contributor
Jump to solution

Hi Luc ,

I tried this option , but it gives the same error and the new disk allocation starts  with scsi(1:1) .

Adding 1 VMDK to cts-prod-vm-1 on datastore datastore Oracle DB Farm Datastore Group

Exception calling "ReconfigVM" with "1" argument(s): "Invalid configuration for device '0'."

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\diskadd.ps1:32 char:29

+ $vm.ExtensionData.ReconfigVM <<<< ($spec)

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

Regards,

Sou

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try this new version, it works for me in my test environment

### Get VM/Disk Count/Datastore information ### 
$vmname = Read-Host "VM Name to add disks to"
$num_disks = Read-Host "number of disks to add"
$ds = "Oracle DB Farm Datastore Group"
$format = Read-Host "Disk Format (thin, thick, EagerZeroedThick)"
$size = Read-Host "Disk Size (GB)"

$vm = Get-VM $vmname
$datastore = Get-DatastoreCluster -Name $ds

### Add $num_disks to VM
1..$num_disks | %{
 
Write-Host "Adding disk $_ size $size GB and format $format to $($vm.Name) on datastore $datastore"

 
if($_ -eq 1){
     
$hd = New-HardDisk -vm $vm -CapacityGB $size -Datastore $datastore -StorageFormat $format
     
$hd = Get-HardDisk -VM $vm | Where {$_.ExtensionData.Backing.UUid -eq $hd.ExtensionData.Backing.Uuid}
     
$ctrl = New-ScsiController -Type Paravirtual -HardDisk $hd
  }
 
else{
     
$hd = New-HardDisk -vm $vm -CapacityGB $size -Datastore $datastore -StorageFormat $format -Controller $ctrl
   
$hd = Get-HardDisk -VM $vm | Where {$_.ExtensionData.Backing.UUid -eq $hd.ExtensionData.Backing.Uuid}
  }
}


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

0 Kudos
sg0133
Contributor
Contributor
Jump to solution

Hi Luc ,

Thanks for the script. It is working only exception is that if i give 1 disk to to be added it takes the scsi id as (1:1). But anyway i am got going to add single disk hence not worried at all.

Can you please explain what this below line in the script actually doing :

1..$num_disks | %{


Thanks in Advance.


Regards,

Sou

0 Kudos
robnasby
Contributor
Contributor
Jump to solution

1..$num_disks is a range.  So if $num_disk is 3, the range is 1..3, or 1, 2, 3.  Piping that to %{} will loop through the values in the range, with $_ containing the value for the current iteration.

0 Kudos