PortValhalla/scripts/Windows/OS/Setup.ps1

71 lines
3 KiB
PowerShell
Raw Normal View History

2023-07-12 20:37:31 +00:00
#!/bin/pwsh
2024-03-23 16:32:45 +00:00
$Global:InformationPreference = "Continue";
$Global:ErrorActionPreference = "Inquire";
2023-06-21 20:10:19 +00:00
$null = $env:WIN_COMPUTER_NAME;
$null = $env:SETUP_SCRIPT_NAME;
[xml]$unattendedConfig = [xml]::new();
$unattendedConfig.PreserveWhitespace = $true;
$readerSettings = [System.Xml.XmlReaderSettings]::new();
$readerSettings.IgnoreComments = $true;
$reader = [System.Xml.XmlReader]::Create("$PSScriptRoot/../Resources/Autounattend.template.xml", $readerSettings);
$unattendedConfig.Load($reader);
2023-06-22 14:05:10 +00:00
$namespace = New-Object -TypeName "Xml.XmlNamespaceManager" -ArgumentList $unattendedConfig.NameTable;
$namespace.AddNamespace("ua", $unattendedConfig.DocumentElement.NamespaceURI);
2023-06-21 20:10:19 +00:00
function Get-PassSettings {
[OutputType([xml])]
param(
[string] $passName
)
2023-06-22 14:05:10 +00:00
return $unattendedConfig.SelectSingleNode("/ua:unattend/ua:settings[@pass='$passName']", $namespace);
2023-06-21 20:10:19 +00:00
}
# Adjust unattended settings
$specializeSettings = Get-PassSettings "specialize";
2023-06-22 14:11:32 +00:00
$specializeSettings.SelectSingleNode("./ua:component[@name='Microsoft-Windows-Shell-Setup']/ua:ComputerName", $namespace).InnerText = "$env:WIN_COMPUTER_NAME";
2023-06-21 20:10:19 +00:00
# Execute corresponding installer script after startup
2023-06-21 20:10:19 +00:00
$oobeSystemSettings = Get-PassSettings "oobeSystem";
2024-03-11 16:45:15 +00:00
$installationCommand = $oobeSystemSettings.SelectSingleNode("./ua:component/ua:FirstLogonCommands/ua:SynchronousCommand[last()]", $namespace);
2024-03-11 16:46:51 +00:00
$newCommand = $installationCommand.ParentNode.AppendChild($installationCommand.CloneNode($true));
2024-07-27 16:59:47 +00:00
function Get-RemoteScriptPath($path) {
$relativePath = [System.IO.Path]::GetRelativePath("$PSScriptRoot/../../..", $path);
Join-Path $env:REMOTE_PROJECT_PATH $relativePath;
}
function Get-Injection($value) {
"([System.Text.Encoding]::Default.GetString([System.Convert]::FromBase64String('$(
[System.Convert]::ToBase64String([System.Text.Encoding]::Default.GetBytes($value))
)')))"
}
function Get-ScriptPathInjection($path) {
"(Join-Path `$env:SystemDrive $(Get-Injection (Get-RemoteScriptPath $path)))"
}
$newCommand.SelectSingleNode("./ua:CommandLine", $namespace).InnerText = `
"powershell -Command " + `
2024-07-27 16:59:47 +00:00
"`$env:INSTALLER_SCRIPT = $(Get-ScriptPathInjection $env:SETUP_SCRIPT_NAME);" + `
"Invoke-Expression $(Get-ScriptPathInjection "$PSScriptRoot/InitialBoot.ps1");";
2024-03-23 12:45:15 +00:00
$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";
2023-06-21 20:10:19 +00:00
2024-03-23 16:31:16 +00:00
if (Get-Command Initialize-SetupConfig -ErrorAction SilentlyContinue) {
2024-03-23 12:45:15 +00:00
Initialize-SetupConfig $unattendedConfig $namespace;
}
2024-07-27 16:59:47 +00:00
$unattendedConfigFile = "X:\unattend.xml";
2023-06-22 14:50:40 +00:00
$unattendedConfig.PreserveWhitespace = $true;
2024-07-27 16:59:47 +00:00
$unattendedConfig.Save($unattendedConfigFile);
2023-06-22 14:17:02 +00:00
2023-06-21 20:10:19 +00:00
Write-Warning "Attention: This program will completely wipe your current disk #1 and install Windows on it. Are you sure you want to do this?"
Read-Host -Prompt "Hit enter to continue or CTRL+C to abort"
2024-07-27 16:59:47 +00:00
& "$SETUP_DRIVE\setup.exe" /Unattend:$unattendedConfigFile;