Skip to content

Commit 930a120

Browse files
committed
Error handling in case Hyper-V VM Notes field is not readable as XML.
1 parent c2ef6f8 commit 930a120

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

AutomatedLab/AutomatedLab.psm1

+5-1
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,11 @@ function Remove-Lab
12171217
{
12181218
$machineMetadata = Get-LWHypervVMDescription -ComputerName $machine -ErrorAction SilentlyContinue
12191219
$vm = Get-VM -Name $machine -ErrorAction SilentlyContinue
1220-
if ($machineMetadata.LabName -ne $labName -and $vm)
1220+
if (-not $machineMetadata)
1221+
{
1222+
Write-Error -Message "Cannot remove machine '$machine' because lab meta data could not be retrieved"
1223+
}
1224+
elseif ($machineMetadata.LabName -ne $labName -and $vm)
12211225
{
12221226
Write-Error -Message "Cannot remove machine '$machine' because it does not belong to this lab"
12231227
}

AutomatedLabWorker/AutomatedLabWorkerVirtualMachines.psm1

+10-4
Original file line numberDiff line numberDiff line change
@@ -1684,10 +1684,16 @@ function Get-LWHypervVMDescription
16841684

16851685
$type = Get-Type -GenericType AutomatedLab.DictionaryXmlStore -T String,String
16861686

1687-
$importMethodInfo = $type.GetMethod('ImportFromString', [System.Reflection.BindingFlags]::Public -bor [System.Reflection.BindingFlags]::Static)
1688-
$dictionary = $importMethodInfo.Invoke($null, $vm.Notes)
1689-
1690-
$dictionary
1687+
try
1688+
{
1689+
$importMethodInfo = $type.GetMethod('ImportFromString', [System.Reflection.BindingFlags]::Public -bor [System.Reflection.BindingFlags]::Static)
1690+
$dictionary = $importMethodInfo.Invoke($null, $vm.Notes)
1691+
$dictionary
1692+
}
1693+
catch
1694+
{
1695+
Write-ScreenInfo -Message "The notes field of the virtual machine '$ComputerName' could not be read as XML" -Type Warning
1696+
}
16911697

16921698
Write-LogFunctionExit
16931699
}

0 commit comments

Comments
 (0)