PoshCode Archive  Artifact [d7b403525a]

Artifact d7b403525a82cacd01d4084774716e78bfe808415e715ad42c1a4458db5825bd:

  • File Error-handling-PowerCLI.ps1 — part of check-in [78322f13a3] at 2018-06-10 13:04:07 on branch trunk — Error handling with PowerCLI 4.1 (user: Yasen Kalchev size: 990)

# 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 

    }
}