From ba058911e9f76dc8157deb1467c840138cdc892d Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Fri, 22 Mar 2024 10:32:53 +0100 Subject: [PATCH] Store scheduled task in separate scripts --- scripts/Windows/OS/User/Add.ps1 | 27 +++++++------------------ scripts/Windows/OS/User/AutoLogin.ps1 | 18 +++++++++++++++++ scripts/Windows/OS/User/UACDisabler.ps1 | 24 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 20 deletions(-) create mode 100644 scripts/Windows/OS/User/AutoLogin.ps1 create mode 100644 scripts/Windows/OS/User/UACDisabler.ps1 diff --git a/scripts/Windows/OS/User/Add.ps1 b/scripts/Windows/OS/User/Add.ps1 index 179bfaca..3ce66497 100644 --- a/scripts/Windows/OS/User/Add.ps1 +++ b/scripts/Windows/OS/User/Add.ps1 @@ -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 ( diff --git a/scripts/Windows/OS/User/AutoLogin.ps1 b/scripts/Windows/OS/User/AutoLogin.ps1 new file mode 100644 index 00000000..d98d061c --- /dev/null +++ b/scripts/Windows/OS/User/AutoLogin.ps1 @@ -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); + }; +} diff --git a/scripts/Windows/OS/User/UACDisabler.ps1 b/scripts/Windows/OS/User/UACDisabler.ps1 new file mode 100644 index 00000000..8ad78ba9 --- /dev/null +++ b/scripts/Windows/OS/User/UACDisabler.ps1 @@ -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; + }; +}