System Center Virtual Machine Manager
A family of System Center products that enable enterprise-wide management of virtual machines.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
When creating a new VM with Powershell, is there any way to catch errors?
New-VM -Name $vmname `
-MemoryStartupBytes $memorySize `
-Path D:\Hyper-V\ `
-NewVHDPath D:\Hyper-V\$vmname\$vmname.vhdx `
-NewVHDSizeBytes $diskSize `
-Generation 2 `
-SwitchName "vSwitch"
For example, an error can occur if the virtual switch does not exist. In this case, no more tasks should be processed and the script should be terminated.
The New-VM cmdlet recognizes all "Common-Parameters", so adding "-ErrorAction STOP" to the cmdlets' parameters and wrapping the cmdlet in a Try/Catch will allow you trigger a terminating exception for an errors encountered during the cmdlets' execution.
Try{
New-VM -Name $vmname
-MemoryStartupBytes $memorySize
-Path D:\Hyper-V\
-NewVHDPath D:\Hyper-V\$vmname\$vmname.vhdx
-NewVHDSizeBytes $diskSize
-Generation 2
-SwitchName "vSwitch"
-ErrorAction STOP
}
Catch{
# Report or handle the error here
}