Skip to content

Commit

Permalink
Create scheduled task to move active hours
Browse files Browse the repository at this point in the history
  • Loading branch information
cschneegans committed Nov 11, 2024
1 parent f4d5056 commit 12c43bf
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public record class Configuration(
bool HardenSystemDriveAcl,
bool AllowPowerShellScripts,
bool DisableLastAccess,
bool NoAutoRebootWithLoggedOnUsers,
bool PreventAutomaticReboot,
bool DisableDefender,
bool DisableSac,
bool DisableUac,
Expand Down Expand Up @@ -370,7 +370,7 @@ CompactOsModes CompactOsMode
HardenSystemDriveAcl: false,
AllowPowerShellScripts: false,
DisableLastAccess: false,
NoAutoRebootWithLoggedOnUsers: false,
PreventAutomaticReboot: false,
DisableDefender: false,
DisableSac: false,
DisableUac: false,
Expand Down
4 changes: 4 additions & 0 deletions UnattendGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<None Remove="resource\ClassicContextMenu.ps1" />
<None Remove="resource\DisableDefender.vbs" />
<None Remove="resource\MakeEdgeUninstallable.ps1" />
<None Remove="resource\MoveActiveHours.vbs" />
<None Remove="resource\MoveActiveHours.xml" />
<None Remove="resource\PauseWindowsUpdate.ps1" />
<None Remove="resource\PauseWindowsUpdate.xml" />
<None Remove="resource\RemoveBloatware.ps1" />
Expand Down Expand Up @@ -40,12 +42,14 @@
<EmbeddedResource Include="resource\KeyboardIdentifier.json" />
<EmbeddedResource Include="resource\Bloatware.json" />
<EmbeddedResource Include="resource\known-writeable-folders.txt" />
<EmbeddedResource Include="resource\MoveActiveHours.xml" />
<EmbeddedResource Include="resource\PauseWindowsUpdate.xml" />
<EmbeddedResource Include="resource\RestartExplorer.ps1" />
<EmbeddedResource Include="resource\SetComputerName.ps1" />
<EmbeddedResource Include="resource\SetColorTheme.ps1" />
<EmbeddedResource Include="resource\PauseWindowsUpdate.ps1" />
<EmbeddedResource Include="resource\ClassicContextMenu.ps1" />
<EmbeddedResource Include="resource\MoveActiveHours.vbs" />
<EmbeddedResource Include="resource\TaskbarIcons.ps1" />
<EmbeddedResource Include="resource\MakeEdgeUninstallable.ps1" />
<EmbeddedResource Include="resource\SetWallpaper.ps1" />
Expand Down
8 changes: 7 additions & 1 deletion modifier/Optimizations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,18 @@ IEnumerable<string> SetExplorerOptions(string rootKey, string subKey)
);
}

if (Configuration.NoAutoRebootWithLoggedOnUsers)
if (Configuration.PreventAutomaticReboot)
{
appender.Append([
CommandBuilder.RegistryCommand(@"add ""HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"" /v AUOptions /t REG_DWORD /d 4 /f"),
CommandBuilder.RegistryCommand(@"add ""HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"" /v NoAutoRebootWithLoggedOnUsers /t REG_DWORD /d 1 /f"),
]);

AddTextFile(Util.StringFromResource("MoveActiveHours.vbs"), @"C:\Windows\Setup\Scripts\MoveActiveHours.vbs");
AddXmlFile(Util.XmlDocumentFromResource("MoveActiveHours.xml"), @"C:\Windows\Setup\Scripts\MoveActiveHours.xml");
appender.Append(
CommandBuilder.PowerShellCommand(@"Register-ScheduledTask -TaskName 'MoveActiveHours' -Xml $( Get-Content -LiteralPath 'C:\Windows\Setup\Scripts\MoveActiveHours.xml' -Raw );")
);
}

if (Configuration.DisableFastStartup)
Expand Down
7 changes: 7 additions & 0 deletions resource/MoveActiveHours.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HKLM = &H80000002
key = "SOFTWARE\Microsoft\WindowsUpdate\UX\Settings"
Set reg = GetObject("winmgmts://./root/default:StdRegProv")
current = Hour(Now)
reg.SetDWORDValue HKLM, key, "ActiveHoursStart", ( current + 23 ) Mod 24
reg.SetDWORDValue HKLM, key, "ActiveHoursEnd", ( current + 11 ) Mod 24
reg.SetDWORDValue HKLM, key, "SmartActiveHoursState", 2
49 changes: 49 additions & 0 deletions resource/MoveActiveHours.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Triggers>
<BootTrigger>
<Repetition>
<Interval>PT4H</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<Enabled>true</Enabled>
</BootTrigger>
<RegistrationTrigger>
<Repetition>
<Interval>PT4H</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<Enabled>true</Enabled>
</RegistrationTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-19</UserId>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\wscript.exe</Command>
<Arguments>C:\Windows\Setup\Scripts\MoveActiveHours.vbs</Arguments>
</Exec>
</Actions>
</Task>

0 comments on commit 12c43bf

Please sign in to comment.