Improve scripts for altering windows partitions

This commit is contained in:
Manuel Thalmann 2023-07-09 23:31:27 +02:00
parent b1fe52c7db
commit f85ee116a0

View file

@ -11,71 +11,106 @@ $Global:SetupConfigPostprocessor = {
$diskConfig = $setupComponent.SelectSingleNode("./ua:DiskConfiguration/ua:Disk", $namespace);
$installationPartition = $setupComponent.SelectSingleNode("./ua:ImageInstall/ua:OSImage/ua:InstallTo/ua:PartitionID", $namespace);
$newIndex = 2;
$installationPartition.InnerText = "$($newIndex + 3)";
$partitionCreationContainer = $diskConfig.SelectSingleNode("./ua:CreatePartitions", $namespace);
$partitionCreations = $partitionCreationContainer.SelectNodes("./ua:CreatePartition", $namespace);
$garbage = 0;
foreach ($partition in $partitionCreations) {
$order = [int]$partition.SelectSingleNode("./ua:Order", $namespace).InnerText;
$newOrder = $order;
if ($order -eq 2) {
$newOrder--;
} else {
$garbage += [int]$partition.SelectSingleNode("./ua:Size", $namespace).InnerText;
if ($newOrder -lt 2) {
$newOrder++;
}
if ($newOrder -ge $newIndex) {
$newOrder++;
}
}
if ($order -ne $newOrder) {
$partition.SelectSingleNode("./ua:Order", $namespace).InnerText = "$newOrder";
}
}
# Add space before Windows installation... wha-!? For Linux, ofc! I use Arch Linux, btw.
$newPartition = $partitionCreations[0].CloneNode($true);
$newPartition.SelectSingleNode("./ua:Order", $namespace).InnerText = "$newIndex";
$newPartition.SelectSingleNode("./ua:Size", $namespace).InnerText = "$((64 * 1024) - 1024)";
$null = $partitionCreationContainer.AppendChild($newPartition);
$partitionModificationContainer = $diskConfig.SelectSingleNode("./ua:ModifyPartitions", $namespace);
$partitionModifications = $partitionModificationContainer.SelectNodes("./ua:ModifyPartition", $namespace);
$newModification = $partitionModifications[2].CloneNode($true);
$newModification.SelectSingleNode("./ua:Order", $namespace).InnerText = "$newIndex";
$newModification.SelectSingleNode("./ua:PartitionID", $namespace).InnerText = "$newIndex";
foreach ($partition in $partitionModifications) {
$partitionID = [int]$partition.SelectSingleNode("./ua:PartitionID", $namespace).InnerText;
$newID = $partitionID;
<#
.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
)
if ($partitionID -eq 2) {
$newID--;
} else {
if ($newID -lt 2) {
$newID++;
$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 ($newID -ge $newIndex) {
$newID++;
if ($order -ne $newOrder) {
$orderNode.InnerText = "$newOrder";
}
}
if ($partitionID -ne $newID) {
$partition.SelectSingleNode("./ua:PartitionID", $namespace).InnerText = "$newID";
$partition.SelectSingleNode("./ua:Order", $namespace).InnerText = "$newID";
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";
}
}
}
$null = $partitionModificationContainer.AppendChild($newModification);
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)
}
Invoke-PartitionRelocation 2 1;
# Add space before Windows installation... wha-!? For Linux, ofc! I use Arch Linux, btw.
Add-Partition 2 ((64 * 1024) - 1024);
}
. "$PSScriptRoot/../../../scripts/Windows/OS/Setup.ps1";