Create a VM and do some configuration with PowerCLI

Like so many others, I’ve been trying to up my PowerCLI skills lately. I do this to make my life and that of my colleagues a bit easier and to get the skills needed to up my certification to the VCAP level šŸ™‚

Here’s some code to create a 64-bit Oracle Enterprise Linux VM with virtual hardware version 9 and without floppy. I’ll try to update this post as more settings must or can be made.

Create the VM with 7 disks.

$name = “MyVMName”
New-VM -VMHost MyESXiHost -CD -Name $name -MemoryMB 16384 -NumCPU 1 -Version v9 -GuestId oracleLinux64Guest -Datastore VMFS3 -DiskGB 10,20,30,200,200,200,10 -DiskStorageFormat Thin

Some settings which Ā are annoying to do with a mouse and also easily forgotten

$vm = Get-VM $name
New-AdvancedSetting -Entity $vm -Name ‘vcpu.hotadd’ -Value ‘true’ -Confirm:$false
New-AdvancedSetting -Entity $vm -Name ‘mem.hotadd’ -Value ‘true’ -Confirm:$false
New-AdvancedSetting -Entity $vm -Name ‘svga.autodetect’ -Value ‘true’ -Confirm:$false

This is some code to do the same if the settings have already been configured

$vm | Get-AdvancedSetting -Name ‘vcpu.hotadd’ | Set-AdvancedSetting -Value ‘true’ -Confirm:$false
$vm | Get-AdvancedSetting -Name ‘mem.hotadd’ | Set-AdvancedSetting -Value ‘true’ -Confirm:$false
$vm | Get-AdvancedSetting -Name ‘svga.autodetect’ | Set-AdvancedSetting -Value ‘true’ -Confirm:$false

Change NIC and SCSI controller type

Get-VM $vm | Get-CDDrive | Set-CDDrive -ISOPath “[VMFS1] ISO\oel_6.4_boot.iso” -StartConnected $true -Confirm:$false
Get-VM $vm | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName Vlan126 -Confirm:$False
Get-VM $vm | Get-NetworkAdapter | Set-NetworkAdapter -Type VMXNet3 -Confirm:$False
Get-VM $vm | Get-ScsiController | Set-ScsiController -Type ParaVirtual

Add the last 4 disks to a seperate SCSCI controller

$disks = Get-Harddisk -VM $vm | Select -Last 4
New-ScsiController -Type ParaVirtual -Harddisk $disks

Start the VM

Start-VM $vm

That’s it for now. Comments are welcome.


About Yuri de Jager
Technology Addict

One Response to Create a VM and do some configuration with PowerCLI

  1. gregcarriger says:

    Thank you for the examples. I didn’t realize I had to get-advancedsetting and pipe it to set-advancedsetting. I was just trying to set-advancedsetting from the getgo.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: