I am trying to create a Powercli script for adding additional disks to Sql vms. Scripts runs fine but the disks labels wont create evenly across multiple scsi controllers.
Ideally this is what I am trying to get.
Hard disk 1 on SCSI 0:1
Hard disk 2 on SCSI 0:2
Hard disk 3 on SCSI 1:1
Hard disk 4 on SCSI 1:2
Hard disk 5 on SCSI 2:1
Hard disk 6 on SCSI 2:2
Hard disk 7 on SCSI 3:1
Hard disk 8 on SCSI 3:2
Hard disk 9 on SCSI 3:3
But this is what I get.
Hard disk 7 on SCSI 0:1
Hard disk 2 on SCSI 0:2
Hard disk 8 on SCSI 1:1
Hard disk 6 on SCSI 1:2
Hard disk 9 on SCSI 2:1
Hard disk 4 on SCSI 2:2
Hard disk 10 on SCSI 3:1
Hard disk 5 on SCSI 3:2
Hard disk 3 on SCSI 3:3
_________________________________________________________________________
Here is my script.
$VMname = Read-Host " Enter the VM hostname"
$D=Read-Host "Enter D drive GB Size"
$E=Read-Host "Enter E drive GB Size"
$SYSTEMDBS1 = Read-Host "Enter SYSTEMDB1 GB Size"
$DATA1 = Read-Host "Enter DATA1 GB Size"
$LOGS1 = Read-Host "Enter LOGS1 GB Size"
$DBA_ADMIN_LOGS1 = Read-Host "Enter DBA_ADMIN_LOGS1 GB Size"
$TEMPDB1 = Read-Host "Enter TEMPDB1 GB Size"
$DBA_ADMIN_DATA1 = Read-Host "Enter DBA_ADMIN_DATA1 GB Size"
$DBAUTILITY = Read-Host "Enter DBAUTILITY GB Size"
stop-vm -vm $VMname -confirm:$false
Start-sleep -s 5
$VM= get-vm -Name $VMname
##Adding D and E Drive
New-HardDisk -VM $VM -CapacityGB $D -DiskType Flat -StorageFormat Thin
New-HardDisk -VM $VM -CapacityGB $E -DiskType Flat -StorageFormat Thin
foreach ($HardDisk in (1))
{
$VM | New-HardDisk -CapacityGB $SYSTEMDBS1 -DiskType Flat -StorageFormat Thin | New-ScsiController -Type ParaVirtual
##
$hdskToChange = Get-HardDisk -VM $VM -Name "Hard disk 4"
##$VM | Get-HardDisk -Name "Hard Disk 3"
## create a new VirtualMachineConfigSpec, with which to make the change to the VM's disk
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
## create a new VirtualDeviceConfigSpec
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "edit"
## populate the "device" property with the existing info from the hard disk to change
$spec.deviceChange[0].device = $hdskToChange.ExtensionData
## then, change the second part of the SCSI ID (the UnitNumber)
$spec.deviceChange[0].device.unitNumber = 0
## reconfig the VM with the updated ConfigSpec (VM must be powered off)
$VM.ExtensionData.ReconfigVM_Task($spec)
Start-Sleep -s 5
New-HardDisk -VM $VM -CapacityGB $DATA1 -DiskType Flat -StorageFormat Thin -Controller "SCSI Controller 1"
}
## Disks on Controller 2
foreach ($HardDisk in (1))
{
$VM | New-HardDisk -CapacityGB $LOGS1 -DiskType Flat -StorageFormat Thin | New-ScsiController -Type ParaVirtual
##
$hdskToChange = Get-HardDisk -VM $VM -Name "Hard disk 6"
##$VM | Get-HardDisk -Name "Hard Disk 3"
## create a new VirtualMachineConfigSpec, with which to make the change to the VM's disk
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
## create a new VirtualDeviceConfigSpec
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "edit"
## populate the "device" property with the existing info from the hard disk to change
$spec.deviceChange[0].device = $hdskToChange.ExtensionData
## then, change the second part of the SCSI ID (the UnitNumber)
$spec.deviceChange[0].device.unitNumber = 0
## reconfig the VM with the updated ConfigSpec (VM must be powered off)
$VM.ExtensionData.ReconfigVM_Task($spec)
Start-Sleep -s 5
New-HardDisk -VM $VM -CapacityGB $DBA_ADMIN_LOGS1 -DiskType Flat -StorageFormat Thin -Controller "SCSI Controller 2"
}
## For Disk on Controller 3
foreach ($HardDisk in (1))
{
$VM | New-HardDisk -CapacityGB $TEMPDB1 -DiskType Flat -StorageFormat Thin | New-ScsiController -Type ParaVirtual
##
$hdskToChange = Get-HardDisk -VM $VM -Name "Hard disk 8"
##$VM | Get-HardDisk -Name "Hard Disk 3"
## create a new VirtualMachineConfigSpec, with which to make the change to the VM's disk
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
## create a new VirtualDeviceConfigSpec
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = "edit"
## populate the "device" property with the existing info from the hard disk to change
$spec.deviceChange[0].device = $hdskToChange.ExtensionData
## then, change the second part of the SCSI ID (the UnitNumber)
$spec.deviceChange[0].device.unitNumber = 0
## reconfig the VM with the updated ConfigSpec (VM must be powered off)
$VM.ExtensionData.ReconfigVM_Task($spec)
Start-Sleep -s 5
New-HardDisk -VM $VM -CapacityGB $DBA_ADMIN_DATA1 -DiskType Flat -StorageFormat Thin -Controller "SCSI Controller 3"
New-HardDisk -VM $VM -CapacityGB $DBAUTILITY -DiskType Flat -StorageFormat Thin -Controller "SCSI Controller 3"
}
Start-sleep -s 2
## Start VM
Start-VM -VM $VM -confirm:$false
_____________________________________________________________________________________________________________________
Any help would be appreciated.
Thank You
Not sure what you are trying to achieve with "foreach ($HardDisk in (1))" ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Copied the script from a website and modified it. I am not a scripter, just tried to tweak the code with my small scripting knowledge.
This is what I am trying to get.
Hard disk 1 on SCSI 0:1
Hard disk 2 on SCSI 0:2
Hard disk 3 on SCSI 1:1
Hard disk 4 on SCSI 1:2
Hard disk 5 on SCSI 2:1
Hard disk 6 on SCSI 2:2
Hard disk 7 on SCSI 3:1
Hard disk 8 on SCSI 3:2
Hard disk 9 on SCSI 3:3
I would really appreciate if you could help me fixing my script.
Thank You
Can anyone please help!!
Thank You
Try it with the below code
$VMname = Read-Host " Enter the VM hostname"
$D=Read-Host "Enter D drive GB Size"
$E=Read-Host "Enter E drive GB Size"
$SYSTEMDBS1 = Read-Host "Enter SYSTEMDB1 GB Size"
$DATA1 = Read-Host "Enter DATA1 GB Size"
$LOGS1 = Read-Host "Enter LOGS1 GB Size"
$DBA_ADMIN_LOGS1 = Read-Host "Enter DBA_ADMIN_LOGS1 GB Size"
$TEMPDB1 = Read-Host "Enter TEMPDB1 GB Size"
$DBA_ADMIN_DATA1 = Read-Host "Enter DBA_ADMIN_DATA1 GB Size"
$DBAUTILITY = Read-Host "Enter DBAUTILITY GB Size"
stop-vm -vm $VMname -confirm:$false
Start-sleep -s 5
$VM= get-vm -Name $VMname
##Adding D and E Drive
New-HardDisk -VM $VM -CapacityGB $D -DiskType Flat -StorageFormat Thin
New-HardDisk -VM $VM -CapacityGB $E -DiskType Flat -StorageFormat Thin
New-HardDisk -VM $VM -CapacityGB $SYSTEMDBS1 -DiskType Flat -StorageFormat Thin
New-HardDisk -VM $VM -CapacityGB $DATA1 -DiskType Flat -StorageFormat Thin
New-HardDisk -VM $VM -CapacityGB $LOGS1 -DiskType Flat -StorageFormat Thin
New-HardDisk -VM $VM -CapacityGB $DBA_ADMIN_LOGS1 -DiskType Flat -StorageFormat Thin
New-HardDisk -VM $VM -CapacityGB $TEMPDB1 -DiskType Flat -StorageFormat Thin
New-HardDisk -VM $VM -CapacityGB $DBA_ADMIN_DATA1 -DiskType Flat -StorageFormat Thin
New-HardDisk -VM $VM -CapacityGB $DBAUTILITY -DiskType Flat -StorageFormat Thin
$hdds = $VM | Get-HardDisk
New-ScsiController -Type ParaVirtual -HardDisk $hdds[2,3]
New-ScsiController -Type ParaVirtual -HardDisk $hdds[4,5]
New-ScsiController -Type ParaVirtual -HardDisk $hdds[6,7,8]
Start-sleep -s 2
## Start VM
Start-VM -VM $VM -confirm:$false
consider awarding points if you find my answer helpful.
Thanks Prakash, your script worked I see all disks on their SCSI controllers but SCSI # on the disk are not in sequence.
Hard disk 1 on SCSI 0:1
Hard disk 2 on SCSI 0:2
Hard disk 3 on SCSI 1:1
Hard disk 4 on SCSI 1:2
Hard disk 5 on SCSI 2:4
Hard disk 6 on SCSI 2:6
Hard disk 7 on SCSI 3:3
Hard disk 8 on SCSI 3:6
Hard disk 9 on SCSI 3:9
Thank YOu
four years later I run into the same "problem" the SCSI # on the controller get out of order. Did anyone resolve this "issue"?
You might give the script in Solved: Script for attaching multiple existing ... |VMware Communities a try.
Afaik, you can only achieve this when using the API method.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What's happening is when you change the SCSI controller and/or target ID of a disk, it renames it to "Hard disk [X]," where [X] is the total number of disks. It also changes the ID number. The only thing that doesn't change is the Filename property. As such, every time you modify a disk you have to re-read the disks.
For example:
$VMObj = Get-VM -Name MyVM
Get-HardDisk -VM $VMObj | Select-Object -Property Name,Filename,Id,CapacityGB
Name Filename Id CapacityGB
---- -------- -- ----------
Hard disk 1 [datastore_01] DharzhakTest/DharzhakTest.vmdk VirtualMachine-vm-864/2000 128
Hard disk 2 [datastore_01] DharzhakTest/DharzhakTest_1.vmdk VirtualMachine-vm-864/2001 11
Hard disk 3 [datastore_01] DharzhakTest/DharzhakTest_2.vmdk VirtualMachine-vm-864/2002 12
Hard disk 4 [datastore_01] DharzhakTest/DharzhakTest_3.vmdk VirtualMachine-vm-864/2003 13
Hard disk 5 [datastore_01] DharzhakTest/DharzhakTest_4.vmdk VirtualMachine-vm-864/2004 14
Hard disk 6 [datastore_01] DharzhakTest/DharzhakTest_5.vmdk VirtualMachine-vm-864/2005 15
Hard disk 7 [datastore_01] DharzhakTest/DharzhakTest_6.vmdk VirtualMachine-vm-864/2006 16
Hard disk 8 [datastore_01] DharzhakTest/DharzhakTest_7.vmdk VirtualMachine-vm-864/2008 17
Hard disk 9 [datastore_01] DharzhakTest/DharzhakTest_8.vmdk VirtualMachine-vm-864/2009 18
If we were to modify the SCSI controller of “Hard disk 9” to change it from SCSI Controller 0 to SCSI Controller 1, the names would not change, because the last modified disk was already listed last.
However, if we modify “Hard Disk 2” to change it from SCSI Controller 0 to SCSI Controller 1, it would be renamed “Hard disk 9” and the ones currently named “Hard disk (3-9)” would be renamed to “Hard disk (2-8)”, accordingly.
So, back to the task at hand. Let's assume we're starting with all the disks on a single SCSI controller:
$VMObj = Get-VM -Name MyVM
$ScsiControllerObjs = Get-ScsiController -VM $VMObj
$DiskObjs = Get-HardDisk -VM $VMObj
$DiskAssignments = @()
foreach ($DiskObj in $DiskObjs) {
$DiskAssignments += "$($DiskObj.Name) $($DiskObj.FileName) $($ScsiControllerObjs.Where({$_.Key -eq $DiskObj.ExtensionData.ControllerKey}).Name):$($DiskObj.ExtensionData.UnitNumber)"
}
Write-Output $DiskAssignments
Hard disk 1 [datastore_01] DharzhakTest/DharzhakTest.vmdk SCSI controller 0:0
Hard disk 2 [datastore_01] DharzhakTest/DharzhakTest_1.vmdk SCSI controller 0:1
Hard disk 3 [datastore_01] DharzhakTest/DharzhakTest_2.vmdk SCSI controller 0:2
Hard disk 4 [datastore_01] DharzhakTest/DharzhakTest_3.vmdk SCSI controller 0:3
Hard disk 5 [datastore_01] DharzhakTest/DharzhakTest_4.vmdk SCSI controller 0:4
Hard disk 6 [datastore_01] DharzhakTest/DharzhakTest_5.vmdk SCSI controller 0:5
Hard disk 7 [datastore_01] DharzhakTest/DharzhakTest_6.vmdk SCSI controller 0:6
Hard disk 8 [datastore_01] DharzhakTest/DharzhakTest_7.vmdk SCSI controller 0:8
Hard disk 9 [datastore_01] DharzhakTest/DharzhakTest_8.vmdk SCSI controller 0:9
If you modify them in the order of the file names, you should get the correct order. There's probably a more graceful way to do this, but you should get the gist of what's going on. This here will assign a new ParaVirtual SCSI controller to the 3rd, 5th and 7th disks and assign the subsequent disks to the new controller. After that, it uses the ReconfigVM_Task method, as needed, to specify the target ID number as the New-ScsiController and Set-HardDisk cmdlets may assign it randomly.
$VMObj = Get-VM -Name MyVM
# Put disk names in an array
$DiskNames = (Get-HardDisk -VM $VMObj).FileName
foreach ($I in 2,4,6) {
# Assign Disk to new ParaVirtual SCSI controller
$DiskObj = Get-HardDisk -VM $VMObj | Where-Object { $_.Filename -eq $DiskNames[$I] }
$ScsiControllerObj = New-ScsiController -HardDisk $DiskObj -Type ParaVirtual -BusSharingMode NoSharing -Confirm:$false
# Correct SCSI target ID from random assignment
$DiskObj = Get-HardDisk -VM $VMObj | Where-Object { $_.Filename -eq $DiskNames[$I] }
# If the Target ID is not 0, set it to 0
if ($DiskObj.ExtensionData.UnitNumber -ne 0) {
$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 = 0
$VMObj.ExtensionData.ReconfigVM_Task($NewSpec)
}
# Assign subsequent Disk to new ParaVirtual SCSI controller
$I++
Start-Sleep 5
$DiskObj = Get-HardDisk -VM $VMObj | Where-Object { $_.Filename -eq $DiskNames[$I] }
Set-HardDisk -HardDisk $DiskObj -Controller $ScsiControllerObj -Confirm:$false
# Correct SCSI target ID from random assignment
$DiskObj = Get-HardDisk -VM $VMObj | Where-Object { $_.Filename -eq $DiskNames[$I] }
# If the Target ID is not 1, set it to 1
if ($DiskObj.ExtensionData.UnitNumber -ne 1) {
$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 = 1
$VMObj.ExtensionData.ReconfigVM_Task($NewSpec)
}
#Assign the last disk to the final controller
if ($I -eq 7) {
$I++
Start-Sleep 5
$DiskObj = Get-HardDisk -VM $VMObj | Where-Object { $_.Filename -eq $DiskNames[$I] }
Set-HardDisk -HardDisk $DiskObj -Controller $ScsiControllerObj -Confirm:$false
# Correct SCSI target ID from random assignment
$DiskObj = Get-HardDisk -VM $VMObj | Where-Object { $_.Filename -eq $DiskNames[$I] }
# If the Target ID is not 2, set it to 2
if ($DiskObj.ExtensionData.UnitNumber -ne 2) {
$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 = 2
$VMObj.ExtensionData.ReconfigVM_Task($NewSpec)
}
}
}
After you run that, you can re-check the SCSI assignments and see that they're now in the desired order.
$VMObj = Get-VM -Name MyVM
$ScsiControllerObjs = Get-ScsiController -VM $VMObj
$DiskObjs = Get-HardDisk -VM $VMObj
$DiskAssignments = @()
foreach ($DiskObj in $DiskObjs) {
$DiskAssignments += "$($DiskObj.Name) $($DiskObj.FileName) $($ScsiControllerObjs.Where({$_.Key -eq $DiskObj.ExtensionData.ControllerKey}).Name):$($DiskObj.ExtensionData.UnitNumber)"
}
Write-Output $DiskAssignments
Hard disk 1 [datastore_01] DharzhakTest/DharzhakTest.vmdk SCSI controller 0:0
Hard disk 2 [datastore_01] DharzhakTest/DharzhakTest_1.vmdk SCSI controller 0:1
Hard disk 3 [datastore_01] DharzhakTest/DharzhakTest_2.vmdk SCSI controller 1:0
Hard disk 4 [datastore_01] DharzhakTest/DharzhakTest_3.vmdk SCSI controller 1:1
Hard disk 5 [datastore_01] DharzhakTest/DharzhakTest_4.vmdk SCSI controller 2:0
Hard disk 6 [datastore_01] DharzhakTest/DharzhakTest_5.vmdk SCSI controller 2:1
Hard disk 7 [datastore_01] DharzhakTest/DharzhakTest_6.vmdk SCSI controller 3:0
Hard disk 8 [datastore_01] DharzhakTest/DharzhakTest_7.vmdk SCSI controller 3:1
Hard disk 9 [datastore_01] DharzhakTest/DharzhakTest_8.vmdk SCSI controller 3:2
Hello Dharzhak,
thank you - worked like a charme.