diff --git a/scripts/Update-OvfConfiguration.ps1 b/scripts/Update-OvfConfiguration.ps1 index 5d8e705..68f1ae3 100644 --- a/scripts/Update-OvfConfiguration.ps1 +++ b/scripts/Update-OvfConfiguration.ps1 @@ -53,31 +53,34 @@ $NS = [System.Xml.XmlNamespaceManager]$XML.NameTable ForEach ($ReferencedDisk in $OVFConfig.Disks.Referenced) { $XMLDisk = $XML.SelectSingleNode("//ns:DiskSection/ns:Disk[@ovf:diskId='$($ReferencedDisk.Id)']", $NS) If ($XMLDisk.Count -eq 1) { - Switch ($ReferencedDisk.UnitSize) { - 'KB' { - $CapacityInBytes = $ReferencedDisk.Capacity * 1KB - } - 'MB' { - $CapacityInBytes = $ReferencedDisk.Capacity * 1MB - } - 'GB' { - $CapacityInBytes = $ReferencedDisk.Capacity * 1GB - } - 'TB' { - $CapacityInBytes = $ReferencedDisk.Capacity * 1TB - } - 'PB' { - $CapacityInBytes = $ReferencedDisk.Capacity * 1PB - } - default { - # Invalid UnitSize; skipping modification of existing disk - Continue - } + $FactorMap = @{ + 'KB' = [Math]::Pow(2,10) + 'MB' = [Math]::Pow(2,20) + 'GB' = [Math]::Pow(2,30) + 'TB' = [Math]::Pow(2,40) + 'PB' = [Math]::Pow(2,50) } - # Determine if specified size is equal or larger than allocated size - If ($XMLDisk.populatedSize -le $CapacityInBytes) { - [void]$XMLDisk.SetAttribute('ovf:capacity', $CapacityInBytes) + If (($FactorMap.Keys -contains $ReferencedDisk.UnitSize)) { + # Convert new capacity to same scale as existing capacity + If ($XMLDisk.capacityAllocationUnits -match 'byte \* 2\^(\d+)') { + $NewSize = [Math]::Truncate($ReferencedDisk.Capacity * $FactorMap[$ReferencedDisk.UnitSize] / [Math]::Pow(2,$Matches[1])) + } + ElseIf ($XMLDisk.capacityAllocationUnits -eq 'byte') { + $NewSize = $ReferencedDisk.Capacity * $FactorMap[$ReferencedDisk.UnitSize] + } + Else { + Throw "Unexpected value '$($XMLDisk.capacityAllocationUnits)' for property 'capacityAllocationUnits'; aborting." + } + # Determine if specified capacity is equal or larger than current & allocated capacity + If ($XMLDisk.populatedSize -le $NewSize -and $XMLDisk.capacity -le $NewSize) { + [void]$XMLDisk.SetAttribute('ovf:capacity', $NewSize) + Write-Host "Updated 'Disk' with new property value" + } + } + Else { + # Invalid UnitSize; skipping modification of existing disk + Continue } } } @@ -128,6 +131,8 @@ ForEach ($DynamicDisk in $OVFConfig.Disks.Dynamic) { [void]$XMLDisk.Attributes.Append($XMLDiskAttrCapacity) [void]$XMLDisk.Attributes.Append($XMLDiskAttrPopulated) + [void]$XML.SelectSingleNode('//ns:DiskSection', $NS).AppendChild($XMLDisk) + $OVFConfig.PropertyCategories[0].ProductProperties += @{ Key = "vmconfig.disksize.$($DiskId)" Type = If ([boolean]$DynamicDisk.Constraints.Minimum -or [boolean]$DynamicDisk.Constraints.Maximum) { @@ -143,6 +148,7 @@ ForEach ($DynamicDisk in $OVFConfig.Disks.Dynamic) { UserConfigurable = 'true' } } +Write-Host "Inserted $($OVFConfig.Disks.Dynamic.Count) new nodes into 'DiskSection'" If ($OVFConfig.DeploymentConfigurations.Count -gt 0) { $XMLSection = $XML.CreateElement('DeploymentOptionSection', $XML.DocumentElement.xmlns)