2018-05-24 16:57:55 +00:00
|
|
|
#Requires -Modules VagrantMessages
|
|
|
|
|
|
|
|
param(
|
2014-02-16 19:31:33 +00:00
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
[string]$VmId
|
|
|
|
)
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2016-06-19 00:13:24 +00:00
|
|
|
# Make sure the exception type is loaded
|
|
|
|
try
|
|
|
|
{
|
|
|
|
# Microsoft.HyperV.PowerShell is present on all versions of Windows with HyperV
|
|
|
|
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.HyperV.PowerShell, Culture=neutral, PublicKeyToken=31bf3856ad364e35')
|
|
|
|
# Microsoft.HyperV.PowerShell.Objects is only present on Windows >= 10.0, so this will fail, and we ignore it since the needed exception
|
|
|
|
# type was loaded in Microsoft.HyperV.PowerShell
|
|
|
|
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.HyperV.PowerShell.Objects, Culture=neutral, PublicKeyToken=31bf3856ad364e35')
|
|
|
|
} catch {
|
|
|
|
# Empty catch ok, since if we didn't load the types, we will fail in the next block
|
|
|
|
}
|
|
|
|
|
|
|
|
$VmmsPath = if ([environment]::Is64BitProcess) { "$($env:SystemRoot)\System32\vmms.exe" } else { "$($env:SystemRoot)\Sysnative\vmms.exe" }
|
|
|
|
$HyperVVersion = [version](Get-Item $VmmsPath).VersionInfo.ProductVersion
|
2016-03-06 16:51:27 +00:00
|
|
|
|
2016-06-19 00:13:24 +00:00
|
|
|
if($HyperVVersion -lt ([version]'10.0')) {
|
2016-03-06 16:51:27 +00:00
|
|
|
$ExceptionType = [Microsoft.HyperV.PowerShell.VirtualizationOperationFailedException]
|
|
|
|
} else {
|
|
|
|
$ExceptionType = [Microsoft.HyperV.PowerShell.VirtualizationException]
|
|
|
|
}
|
2014-02-15 23:29:16 +00:00
|
|
|
try {
|
2017-08-08 17:00:29 +00:00
|
|
|
$VM = Hyper-V\Get-VM -Id $VmId -ErrorAction "Stop"
|
2014-02-16 19:31:33 +00:00
|
|
|
$State = $VM.state
|
|
|
|
$Status = $VM.status
|
2016-06-19 00:13:24 +00:00
|
|
|
} catch [Exception] {
|
|
|
|
if($_.Exception.GetType() -eq $ExceptionType)
|
|
|
|
{
|
|
|
|
$State = "not_created"
|
|
|
|
$Status = $State
|
|
|
|
}
|
2018-05-24 16:57:55 +00:00
|
|
|
else
|
2016-06-19 00:13:24 +00:00
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|
2014-02-16 19:31:33 +00:00
|
|
|
|
|
|
|
$resultHash = @{
|
|
|
|
state = "$State"
|
|
|
|
status = "$Status"
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|
2014-02-16 19:31:33 +00:00
|
|
|
$result = ConvertTo-Json $resultHash
|
2018-06-12 22:32:29 +00:00
|
|
|
Write-OutputMessage $result
|