Nest Windows setup in a function

This commit is contained in:
Manuel Thalmann 2024-07-28 20:52:07 +02:00
parent 49e9d646c0
commit bea3e68925

View file

@ -1,4 +1,5 @@
#!/bin/pwsh #!/bin/pwsh
function Start-Setup {
$Global:InformationPreference = "Continue"; $Global:InformationPreference = "Continue";
$Global:ErrorActionPreference = "Inquire"; $Global:ErrorActionPreference = "Inquire";
$null = $env:WIN_COMPUTER_NAME; $null = $env:WIN_COMPUTER_NAME;
@ -27,15 +28,6 @@ function Get-PassSettings {
return $unattendedConfig.SelectSingleNode("/ua:unattend/ua:settings[@pass='$passName']", $namespace); return $unattendedConfig.SelectSingleNode("/ua:unattend/ua:settings[@pass='$passName']", $namespace);
} }
# Adjust unattended settings
$specializeSettings = Get-PassSettings "specialize";
$specializeSettings.SelectSingleNode("./ua:component[@name='Microsoft-Windows-Shell-Setup']/ua:ComputerName", $namespace).InnerText = "$env:WIN_COMPUTER_NAME";
# Execute corresponding installer script after startup
$oobeSystemSettings = Get-PassSettings "oobeSystem";
$installationCommand = $oobeSystemSettings.SelectSingleNode("./ua:component/ua:FirstLogonCommands/ua:SynchronousCommand[last()]", $namespace);
$newCommand = $installationCommand.ParentNode.AppendChild($installationCommand.CloneNode($true));
function Get-RemoteScriptPath($path) { function Get-RemoteScriptPath($path) {
$relativePath = [System.IO.Path]::GetRelativePath("$PSScriptRoot/../../..", $path); $relativePath = [System.IO.Path]::GetRelativePath("$PSScriptRoot/../../..", $path);
Join-Path $env:REMOTE_PROJECT_PATH $relativePath; Join-Path $env:REMOTE_PROJECT_PATH $relativePath;
@ -51,6 +43,15 @@ function Get-ScriptPathInjection($path) {
"(Join-Path `$env:SystemDrive $(Get-Injection (Get-RemoteScriptPath $path)))" "(Join-Path `$env:SystemDrive $(Get-Injection (Get-RemoteScriptPath $path)))"
} }
# Adjust unattended settings
$specializeSettings = Get-PassSettings "specialize";
$specializeSettings.SelectSingleNode("./ua:component[@name='Microsoft-Windows-Shell-Setup']/ua:ComputerName", $namespace).InnerText = "$env:WIN_COMPUTER_NAME";
# Execute corresponding installer script after startup
$oobeSystemSettings = Get-PassSettings "oobeSystem";
$installationCommand = $oobeSystemSettings.SelectSingleNode("./ua:component/ua:FirstLogonCommands/ua:SynchronousCommand[last()]", $namespace);
$newCommand = $installationCommand.ParentNode.AppendChild($installationCommand.CloneNode($true));
$newCommand.SelectSingleNode("./ua:CommandLine", $namespace).InnerText = ` $newCommand.SelectSingleNode("./ua:CommandLine", $namespace).InnerText = `
"powershell -Command " + ` "powershell -Command " + `
"`$env:INSTALLER_SCRIPT = $(Get-ScriptPathInjection $env:SETUP_SCRIPT_NAME);" + ` "`$env:INSTALLER_SCRIPT = $(Get-ScriptPathInjection $env:SETUP_SCRIPT_NAME);" + `
@ -72,3 +73,6 @@ $unattendedConfig.Save($unattendedConfigFile);
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?" 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" Read-Host -Prompt "Hit enter to continue or CTRL+C to abort"
& "$SETUP_DRIVE\setup.exe" /Unattend:$unattendedConfigFile; & "$SETUP_DRIVE\setup.exe" /Unattend:$unattendedConfigFile;
}
Start-Setup