Skip to content

Commit

Permalink
feat(post-install env): init packages script
Browse files Browse the repository at this point in the history
  • Loading branch information
he3als committed Oct 15, 2023
1 parent 017e182 commit d398aa4
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.dataButton::after{content:"INFO;Starting up;1"}
.dataButton::after{content:"INFO;Getting ready;1"}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<circle class="radial-bg" cx="8px" cy="8px" r="7px"></circle>
<circle class="radial radial-infinite" cx="8px" cy="8px" r="7px"></circle>
</svg>
<h4 id="_progressText" class="errorText">An error has occurred.</h4>
<!-- <h4 id="_progressText" class="errorText">An error has occurred.</h4> -->
<h4 id="_progressText" class="normalText">Updating components...</h4>
<h4 id="_progressText" class="facts">Factual information!</h4>
</div>
Expand All @@ -54,7 +54,7 @@ <h4 id="_progressText" class="facts">Factual information!</h4>

const progressText = document.querySelector('.normalText');
const circle = document.querySelector('.radial-infinite');
const errorText = document.querySelector('.errorText');
// const errorText = document.querySelector('.errorText');
const factsElement = document.querySelector('.facts');
const element = document.querySelector('.data');

Expand All @@ -73,13 +73,14 @@ <h4 id="_progressText" class="facts">Factual information!</h4>
circle.style.strokeDasharray = cssPx + 'px 42.97px';
}

function showFatalError(error) {
errorText.textContent = 'An error has occured: ' + error;
errorText.style.display = 'block';
factsElement.style.display = 'none';
updateProgressText('Your computer will restart shortly, with more details next startup.')
}

// It's probably a better user experience to show the error next reboot.
//
// function showFatalError(error) {
// errorText.textContent = 'An error has occured: ' + error;
// errorText.style.display = 'block';
// factsElement.style.display = 'none';
// updateProgressText('No worries. Your computer will restart shortly, with more details next startup.')
// }

function setNewFact(first) {
if (factCount == 7) {factCount = 0} else {factCount++}
Expand Down Expand Up @@ -118,15 +119,15 @@ <h4 id="_progressText" class="facts">Factual information!</h4>
const items = data.split(';').map(function(item) {
return item.replace(/"/g, "");
});
if (items[0] === 'INFO') {
// if (items[0] === 'INFO') {
setProgress(items[2], items[1]);
setTimeout(function () {
count++; if (count % 3 === 0) { setNewFact(false); }
count++; if (count % 45 === 0) { setNewFact(false); }
getData();
}, 3000);
} else if (items[0] === 'ERROR') {
showFatalError(items[1]);
}
}, 200);
// } else if (items[0] === 'ERROR') {
// showFatalError(items[1]);
// }
}

setProgress(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,125 @@
@echo off
cd /d "%~dp0"
echo hi
start
pause
for %%a in (
log
setInfo
wait
addPackage
) do (
set %%a=call :%%a
)
:: Set to 'High Performance' power plan
powercfg /s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c

:: ======================================================================================================================= ::
:: VARIABLES ::
:: ======================================================================================================================= ::

echo INFO: Setting variables...

:: Set target drive
set "targetDrive=C:"

:: HTA
set "htaFolderPath=%cd%\hta"
set "htaPath=%htaFolderPath%\hta.html"
set "dataPath=%htaFolderPath%\data.css"

:: Target disk variables
set "packagesList=%targetDrive%\Windows\AtlasModules\packagestoInstall"
set "atlasLogDirectory=%targetDrive%\Windows\AtlasModules\Logs"

:: Other variables
set "percentage=1"

:: Set log file
echo INFO: Setting log file...
if not exist "%atlasLogDirectory%" mkdir "%atlasLogDirectory%" & echo Made Atlas log directory...
:makeLogDirectoryAndFile
set /a logNum += 1
set "logDirPath=%atlasLogDirectory%\%logNum%-AtlasPackageInstall-%date:/=-%"
if exist "%logDirPath%" (goto makeLogDirectoryAndFile) else (
mkdir "%logDirPath%"
set "logPath=%logDirPath%\dism.log"
)

echo INFO: Starting debug console...
start /i /d "%targetDrive%\Windows" /min "Atlas Debug Console" cmd /k set "log=notepad %logPath%" ^& cls ^& echo Type %%log%% to open the log.

:: -------------------------------------------------------------------------------------------------------- ::
:: Start looping through packages ::
:: -------------------------------------------------------------------------------------------------------- ::

for /f "usebackq" %%a in (`type "%packagesList%" ^| find "" /v /c`) do set packageCount=%%a
%log% "INFO: Installing %packageCount% packages from %packagesList%..."
set /a percentageSteps=100/%packageCount%
for /f "tokens=* delims=" %%a in (%packagesList%) do (
%addPackage% "%%a"
)

:: -------------------------------------------------------------------------------------------------------- ::
:: Finsh up ::
:: -------------------------------------------------------------------------------------------------------- ::

echo INFO: Finishing up
%setInfo% "Restarting" "100"

if defined error echo "%logPath%" "%error%" > "%targetDrive%\Windows\System32\AtlasPackagesFailure"
copy /y "%packagesList%" "%logDirPath%"
del /f /q "%packagesList%"

%wait% 6
wpeutil reboot

:: ======================================================================================================================= ::
:: FUNCTIONS ::
:: ======================================================================================================================= ::
exit /b

:addPackage [packagePath]
set /a packageNum += 1
set /a halfWayPercentage = %percentage% + ( %percentageSteps% / 2 )

set "dismCurrentLog=%logDirPath%\dismTemp%random%.txt"
set "dismCommand=dism /image:%targetDrive%\ /add-package:"%targetDrive%\%~1" /logpath:"%dismCurrentLog%""
%log% "%dismCommand%"
%setInfo% "Adding package %packageNum%" "%halfWayPercentage%"

%dismCommand%
set dismErrorlevel=%errorlevel%
if %dismErrorlevel%==0 (
%log% "SUCCESS: Successfully deployed %~1..."
type "%dismCurrentLog%" >> "%logPath%"
) else (
%log% "ERROR: Failed to deploy %~1! Exit code %dismErrorLevel%."
set error=%dismErrorlevel%
)
del /f /q "%dismCurrentLog%"

set /a percentage=%percentage% + %percentageSteps%
%setInfo% "Adding package %packageNum%" "%percentage%"
exit /b

:log [text]
(
echo]
echo ==================================================================================================================================
echo %~1
echo ==================================================================================================================================
echo]
) >> "%logPath%"
echo %~1
exit /b

:setInfo [message] [percentage]
echo .dataButton::after{content:"INFO;%~1;%~2"} > "%dataPath%"
exit /b

:: :setError [message]
:: echo .dataButton::after{content:"ERROR;%~1"} > "%dataPath%"
:: exit /b

:wait [seconds]
set /a timeInMs=%~1 * 1000
cscript %cd%\sleep.vbs %timeInMs% //B
exit /b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WScript.Sleep WScript.Arguments(0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
systemDrive = objShell.ExpandEnvironmentStrings("%SystemDrive%")

strHTAPath = systemDrive & "\atlas\hta\hta.html"
Dim objProcess, strHTAPath, bHTARunning
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

If objFSO.FileExists("C:\Windows\AtlasModules\packagestoInstall") Then
objShell.Run systemDrive & "\atlas\packages.cmd", 0
Else
objShell.Run systemDrive & "\sources\recovery\RecEnv.exe", 1
End If

Do
bHTARunning = False
For Each objProcess in objWMIService.ExecQuery("Select * from Win32_Process")
If LCase(objProcess.Name) = "mshta.exe" Then
If InStr(1, objProcess.CommandLine, strHTAPath, vbTextCompare) > 0 Then
bHTARunning = True
Exit For
End If
End If
Next

If Not bHTARunning Then
objShell.Run "mshta " & strHTAPath
End If

WScript.Sleep 3000
Loop
20 changes: 15 additions & 5 deletions src/playbook/Executables/AtlasModules/Scripts/recovery.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $deviceDrive = ($bcdeditRecoveryOutput -split '\]' -split '\[')[1]
$winrePath = ($bcdeditRecoveryOutput -split '\]' -split ',')[1]

# If WinRE is on Recovery partition, mount it
if ($deviceDrive -notcontains ':') {
if ($deviceDrive -notlike '*:*') {
$Kernel32 = Add-Type -Name 'Kernel32' -Namespace '' -PassThru -MemberDefinition @"
[DllImport("kernel32")]
public static extern int QueryDosDevice(string name, System.Text.StringBuilder path, int pathMaxLength);
Expand Down Expand Up @@ -71,12 +71,12 @@ Mount-WindowsImage -ImagePath $fullWimPath -Index 1 -Path $atlasWinRE | Out-Null
# Set startup application
# https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/winpeshlini-reference-launching-an-app-when-winpe-starts
[IO.File]::WriteAllLines("$atlasWinRE\Windows\System32\winpeshl.ini", @"
[LaunchApp]
AppPath = %SYSTEMDRIVE%\atlas\packages.cmd
[LaunchApps]
%WINDIR%\System32\wscript.exe, %SYSTEMDRIVE%\atlas\startup.vbs //B
"@)

# Copy Atlas Package Installation Environment items
Copy-Item "$env:windir\AtlasModules\Other\recovery\*" -Destination "$atlasWinRe\atlas" -Recurse -Force
Copy-Item "$atlasEnvironmentItems\*" -Destination "$atlasWinRe\atlas" -Recurse -Force

# Cleanup
Dismount-WindowsImage -Path $atlasWinRE -Save
Expand All @@ -86,8 +86,18 @@ if ($mountPoint) {
if (!$?) { Remove-Item $mountPoint -Force }
}

# Boot into Windows Recovery next boot
# Boot into Windows Recovery
reagentc /boottore | Out-Null
Restart-Computer

# ===================================================================== #
## This was my attempt at making the modified Atlas WinRE a seperate WIM.

## This should be done as it means you don't have to modify system files,
## meaning Windows Updates won't ever overwrite it.

## We should find a solution to do this before v0.3.0 ideally.
# ===================================================================== #

# Load the Recovery Registry hive
# do { $atlasRegistryPath = "HKLM\atlaswinre$($count; $count++)" } until (!(Test-Path "Registry::$atlasRegistryPath"))
Expand Down
3 changes: 3 additions & 0 deletions src/playbook/Executables/AtlasModules/packagesToInstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Windows\AtlasModules\Packages\Z-Atlas-NoDefender-Package31bf3856ad364e35amd643.0.0.0.cab
Windows\AtlasModules\Packages\Z-Atlas-Misc-Remover-Package31bf3856ad364e35amd643.0.0.0.cab
Windows\AtlasModules\Packages\Z-Atlas-NoTelemetry-Package31bf3856ad364e35amd643.0.0.0.cab

0 comments on commit d398aa4

Please sign in to comment.