GhislainL
Contributor
Contributor

VSphere: How to create a vm with IDE disks using powercli

I'm looking for a powercli script to create a VM with multiple IDE disks. It seams the "new-VM creates disks in SCSI mode...

Thanks.

0 Kudos
6 Replies
LucD
Leadership
Leadership

You will have to use the API, see Re: How to create an IDE hard disk instead of SCSI?


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

GhislainL
Contributor
Contributor

I have read that article... for 1 disk is working just fine but how about 2 or 3 more disks?

0 Kudos
LucD
Leadership
Leadership

You could run that script 2 or 3 times.


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

0 Kudos
GhislainL
Contributor
Contributor

Here is my script:

Connect-VIServer –Server 'vcentersrv01'

$vmName = "vm-iso-creator2"

$hdSize_os = 50 * 1GB

$hdSize_app = 50 * 1GB

$hdSize_arch = 50 * 1GB

$hdSize_db = 100 * 1GB

$hdSize_vip = 50 * 1GB

new-vm -name $vmName -vmhost 'esx14' -datastore 'ant_esxds01' -diskGB 10 -cd  -MemoryGB 16 -numcpu 2 -networkname 'VM Network'

$spec = New-Object -Type VMware.Vim.VirtualMachineConfigSpec -Property @{"NumCoresPerSocket" = 2}

(get-vm $vmName).ExtensionData.ReconfigVM_Task($spec)

get-vm -name $vmName | get-cddrive | set-cddrive -isopath "[ant_esx_iso01] ISO\RHEL76.iso" -StartConnected $true -confirm:$false

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo

$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"

(get-vm $vmName).ExtensionData.ReconfigVM_Task($vmConfigSpec)

#IDE DISK

$vm = Get-VM -Name $vmName

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

# Check if there is an IDE COntroller present

$ideCtrl = $vm.ExtensionData.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualIDEController"} | select -First 1

if(!$ideCtrl){

    $ctrl = New-Object VMware.Vim.VirtualDeviceConfigSpec

    $ctrl.Operation = "add"

    $ctrl.Device = New-Object VMware.Vim.VirtualIDEController

    $ideKey = -1

    $ctrl.Device.ControllerKey = $ideKey

    $spec.deviceChange += $ctrl

}

else{

    $ideKey = $ideCtrl.Key

}

# Remove SCSI disk

get-harddisk -vm $vmName | remove-harddisk -confirm:$false -deletepermanently

# Get next harddisk number

$hdNr = 0

$vm.ExtensionData.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} | %{

    $nr = [int]$_.DeviceInfo.Label.Split(' ')[2]

    if($hdNr -lt $nr){

       $hdNr = $nr    }

if($hdNr -eq 0){$hdNrStr = ""}

else{$hdNrStr = "_" + $hdNr}

# Get datastore

$dsName = $vm.ExtensionData.Config.Files.VmPathName.Split(']')[0].TrimStart('[')

#

# Add IDE harddisk for OS

$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

$dev.FileOperation = "create"

$dev.Operation = "add"

$dev.Device = New-Object VMware.Vim.VirtualDisk

$dev.Device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

$dev.Device.backing.Datastore = (Get-Datastore -Name $dsName).Extensiondata.MoRef

$dev.Device.backing.DiskMode = "persistent"

$dev.Device.Backing.FileName = "[" + $dsName + "] " + $vmName + "/" + $vmName + $hdNrStr + ".vmdk"

$dev.Device.backing.ThinProvisioned = $true

$dev.Device.CapacityInKb = $hdSize_os / 1KB

$dev.Device.ControllerKey = $ideKey

$dev.Device.UnitNumber = -1

$spec.deviceChange += $dev

$vm.ExtensionData.ReconfigVM($spec)

#

# Add IDE harddisk for APP

$hdNrStr = "_2"

$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

$dev.FileOperation = "create"

$dev.Operation = "add"

$dev.Device = New-Object VMware.Vim.VirtualDisk

$dev.Device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

$dev.Device.backing.Datastore = (Get-Datastore -Name $dsName).Extensiondata.MoRef

$dev.Device.backing.DiskMode = "persistent"

$dev.Device.Backing.FileName = "[" + $dsName + "] " + $vmName + "/" + $vmName + $hdNrStr + ".vmdk"

$dev.Device.backing.ThinProvisioned = $true

$dev.Device.CapacityInKb = $hdSize_app / 1KB

$dev.Device.ControllerKey = $ideKey

$dev.Device.UnitNumber = -1

$spec.deviceChange += $dev

$vm.ExtensionData.ReconfigVM($spec)

When running it, the VM gets created but with only one disk.... When trying to create th 2nd disk, I'm getting an error saying already exist.  But I have set hdNrStr = "_2" and the first disk has "_1"

0 Kudos
LucD
Leadership
Leadership

That might be because the same UnitNumber is used for both HD.
Can you try with different numbers?
I'll do some testing on my side as well.


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

0 Kudos
GhislainL
Contributor
Contributor

OK, I have found my issue. First, only 2 devices per IDE controller... 2nd, I was trying to install RHEL and I was not able to do it with scsi controller when the vm was created from the command line. So I thought it was because of the type of disk... but if I create the vm from the web interface... it's working fine. So I compare both VM to find out the from the command line, the type os SCSI controller was different & preventing the RHEL installation of working because no disk was found. So I change the SCSI controller type to "paravirtual" and BINGO... it's working now.

Thanks for your help!!!

0 Kudos