From 074865ab6111d0ef730b8a11375e38f3618d5ba1 Mon Sep 17 00:00:00 2001 From: djpbessems Date: Wed, 12 May 2021 15:25:29 +0200 Subject: [PATCH] Housekeeping wrt comments;Several code optimizations --- scripts/Update-OvfConfiguration.ps1 | 55 +++++++++++++---------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/scripts/Update-OvfConfiguration.ps1 b/scripts/Update-OvfConfiguration.ps1 index 7d48b7e..f6ce773 100644 --- a/scripts/Update-OvfConfiguration.ps1 +++ b/scripts/Update-OvfConfiguration.ps1 @@ -50,53 +50,46 @@ $NS = [System.Xml.XmlNamespaceManager]$XML.NameTable [void]$NS.AddNamespace('ns', $XML.DocumentElement.xmlns) [void]$NS.AddNamespace('ovf', $XML.DocumentElement.ovf) [void]$NS.AddNamespace('rasd', $XML.DocumentElement.rasd) +[void]$NS.AddNamespace('vmw', $XML.DocumentElement.vmw) -# Create copy of default resourceType=17 (=Harddisk) node +# Create copy of existing 'Item/ResourceType'=17 (=Hard disk) node $XMLDiskTemplate = $XML.SelectSingleNode("//ns:VirtualHardwareSection/ns:Item/rasd:ResourceType[.='17']", $NS).ParentNode.CloneNode($True) ForEach ($Disk in $OVFConfig.DynamicDisks) { - # Determine next free available diskId + # Determine next free available 'diskId' $XMLDisks = $XML.SelectNodes("//ns:DiskSection/ns:Disk[contains(@ovf:diskId,'vmdisk')]", $NS) - $DiskId = $XMLDisks.Count + 1 + $DiskId = 1 While ($XMLDisks.DiskId -contains "vmdisk$($DiskId)") { $DiskId++ } + # Add new 'Disk' node (under 'DiskSection') $XMLDisk = $XML.CreateElement('Disk', $XML.DocumentElement.xmlns) - Switch ($Disk.UnitSize) { - 'KB' { - $Powers = 10 - } - 'MB' { - $Powers = 20 - } - 'GB' { - $Powers = 30 - } - 'TB' { - $Powers = 40 - } - 'PB' { - $Powers = 50 - } - default { - # Invalid UnitSize; skipping modification of existing disk - Continue - } + $PowersMap = @{ + KB = 10 + MB = 20 + GB = 30 + TB = 40 + PB = 50 } - [void]$XMLDisk.SetAttribute('capacityAllocationUnits', $NS.LookupNamespace('ovf'), "byte * 2^$($Powers)") + If ($PowersMap.Keys -notcontains $Disk.UnitSize) { + # Invalid UnitSize; skipping adding new disk + Continue + } + + [void]$XMLDisk.SetAttribute('capacityAllocationUnits', $NS.LookupNamespace('ovf'), "byte * 2^$($PowersMap[$Disk.UnitSize])") [void]$XMLDisk.SetAttribute('format', $NS.LookupNamespace('ovf'), 'http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized') [void]$XMLDisk.SetAttribute('diskId', $NS.LookupNamespace('ovf'), "vmdisk$($DiskId)") [void]$XMLDisk.SetAttribute('capacity', $NS.LookupNamespace('ovf'), '${{vmconfig.disksize.{0}}}' -f $DiskId) [void]$XMLDisk.SetAttribute('populatedSize', $NS.LookupNamespace('ovf'), 0) [void]$XML.SelectSingleNode('//ns:DiskSection', $NS).AppendChild($XMLDisk) - # Add new resourceType nodes + # Add new 'Item/ResourceType' node (under 'VirtualHardwareSection') $XMLDiskItem = $XMLDiskTemplate.CloneNode($True) $XMLDiskItem.SelectSingleNode('rasd:AddressOnParent', $NS).InnerText = ($DiskId - 1) $XMLDiskItem.SelectSingleNode('rasd:ElementName', $NS).InnerText = "Hard Disk $($DiskId)" $XMLDiskItem.SelectSingleNode('rasd:HostResource', $NS).InnerText = "ovf:/disk/vmdisk$($DiskId)" - # Determine next free available InstanceID + # Determine next free available 'InstanceID' $InstanceIDs = $XML.SelectNodes('//ns:VirtualHardwareSection/ns:Item/rasd:InstanceID', $NS) $InstanceID = 1 While ($InstanceIDs.InnerText -contains $InstanceID) { @@ -147,14 +140,14 @@ If ($OVFConfig.DeploymentConfigurations.Count -gt 0) { Write-Host "Inserted 'DeploymentOptionSection' with $($Configuration.Count) nodes" If ($OVFConfig.DeploymentConfigurations.Count -eq $OVFConfig.DeploymentConfigurations.Size.Count) { - # Create copies of default resourceType nodes + # Create copies of existing 'Item/ResourceType' nodes $XMLCPUTemplate = $XML.SelectSingleNode("//ns:VirtualHardwareSection/ns:Item/rasd:ResourceType[.='3']", $NS).ParentNode.CloneNode($True) $XMLMemoryTemplate = $XML.SelectSingleNode("//ns:VirtualHardwareSection/ns:Item/rasd:ResourceType[.='4']", $NS).ParentNode.CloneNode($True) - # Delete default resourceType nodes + # Delete existing nodes ForEach ($Node in $XML.SelectNodes("//ns:VirtualHardwareSection/ns:Item/rasd:ResourceType[.='3' or .='4']", $NS).ParentNode) { [void]$Node.ParentNode.RemoveChild($Node) } - # Add updated resourceType nodes + # Add adjusted 'Item/ResourceType' nodes ForEach ($Configuration in $OVFConfig.DeploymentConfigurations) { $XMLCPU = $XMLCPUTemplate.CloneNode($True) [void]$XMLCPU.SetAttribute('configuration', $NS.LookupNamespace('ovf'), $Configuration.Id) @@ -182,7 +175,7 @@ ForEach ($ExtraConfig in $OVFConfig.AdvancedOptions) { [void]$XML.SelectSingleNode('//ns:VirtualHardwareSection', $NS).AppendChild($XMLExtraConfig) } -Write-Host "Added $($OVFConfig.AdvancedOptions.Count) 'vmw:ExtraConfig' nodes" +Write-Host "Added $($OVFConfig.AdvancedOptions.Count) 'vmw:ExtraConfig' node(s)" $XMLProductSection = $XML.SelectSingleNode('//ns:ProductSection', $NS) If ($XMLProductSection -eq $Null) { @@ -211,7 +204,7 @@ ForEach ($Category in $OVFConfig.PropertyCategories) { ForEach ($Property in $Category.ProductProperties) { $XMLProperty = $XML.CreateElement('Property', $XML.DocumentElement.xmlns) - $XMLProperty.SetAttribute('key', $NS.LookupNamespace('ovf'), $Property.Key) + [void]$XMLProperty.SetAttribute('key', $NS.LookupNamespace('ovf'), $Property.Key) Switch -regex ($Property.Type) { '^boolean' { [void]$XMLProperty.SetAttribute('type', $NS.LookupNamespace('ovf'), 'boolean')