Although its unlikely that many will want to create a clean blank virtual machine using PowerCLI. It is indeed possible – but administrator may find more use from creating new VMs from existing ones or from templates.
Creating a VM requires knowing what GuestID value to use for the corresponding operating system – this is referred to as the VirtualMachineGuestOsIdentifier. A list of VirtualMachineGuestOsIdentifier’s which maps the GuestID parameter to the friendly name is available in the SDK documentation: VirtualMachineGuestOsIdentifier. For instance Microsoft Windows 2012 R2 is known as “windows8Server64Guest”.
Creating a Virtual Machine:
The first line of this PowerCLI creates a new Windows 2012 R2 VM called CorpHQApp01 on the GoldCluster01 using the datastore called Bronze. The VM is allocated 2-vCPUs, 4GB RAM and thin virtual disk of 40GB in size connected to the portgroup called VLAN101. The second line adds the CD-Rom Device and connects the win2012R2.iso held on the software datastore.
New-VM -Name CorpHQApp01 -ResourcePool GoldCluster01 -Datastore Bronze -NumCPU 2 -MemoryMB 4096 -DiskMB 40000 -DiskStorageFormat Thin -GuestID “windows8Server64Guest” -NetworkName “VLAN101”
$cd = New-CDDrive -VM CorpHQApp01 -ISOPath “[software] microsoft/win2012R2.iso” -StartConnected
Start-VM -VM CorpHQApp01
List and Disconnect VMs with Connected CD-ROM Device
Typical vCenter Admins will connect up CD-ROM Devices, and then forget to disconnect them. The CD-ROM Device isn’t a truly virtualized device and it can be drain on resources, and cause problems elsewhere.
This PowerCLI one-liner will list all the VMs that have CD-ROM Device Connected. The CD-ROM is not regarded as “connected” if a VM is configured for it – but the VM is powered off. So only powered on VMs, which an actively connected CD-ROM Device will be found with this PowerCLI script.
Get-VM | where { $_ | Get-CDdrive | where { $_.ConnectionState.Connected -eq “true” } } | select Name
This PowerCLI one-liner will list all the VMs which have path set for either datastore ISO or the physical DVD-Drive of the VMware ESXi host.
Get-VM | where { $_ | Get-CDdrive | where { $_.IsoPath.Length -gt 0 -OR $_.HostDevice.Length -gt 0 } } | select Name
This PowerCLI two-liner disconnects any CD-ROM Device found to be connected, and then unconfigures any VM set to use an ISO file or the host’s physical DVD Device.
Get-VM | Get-CDDrive | Where {$_.ConnectionState.Connected} | Set-CDDrive -connected 0 -StartConnected 0 -Confirm:$false
Get-VM | Get-CDDrive | Where { $_.IsoPath.Length -gt 0 -OR $_.HostDevice.Length -gt 0 } | Set-CDDrive -NoMedia -Confirm:$False
List VMs which require a VMware Tools upgrade:
Using the ExtensionData.Guest.ToolsStatus value we can query the state of VMware Tools on the VMs.
Write-Host “================================================================”
Write-Host “These VMs need VMware Tools updating”
Write-Host “================================================================”
Get-VM | Where-Object {$_.ExtensionData.Guest.ToolsStatus -eq “toolsOld”}
Write-Host “================================================================”
Write-Host “These VMs are powered on, and do not have VMware Tools installed”
Write-Host “================================================================”
Get-VM | where {$_.powerstate -match “On”} | Where-Object {$_.ExtensionData.Guest.ToolsStatus -eq “ToolsNotInstalled”}
Enable Time Synchronisation and Upgrade Tools on Power On:
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = “UpgradeAtPowerCycle”
$vmConfigspec.Tools.syncTimeWithHost = $true
Get-VM | %{ $_.Extensiondata.ReconfigVM($vmConfigSpec) }