VMware Cloud Community
js40687
Contributor
Contributor

Automate add Windows Hard Disk

Hi

I'm trying to automate adding a new Hard Disk to a Windows 2008R2 VM

The VM already exists and has 2 disks already on the default SCSI Controller (LSI Logic SAS) as SCSI 0:0 and SCSI 0:1

I want to add a 3rd disk on a new ParaVirtual Controller

This I can do easily enough with the following

Get-VM $VM_Name | New-HardDisk -CapacityKB $Drive_Size -Datastore $datastore2 | New-ScsiController -Type ParaVirtual

but.......it comes in on SCSI 1:2, not a massive problem but to follow our standards I ideally want it on SCSI 1:0, how can I change that via PowerCLi?

not too concerned if it is create it and let it default to SCSI 1:2 and then change it or create it directly on SCSI 1:0 in the first place

Any ideas please?

Thanks

Jim


Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

This will add a harddisk on a paravirtual controller, and then change the unit number of the harddisk to 0.

$vm = Get-VM -Name VM
$hd = New-HardDisk -VM $vm -CapacityGB 1 -StorageFormat Thin
$ctrl = New-ScsiController -HardDisk $hd -Type Paravirtual
$hd = Get-HardDisk -VM $vm | Where {$_.Name -eq $hd.Name}

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion
$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device = $hd.ExtensionData
$spec.deviceChange[0].device.UnitNumber = 0
$spec.deviceChange[0].operation = "edit"
$vm.ExtensionData.ReconfigVM($spec)


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

Reply
0 Kudos
js40687
Contributor
Contributor

Thanks Luc

What I am trying to do is add 4 new disks to an existing 2008R2 VM

The VM already has 2 disks, C: drive and 😧 drive

Hard Disk 1 SCSI 0:0

Hard Disk 2 SCSI 0:1

I then want to add the following 4 disks, this is our standard for SQL servers

Hard Disk 3 SCSI 1:0 new ParaVirtual Controller 1Gb

Hard Disk 4 SCSI 2:0 new ParaVirtual Controller 2Gb

Hard Disk 5 SCSI 2:1 Controller above 3Gb

Hard Disk 6 SCSI 3:0 new ParaVirtual Controller 4Gb

The sizes are only to help identify the disks

Using the code pasted below, I seem to get the following config, plus an error when allocating the 3rd disk, the error being

I guess it is trying to add to the default original Controller on UnitNumber 1 which is already in use

How do I point it at SCSI 2:1? Is there a device.ControllerNumber parameter?

On completion I get

Hard Disk 3 SCSI 0:2 3Gb

Hard Disk 4 SCSI 1:0 1Gb correct, other than I would expect it to be Hard Disk 3

Hard Disk 5 SCSI 2:2 2Gb should have been SCSI 2:0, and Hard Disk 4

Hard Disk 6 SCSI 3:3 4Gb should have been SCSI 3:0

I am assuming the error isn’t helping with the Hard Disk numbering?

The code I am using is

$vm = Get-VM -Name $VM_Name

$hd = New-HardDisk -VM $vm -CapacityGB $R_Drive_Size

$ctrl = New-ScsiController -HardDisk $hd -Type Paravirtual

$hd = Get-HardDisk -VM $vm | Where {$_.Name -eq $hd.Name}

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$spec.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion

$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec

$spec.deviceChange[0].device = $hd.ExtensionData

$spec.deviceChange[0].device.UnitNumber = 0

$spec.deviceChange[0].operation = "edit"

$vm.ExtensionData.ReconfigVM($spec)

$vm = Get-VM -Name $VM_Name

$hd = New-HardDisk -VM $vm -CapacityGB $S_Drive_Size

$ctrl = New-ScsiController -HardDisk $hd -Type Paravirtual

$hd = Get-HardDisk -VM $vm | Where {$_.Name -eq $hd.Name}

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$spec.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion

$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec

$spec.deviceChange[0].device = $hd.ExtensionData

$spec.deviceChange[0].device.UnitNumber = 0

$spec.deviceChange[0].operation = "edit"

$vm.ExtensionData.ReconfigVM($spec)

$vm = Get-VM -Name $VM_Name

$hd = New-HardDisk -VM $vm -CapacityGB $T_Drive_Size

$hd = Get-HardDisk -VM $vm | Where {$_.Name -eq $hd.Name}

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$spec.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion

$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec

$spec.deviceChange[0].device = $hd.ExtensionData

$spec.deviceChange[0].device.UnitNumber = 1

$spec.deviceChange[0].operation = "edit"

$vm.ExtensionData.ReconfigVM($spec)

$vm = Get-VM -Name $VM_Name

$hd = New-HardDisk -VM $vm -CapacityGB $U_Drive_Size

$ctrl = New-ScsiController -HardDisk $hd -Type Paravirtual

$hd = Get-HardDisk -VM $vm | Where {$_.Name -eq $hd.Name}

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$spec.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion

$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec

$spec.deviceChange[0].device = $hd.ExtensionData

$spec.deviceChange[0].device.UnitNumber = 0

$spec.deviceChange[0].operation = "edit"

$vm.ExtensionData.ReconfigVM($spec)

Cheers

Jim

Reply
0 Kudos
LucD
Leadership
Leadership

New harddisks are always assigned to the lowest available unit and cluster numbers.

So in case of the 3th harddisk, you also need to specify to which controller it should go.

A 2nd problem is the harddisk name changes when you move the harddisk around, so in this version I use the UUID to identify a specific harddisk.

The UUID is unique and doesn't change when the harddisk is moved around.

Try like this

$vm = Get-VM -Name $VM_Name

$hd = New-HardDisk -VM $vm -CapacityGB 1 -StorageFormat Thin
$ctrl = New-ScsiController -HardDisk $hd -Type Paravirtual
$hd = Get-HardDisk -VM $vm |
 
Where {$_.ExtensionData.Backing.UUid -eq $hd.ExtensionData.Backing.Uuid}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion
$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device = $hd.ExtensionData
$spec.deviceChange[0].device.UnitNumber = 0
$spec.deviceChange[0].operation = "edit"
$vm.ExtensionData.ReconfigVM($spec)

$vm = Get-VM -Name $VM_Name
$hd = New-HardDisk -VM $vm  -CapacityGB 2 -StorageFormat Thin
$ctrl = New-ScsiController -HardDisk $hd -Type Paravirtual
$hd = Get-HardDisk -VM $vm |
 
Where {$_.ExtensionData.Backing.UUid -eq $hd.ExtensionData.Backing.Uuid}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion
$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device = $hd.ExtensionData
$spec.deviceChange[0].device.UnitNumber = 0
$spec.deviceChange[0].operation = "edit"
$vm.ExtensionData.ReconfigVM($spec)

$vm = Get-VM -Name $VM_Name
$hd = New-HardDisk -VM $vm  -CapacityGB 3 -StorageFormat Thin
$hd = Get-HardDisk -VM $vm |
 
Where {$_.ExtensionData.Backing.UUid -eq $hd.ExtensionData.Backing.Uuid}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion
$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device = $hd.ExtensionData
$spec.deviceChange[0].device.ControllerKey = $ctrl.ExtensionData.Key
$spec.deviceChange[0].device.UnitNumber = 1
$spec.deviceChange[0].operation = "edit"
$vm.ExtensionData.ReconfigVM($spec)

$vm = Get-VM -Name $VM_Name
$hd = New-HardDisk -VM $vm  -CapacityGB 4 -StorageFormat Thin
$ctrl = New-ScsiController -HardDisk $hd -Type Paravirtual
$hd = Get-HardDisk -VM $vm |
 
Where {$_.ExtensionData.Backing.UUid -eq $hd.ExtensionData.Backing.Uuid}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion
$spec.DeviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].device = $hd.ExtensionData
$spec.deviceChange[0].device.UnitNumber = 0
$spec.deviceChange[0].operation = "edit"
$vm.ExtensionData.ReconfigVM($spec)

For a quick test I made some changes to the parameters on the New-Harddisk cmdlets, you will have to update those lines.


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

Reply
0 Kudos
js40687
Contributor
Contributor

Thanks Luc

A combination of using the UUID and specifying the actual Controller has resolved this

All are now on the correct Controllers and on the correct ID

Many Thanks

Jim

Reply
0 Kudos