Await custom scheduled tasks

This commit is contained in:
Manuel Thalmann 2024-03-22 10:59:24 +01:00
parent ba058911e9
commit 5f02a650ad
3 changed files with 19 additions and 4 deletions

View file

@ -57,7 +57,7 @@ function New-PersonalUser([Context] $context)
Enable-PersonalUserAutologon $context;
$context.RegisterReboot();
$context.SetStage("RemoveAdmin");
Write-EventLog -LogName Application -Source "Application" -EventId $context.Get($uacDisablerTriggerProperty) -Message "This event was created by $env:Username";
Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty);
exit;
}
elseif ($context.GetStage() -eq "RemoveAdmin")
@ -83,11 +83,11 @@ function Enable-UACNextLogin([Context] $context) {
[System.Tuple]::Create(
$autoLoginTrigger,
$autoLoginName,
"{ $((Get-AutoLoginScript)) }.Invoke('$contextScript', '$preparedUsernameProperty', '$preparedPasswordProperty')")
"{ $((Get-AutoLoginScript)) }.Invoke($autoLoginTrigger, '$contextScript', '$preparedUsernameProperty', '$preparedPasswordProperty')")
[System.Tuple]::Create(
$uacDisablerTrigger,
$uacDisablerName,
"{ $((Get-UACDisablerScript)) }.Invoke('$contextScript', '$autoLoginName', '$uacDisablerName', '$autoLoginTriggerProperty', '$uacDisablerTriggerProperty')"));
"{ $((Get-UACDisablerScript)) }.Invoke($uacDisablerTrigger, '$contextScript', '$autoLoginName', '$uacDisablerName', '$autoLoginTriggerProperty', '$uacDisablerTriggerProperty')"));
foreach ($options in $optionCollection) {
$action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument (
@ -127,5 +127,16 @@ function Enable-PersonalUserAutologon([Context] $context)
$context.Set($preparedUsernameProperty, $context.UserName, "ExpandString");
$context.Set($preparedPasswordProperty, $password, "ExpandString");
Write-EventLog -LogName Application -Source "Application" -EventId $context.Get($autoLoginTriggerProperty) -Message "This event was created by $env:Username";
Start-EventDrivenTask $context.Get($autoLoginTriggerProperty);
}
function Start-EventDrivenTask() {
param(
[int]$EventID
);
$identifier = "EventLog$_";
Write-EventLog -LogName Application -Source "Application" -EventId $EventID -Message "This event was created by $env:Username";
Register-WmiEvent -Query "SELECT * FROM __InstanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent' AND EventIdentifier=$EventID" -SourceIdentifier "$identifier";
Wait-Event -SourceIdentifier $identifier;
}

View file

@ -4,6 +4,7 @@
function Get-AutoLoginScript() {
return {
param(
[int]$EventID,
[string]$ContextScriptPath,
[string]$PreparedUsernameProperty,
[string]$PreparedSecretProperty
@ -14,5 +15,6 @@ function Get-AutoLoginScript() {
$userName = $context.Get($PreparedUsernameProperty);
$password = $context.Get($PreparedSecretProperty);
$context.SetAutologin($userName, $password);
Write-EventLog -LogName Application -Source "Application" -EventId $EventID -Message "This event was created by $env:Username";
};
}

View file

@ -4,6 +4,7 @@
function Get-UACDisablerScript() {
return {
param (
[int]$EventID,
[string]$ContextScriptPath,
[string]$AutoLoginTaskName,
[string]$UACDisablerTaskName,
@ -19,6 +20,7 @@ function Get-UACDisablerScript() {
$context.Remove($AutoLoginTriggerProperty);
$context.Remove($UACDisablerTriggerProperty);
$context.DeregisterNewUserReboot();
Write-EventLog -LogName Application -Source "Application" -EventId $EventID -Message "This event was created by $env:Username";
Restart-Computer -Force;
};
}