Powercli and new virtual machine script for a friend

Okay just for a bit of fun lets see how easy it is to create a machine from powercli and get it complex and more complex as in build it to a colossal beast..

vmhost = bob
cpu = 2
memory = 2gb
disk 1 = 10GB
disk 2 = 10GB
disk 3 = 10GB
virtual guest = ralph
datastore cluster = belinda
datastore = sally
vdswitch = rastus
network vdportgroup name = irene

Typicals..
As always we need to connect to the host or vcenter.
so.
A. connect-viserver bob

Basic

New-vm -vmhost bob -datastore sally -DiskMB 10240,10240,10240 -diskstorageformat thin -Numcpu 2 -memorygb 2 -GuestID freebsd64Guest -notes "basic guest automation by whackdiddy" -confirm:$false

More than basic (remove nic and replace with vmxnet3 nic)

New-vm -vmhost bob -datastore sally -DiskMB 10240,10240,10240 -diskstorageformat thin -Numcpu 2 -memorygb 2 -GuestID freebsd64Guest -notes "basic guest automation by whackdiddy"; sleep 15; get-networkadapter -vm bob | remove-networkadapter -confirm:$false; sleep 15;New-Networkadapter -vm bob -type vmxnet3 -portgroup irene -StartConnected -confirm:$false

Adding in a cdrom

New-vm -vmhost bob -datastore sally -DiskMB 10240,10240,10240 -diskstorageformat thin -Numcpu 2 -memorygb 2 -GuestID freebsd64Guest -notes "basic guest automation by whackdiddy"; sleep 15; get-networkadapter -vm bob | remove-networkadapter -confirm:$false;New-cddrive -vm bob;New-Networkadapter -vm bob -type vmxnet3 -portgroup irene -StartConnected -confirm:$false

Removing the floppy drive

New-vm -vmhost bob -datastore sally -DiskMB 10240,10240,10240 -diskstorageformat thin -Numcpu 2 -memorygb 2 -GuestID freebsd64Guest -notes "basic guest automation by whackdiddy"; sleep 15; get-networkadapter -vm bob | remove-networkadapter -confirm:$false;New-cddrive -vm bob; get-vm bob | get-floppydrive | remove-floppydrive; sleep 15;New-Networkadapter -vm bob -type vmxnet3 -portgroup irene -StartConnected -confirm:$false

Finding a datastore – I did this in excel so I could calculate the amount of space needed (gt = Greater Than), this will list all the datastores that have more than 30720 space free, the -last 1 command does exactly that selects the last one. .So you could do a sort by command to select the most disk space free or the smallest amount of diskspace free.

New-vm -vmhost bob -datastore (get-datastorecluster belinda | get-datastore | where {$_.freespacemb -gt 30720} |select -last 1) -DiskMB 10240,10240,10240 -diskstorageformat thin -Numcpu 2 -memorygb 2 -GuestID freebsd64Guest -notes "basic guest automation by whackdiddy"; sleep 15; get-networkadapter -vm bob | remove-networkadapter -confirm:$false;New-cddrive -vm bob; get-vm bob | get-floppydrive | remove-floppydrive; sleep 15;New-Networkadapter -vm bob -type vmxnet3 -portgroup irene -StartConnected -confirm:$false

Too many datacenters and too many vlans that are similar so lets narrow the portgroup down..

New-vm -vmhost bob -datastore (get-datastorecluster belinda | get-datastore | where {$_.freespacemb -gt 30720} |select -last 1) -DiskMB 10240,10240,10240 -diskstorageformat thin -Numcpu 2 -memorygb 2 -GuestID freebsd64Guest -notes "basic guest automation by whackdiddy"; sleep 15; get-networkadapter -vm bob | remove-networkadapter -confirm:$false;New-cddrive -vm bob; get-vm bob | get-floppydrive | remove-floppydrive; sleep 15;New-Networkadapter -vm bob -type vmxnet3 -portgroup (get-vdswitch -name rastus | get-vdportgroup -name irene) -StartConnected -confirm:$false

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s