# encoding: utf-8
# api: powershell
# title: Error handling PowerCLI
# description: Error handling with PowerCLI 4.1
# version: 0.1
# author: Yasen Kalchev
# license: CC0
# x-poshcode-id: 2017
# x-archived: 2010-07-26T15:33:39
#
#
$vmPath = "[Storage1] MyVM/MyVM.vmx"
$vm = New-VM �VMHost "192.168.1.10" �VMFilePath $vmPath -Name MyVM
# Check if there is an error and if so � handle it
if (!$?) {
if ($error[0].Exception �is [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.DuplicateName]) {
# A VM with the same name already exists. Change the name and try again.
$vm = New-VM �VMHost "192.168.1.10" �VMFilePath $vmPath �Name "myVM (1)"
} elseif ($error[0].Exception �is [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InsufficientResourcesFault]) {
# There aren�t enough resources on this host. Try again on another host.
$vm = New-VM �VMHost "192.168.1.12" �VMFilePath $vmPath
} else {
# Handle unexpected exceptions here
}
}