Einige PowerShell Beispiele zur VMware PowerCLI
# https://www.vmware.com/support/developer/PowerCLI/
# Import module
Import-Module VMware.VimAutomation.Core
# Connect to vCenter
Connect-VIServer -Server 'vcenter.mydomain.local' -Force -ErrorAction Stop -WarningAction SilentlyContinue
# List all VMs
Get-VM
# Retrive some info from VM
Get-VM -Name 'TestVM01'
# Query VMware Tools Version from all VMs
Get-VM | Get-VMGuest | Select VMName, ToolsVersion
# Test if VM exists
if (Get-VM -Name 'TestVM01' -ea SilentlyContinue) { Write-Host "VM found!" } else { Write-Host "VM NOT found!" }
# Create a new empty VM with some parameters
New-VM -Name 'TestVM01' -ResourcePool 'Host01' -Datastore 'Datastore01' -NumCPU 2 -MemoryGB 4 -DiskGB 40 -NetworkName 'Network01' -Floppy -CD -DiskStorageFormat Thin -GuestID winNetDatacenterGuest
# Create a temporary OS customization specification (for use in the next example)
$OSCustomSpec = New-OSCustomizationSpec -OStype Windows -NamingScheme VM -ChangeSid -Workgroup 'Workgroup' -AdminPassword 'Password' -OrgName 'Company Name' -FullName 'Company Name' -ProductKey 'AAAAA-BBBBB-CCCCC-DDDDD-EEEEE' -GuiRunOnce 'C:\temp\start.cmd' -AutoLogonCount 1 -TimeZone 110
# Create a new VM via cloning from template with OS customization specification
# Note: The parameter ResourcePool can be used te specify a ResourcePool, Clustoer or Host
New-VM -Name 'TestVM01' -ResourcePool 'Host01' -Template $(Get-Template 'TemplateVM01') -OSCustomizationSpec $OSCustomSpec -Location 'Folder01\Folder02' -Datastore 'Datastore01'
# Change disk size/CPU/Memory
Get-HardDisk -VM 'TestVM01' | Set-HardDisk -CapacityGB 100 -Confirm:$false
Set-VM -VM 'TestVM01' -NumCpu 2 -Confirm:$false
Set-VM -VM 'TestVM01' -MemoryGB 4 -Confirm:$false
# Add/Remove CD-ROM drive/FlopyDrive
New-CDDrive -VM 'TestVM01' -StartConnected:$true
Get-CDDrive 'TestVM01' | Remove-CDDrive -Confirm:$false
New-FloppyDrive -VM 'TestVM01' -StartConnected:$true
Get-FloppyDrive 'TestVM01' | Remove-FloppyDrive -Confirm:$false
# Advanced settings
New-AdvancedSetting -Entity 'TestVM01' -Name isolation.tools.copy.disable -Value true -Confirm:$false -force
New-AdvancedSetting -Entity 'TestVM01' -Name isolation.tools.paste.disable -Value true -Confirm:$false -force
New-AdvancedSetting -Entity 'TestVM01' -Name isolation.tools.dnd.disable -Value true -Confirm:$false -force
# Get PowerState, PowerOff, PowerOn, etc.
(Get-VM 'TestVM01').PowerState
Stop-VM -VM 'TestVM01' -Confirm:$false
Start-VM -VM 'TestVM01' -Confirm:$false
# Remove VM
Remove-VM -VM 'TestVM01' -DeletePermanently -Confirm:$false -ErrorAction Stop
# Get network adapter details
Get-VM 'TestVM01' | Get-NetworkAdapter
# Remove existing network adapter, add a new Vmxnet3 NIC to a specific network and with static MAC address
Get-VM 'TestVM01' | Get-NetworkAdapter | Remove-NetworkAdapter -Confirm:$false
Get-VM 'TestVM01' | New-NetworkAdapter -NetworkName 'Network01' -StartConnected:$true -Confirm:$false -Type Vmxnet3
Set-VMMACAddress -VMname 'TestVM01' -MACAddress '00:50:56:88:88:88' -ErrorAction Stop