PowerShell Friday: Creating Virtual Machines
One of the most basic things you will do within a virtual infrastructure is creating virtual machines. Adding one new virtual machine like this is fine, but if you have to repeat this proces multiple times it might be more usefull to use a script containing the PowerCLI cmdlet New-VM.
You can use the New-VM cmdlet in conjuction with templates and customization specifications, which can also be created through the use of PowerCLI. Those cmdlets will be handled in articles yet to come.
There are a lot of parameters that can be used with the New-VM cmdlet but at the very least you should enter a virtual machine name and cluster / resourcepool.
New-VM -Name 'VMname' -ResourcePool 'ResourcePool'
If you also want to specify the location of the VDMK files you can add the Datastore parameter
New-VM -Name 'VMname' -ResourcePool 'ResourcePool' -Datastore 'Datastore'
When you want to clone a virtual machine you can add the “-VM” parameter
New-VM -Name 'VMname' -VM 'VMtoClone' -ResourcePool 'ResourcePool'
To import a virtual machine you will need to first get the folder name on a specific datastore and load that into a variable. You can then use that variable together with the new-vm cmdlet to import a virtual machine
cd vmstores:\MYserver@443\Datacenter\Datastore\MYVirtualMachine\ $vmxFile = Get-Item 'MyVirtualMachine.vmx' $vmhost = Get-VMHost -Name 'MyVMHost' New-VM -VMHost $vmhost -VMFilePath $vmxFile.DatastoreFullPath
By default the New-VM cmlet will create a virtual machine that contains 1 vCPU, 256 MB memory and a 4GB disk and a NIC connected at “internal network”. To change this you can add parameters like this.
New-VM -Name 'VMname' -ResourcePool 'ResourcePool' -NumCpu 4 -MemoryMB 2048 -DiskMB 40960 -NetworkName 'production network'
As mentioned there are a lot of parameters you can use and the above examples only contain about half. I won’t write them all out in detail, instead I will give a summation for the rest of the parameters and a description.
|VMhost||Specifies a vSphere host to place the virtual machine on|
|Template||Enables you to create a virtual machine based of a template|
|AlternateGuestName||Allows you to change the guest OS name. Only to be used when you use the parameter “otherGuest” or “otherGuest64”.|
|CD||Add a CD drive to the guest or not|
|Confirm||Whether the cmdlet should ask for confirmation|
|Description||Adding a description in the notes field of a virtual machine|
|DiskStorageFormat||Specify the virtual disk format Thin or Thick|
|DrsAutomationLevel||Specifies the level of DRS automation applied to the virtual machine|
|Floppy||Add a Floppy drive to the guest or not|
|GuestID||What will the OS be, each OS has it’s own code. For reference you can look at this site|
|HAIsolationResponse||Indicate the HA response for the virtual machine in case of host isolation|
|HARestartPriority||Specify the HA restart priority of the virtual machine in case a host fails|
|Location||Folder placement of the virtual machine|
|OSCustomizationSpec||Specify the customization specification to be used|
|VApp||Places the virtual machine within a VApp|
|Version||Defines the hardware version the virtual machine will be using|
|VMSwapfilePolicy||Specify the swap file policy that is used for the virtual machine|
Other articles in the series PowerShell Friday:
- PowerShell Friday: Getting Started with PowerShell and PowerCLI
- PowerShell Friday: Connecting to vCenter
- PowerShell Friday: Starting VMs
- PowerShell Friday: stopping VMs
- PowerShell Friday: Creating Virtual Machines
- PowerShell Friday: Snapshots
- PowerShell Friday: Adding CPU’s with PowerCLI
- PowerShell Friday: Adding Memory with PowerCLI
- PowerShell Friday: ExtensionData
- PowerShell Friday: Retrieving IP addresses for VMs
- PowerShell Friday: Copying files with Copy-VMGuestFile
- PowerShell Friday: Setting Reservations with PowerCLI
- PowerShell Friday: Enabling SSH with PowerCLI
- PowerShell Friday: Christmas Special
- PowerShell Friday: Configuring vSphere MTU Size
- PowerShell Friday: Load PowerCLI from your own script
- PowerShell Friday: Using the Cisco ACI API
- Onboard existing workloads in Cloud Automation Services by Erik Scholten
- Getting started with Photon OS by Anne Jan Elsinga
- Customizing your CAS deployment using Cloud-Init by Erik Scholten
- How to use remote access authentication in your… by Erik Scholten
- How to install HyTrust KeyControl 5.1 in vSphere 6.7 by Edwin Weijdema