Store scheduled task in separate scripts

This commit is contained in:
Manuel Thalmann 2024-03-22 10:32:53 +01:00
parent d649525722
commit ba058911e9
3 changed files with 49 additions and 20 deletions

View file

@ -1,5 +1,8 @@
#!/bin/pwsh
. "$PSScriptRoot/../Scripts/Context.ps1";
$contextScript = "$PSScriptRoot/../../Scripts/Context.ps1";
. "$PSScriptRoot/AutoLogin.ps1";
. "$PSScriptRoot/UACDisabler.ps1";
. "$contextScript";
$preparedUsernameProperty = "AutoLoginUser";
$preparedPasswordProperty = "AutoLoginPassword";
$autoLoginTriggerProperty = "AutoLoginTrigger";
@ -76,31 +79,15 @@ function Enable-UACNextLogin([Context] $context) {
$context.Set($autoLoginTriggerProperty, $autoLoginTrigger, "DWord");
$context.Set($uacDisablerTriggerProperty, $uacDisablerTrigger, "DWord");
$optionCollection = [System.Tuple[int, string, string[]][]]@(
$optionCollection = [System.Tuple[int, string, string][]]@(
[System.Tuple]::Create(
$autoLoginTrigger,
$autoLoginName,
@(
". '$PSScriptRoot/../Scripts/Context.ps1';",
"`$context = [Context]::new();",
"`$username = `$context.Get('$preparedUsernameProperty');",
"`$password = `$context.Get('$preparedPasswordProperty');",
"`$context.SetAutologin(`$username, `$password);",
"`$context.Remove('$preparedUsernameProperty');",
"`$context.Remove('$preparedPasswordProperty');")),
"{ $((Get-AutoLoginScript)) }.Invoke('$contextScript', '$preparedUsernameProperty', '$preparedPasswordProperty')")
[System.Tuple]::Create(
$uacDisablerTrigger,
$uacDisablerName,
@(
"Unregister-ScheduledTask -Confirm:`$false '$autoLoginName';",
"Unregister-ScheduledTask -Confirm:`$false '$uacDisablerName';",
". '$PSScriptRoot/../Scripts/Context.ps1';",
"`$context = [Context]::new();",
"`$context.SetUACState(`$false);",
"`$context.Remove('$autoLoginTriggerProperty');",
"`$context.Remove('$uacDisablerTriggerProperty');",
"`$context.DeregisterNewUserReboot();",
"Restart-Computer -Force;")));
"{ $((Get-UACDisablerScript)) }.Invoke('$contextScript', '$autoLoginName', '$uacDisablerName', '$autoLoginTriggerProperty', '$uacDisablerTriggerProperty')"));
foreach ($options in $optionCollection) {
$action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument (

View file

@ -0,0 +1,18 @@
#!/bin/pwsh
. "$PSScriptRoot/../../Scripts/Context.ps1";
function Get-AutoLoginScript() {
return {
param(
[string]$ContextScriptPath,
[string]$PreparedUsernameProperty,
[string]$PreparedSecretProperty
);
. "$ContextScriptPath";
$context = [Context]::new();
$userName = $context.Get($PreparedUsernameProperty);
$password = $context.Get($PreparedSecretProperty);
$context.SetAutologin($userName, $password);
};
}

View file

@ -0,0 +1,24 @@
#!/bin/pwsh
. "$PSScriptRoot/../../Scripts/Context.ps1";
function Get-UACDisablerScript() {
return {
param (
[string]$ContextScriptPath,
[string]$AutoLoginTaskName,
[string]$UACDisablerTaskName,
[string]$AutoLoginTriggerProperty,
[string]$UACDisablerTriggerProperty
);
. "$ContextScriptPath";
$context = [Context]::new();
Unregister-ScheduledTask -Confirm:$false $AutoLoginTaskName;
Unregister-ScheduledTask -Confirm:$false $UACDisablerTaskName;
$context.SetUACState($false);
$context.Remove($AutoLoginTriggerProperty);
$context.Remove($UACDisablerTriggerProperty);
$context.DeregisterNewUserReboot();
Restart-Computer -Force;
};
}