From 0a7c20a93a7fb80ddfc8d37474bec4805ccd87ae Mon Sep 17 00:00:00 2001 From: Jon Topper Date: Sun, 6 Mar 2016 16:51:27 +0000 Subject: [PATCH] Determine status behaviour based on PS version --- plugins/providers/hyperv/scripts/get_vm_status.ps1 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/providers/hyperv/scripts/get_vm_status.ps1 b/plugins/providers/hyperv/scripts/get_vm_status.ps1 index 5c1e1aa73..761b9867d 100644 --- a/plugins/providers/hyperv/scripts/get_vm_status.ps1 +++ b/plugins/providers/hyperv/scripts/get_vm_status.ps1 @@ -7,12 +7,18 @@ Param( $Dir = Split-Path $script:MyInvocation.MyCommand.Path . ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1")) -# Get the VM with the given name + +if($PSVersionTable.PSVersion.Major -le 4) { + $ExceptionType = [Microsoft.HyperV.PowerShell.VirtualizationOperationFailedException] +} else { + $ExceptionType = [Microsoft.HyperV.PowerShell.VirtualizationException] +} + try { $VM = Get-VM -Id $VmId -ErrorAction "Stop" $State = $VM.state $Status = $VM.status -} catch [Microsoft.HyperV.PowerShell.VirtualizationException] { +} catch $ExceptionType { $State = "not_created" $Status = $State }