From c5a401f7eb552eab0e2fdc6a11c92f80506f8fd5 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 23 Mar 2024 13:45:15 +0100 Subject: [PATCH] Refactor Windows setup scripts --- profiles/DerGeret/Windows/Setup.ps1 | 190 ++++++++++++++-------------- scripts/Windows/OS/Setup.ps1 | 8 +- 2 files changed, 101 insertions(+), 97 deletions(-) diff --git a/profiles/DerGeret/Windows/Setup.ps1 b/profiles/DerGeret/Windows/Setup.ps1 index 731346f8..ac90bf24 100644 --- a/profiles/DerGeret/Windows/Setup.ps1 +++ b/profiles/DerGeret/Windows/Setup.ps1 @@ -2,8 +2,103 @@ $env:WIN_COMPUTER_NAME = "DerGeret"; $env:SETUP_SCRIPT_NAME = "$PSScriptRoot/Restore.ps1"; -$Global:SetupConfigPostprocessor = { - param([xml] $config, [System.Xml.XmlNamespaceManager] $namespace) +function Initialize-SetupConfig() { + param( + [xml] $config, + [System.Xml.XmlNamespaceManager] $namespace + ); + + New-Module { + <# + .SYNOPSIS + Increases the ID of all partitions in the specified range by 1. + #> + function Move-PartitionRange { + param ( + [int]$From = 0, + [System.Nullable[int]]$To = $null, + [int]$By = 1 + ) + + $installPartitionID = [int]$installationPartition.InnerText; + + if (($installPartitionID -ge $From) -and (($null -eq $To) -or ($installPartitionID -lt $To))) { + $installationPartition.InnerText = "$($installPartitionID + $By)"; + } + + foreach ($partition in $partitionCreations) { + $orderNode = $partition.SelectSingleNode("./ua:Order", $namespace); + $order = [int]$orderNode.InnerText; + $newOrder = $order; + + if (($newOrder -ge $From) -and (($null -eq $To) -or ($newOrder -lt $To))) { + $newOrder += $By; + } + + if ($order -ne $newOrder) { + $orderNode.InnerText = "$newOrder"; + } + } + + foreach ($partition in $partitionModifications) { + $partitionNode = $partition.SelectSingleNode("./ua:PartitionID", $namespace); + $partitionID = [int]$partitionNode.InnerText; + $newID = $partitionID; + + if (($newID -ge $From) -and (($null -eq $To) -or ($newID -lt $To))) { + $newID += $By; + } + + if ($partitionID -ne $newID) { + $partitionNode.InnerText = "$newID"; + $partition.SelectSingleNode("./ua:Order", $namespace).InnerText = "$newID"; + } + } + } + + function Add-Partition { + param ( + [int]$Index, + [int]$Size, + [string]$Type = "Primary" + ) + + Move-PartitionRange -From $Index -By 1; + + $newPartition = $partitionCreations[0].CloneNode($true); + $newPartition.SelectSingleNode("./ua:Order", $namespace).InnerText = "$Index"; + $newPartition.SelectSingleNode("./ua:Type", $namespace).InnerText = "$Type"; + $newPartition.SelectSingleNode("./ua:Size", $namespace).InnerText = "$Size"; + $null = $partitionCreationContainer.AppendChild($newPartition); + + $newModification = $partitionModifications[2].CloneNode($true); + $newModification.SelectSingleNode("./ua:Order", $namespace).InnerText = "$Index"; + $newModification.SelectSingleNode("./ua:PartitionID", $namespace).InnerText = "$Index"; + $null = $partitionModificationContainer.AppendChild($newModification); + } + + <# + .SYNOPSIS + Relocates the partition with the specified `$From` ID to the specified `$To` ID. + #> + function Invoke-PartitionRelocation { + param ( + [int]$From, + [int]$To + ) + + Move-PartitionRange $From ($From + 1) (-1 * ($From + 1)) + + if ($From -gt $To) { + Move-PartitionRange $To $From; + } + elseif ($From -lt $To) { + Move-PartitionRange $From ($To + 1) -1; + } + + Move-PartitionRange -1 0 ($To + 1) + } + } $setupComponent = $config.SelectSingleNode( "/ua:unattend/ua:settings[@pass='windowsPE']/ua:component[@name='Microsoft-Windows-Setup']", @@ -17,97 +112,6 @@ $Global:SetupConfigPostprocessor = { $partitionModificationContainer = $diskConfig.SelectSingleNode("./ua:ModifyPartitions", $namespace); $partitionModifications = $partitionModificationContainer.SelectNodes("./ua:ModifyPartition", $namespace); - - <# - .SYNOPSIS - Increases the ID of all partitions in the specified range by 1. - #> - function Move-PartitionRange { - param ( - [int]$From = 0, - [System.Nullable[int]]$To = $null, - [int]$By = 1 - ) - - $installPartitionID = [int]$installationPartition.InnerText; - - if (($installPartitionID -ge $From) -and (($null -eq $To) -or ($installPartitionID -lt $To))) { - $installationPartition.InnerText = "$($installPartitionID + $By)"; - } - - foreach ($partition in $partitionCreations) { - $orderNode = $partition.SelectSingleNode("./ua:Order", $namespace); - $order = [int]$orderNode.InnerText; - $newOrder = $order; - - if (($newOrder -ge $From) -and (($null -eq $To) -or ($newOrder -lt $To))) { - $newOrder += $By; - } - - if ($order -ne $newOrder) { - $orderNode.InnerText = "$newOrder"; - } - } - - foreach ($partition in $partitionModifications) { - $partitionNode = $partition.SelectSingleNode("./ua:PartitionID", $namespace); - $partitionID = [int]$partitionNode.InnerText; - $newID = $partitionID; - - if (($newID -ge $From) -and (($null -eq $To) -or ($newID -lt $To))) { - $newID += $By; - } - - if ($partitionID -ne $newID) { - $partitionNode.InnerText = "$newID"; - $partition.SelectSingleNode("./ua:Order", $namespace).InnerText = "$newID"; - } - } - } - - function Add-Partition { - param ( - [int]$Index, - [int]$Size, - [string]$Type = "Primary" - ) - - Move-PartitionRange -From $Index -By 1; - - $newPartition = $partitionCreations[0].CloneNode($true); - $newPartition.SelectSingleNode("./ua:Order", $namespace).InnerText = "$Index"; - $newPartition.SelectSingleNode("./ua:Type", $namespace).InnerText = "$Type"; - $newPartition.SelectSingleNode("./ua:Size", $namespace).InnerText = "$Size"; - $null = $partitionCreationContainer.AppendChild($newPartition); - - $newModification = $partitionModifications[2].CloneNode($true); - $newModification.SelectSingleNode("./ua:Order", $namespace).InnerText = "$Index"; - $newModification.SelectSingleNode("./ua:PartitionID", $namespace).InnerText = "$Index"; - $null = $partitionModificationContainer.AppendChild($newModification); - } - - <# - .SYNOPSIS - Relocates the partition with the specified `$From` ID to the specified `$To` ID. - #> - function Invoke-PartitionRelocation { - param ( - [int]$From, - [int]$To - ) - - Move-PartitionRange $From ($From + 1) (-1 * ($From + 1)) - - if ($From -gt $To) { - Move-PartitionRange $To $From; - } - elseif ($From -lt $To) { - Move-PartitionRange $From ($To + 1) -1; - } - - Move-PartitionRange -1 0 ($To + 1) - } - $partitionCreations[1].SelectSingleNode("./ua:Size", $namespace).InnerText = "$(1024)"; Invoke-PartitionRelocation 2 1; diff --git a/scripts/Windows/OS/Setup.ps1 b/scripts/Windows/OS/Setup.ps1 index 47ff0ce8..9e7f8838 100644 --- a/scripts/Windows/OS/Setup.ps1 +++ b/scripts/Windows/OS/Setup.ps1 @@ -39,12 +39,12 @@ $newCommand.SelectSingleNode("./ua:CommandLine", $namespace).InnerText = ` $(Get-Content "$PSScriptRoot/InitialBoot.ps1") + ` "; pwsh '$([System.IO.Path]::GetRelativePath("$PSScriptRoot/../../..", $env:SETUP_SCRIPT_NAME))';"; -$order = [int]$newCommand.SelectSingleNode("./ua:Order", $namespace).InnerText; -$newCommand.SelectSingleNode("./ua:Order", $namespace).InnerText = $order + 1; +$orderElement = $newCommand.SelectSingleNode("./ua:Order", $namespace); +$orderElement.InnerText = ([int]($orderElement.InnerText) + 1); $newCommand.SelectSingleNode("./ua:Description", $namespace).InnerText = "Install PowerShell Core and git and run setup script"; -if ($Global:SetupConfigPostprocessor) { - $Global:SetupConfigPostprocessor.Invoke($unattendedConfig, $namespace); +if (Get-Command Initialize-SetupConfig) { + Initialize-SetupConfig $unattendedConfig $namespace; } $unattendedConfig.PreserveWhitespace = $true;