Call Scheduled Tasks using scripts

This commit is contained in:
Manuel Thalmann 2024-03-23 01:27:49 +01:00
parent 98acbc4817
commit 4c66d01f00
3 changed files with 42 additions and 62 deletions

View file

@ -84,32 +84,25 @@ function Enable-UACNextLogin([Context] $context) {
$autoLoginTrigger, $autoLoginTrigger,
$autoLoginName, $autoLoginName,
@( @(
"{", ". `"$PSScriptRoot/AutoLogin.ps1`"",
" Invoke-Command { $("$(Get-AutoLoginScript)" -split "[`r`n]" -join " ") } -ArgumentList @(", " $autoLoginTrigger",
" $autoLoginTrigger,", " '$contextScript'",
" '$contextScript',", " '$preparedUsernameProperty'",
" '$preparedUsernameProperty',", " '$preparedPasswordProperty'")),
" '$preparedPasswordProperty'",
" )",
"}")),
[System.Tuple]::Create( [System.Tuple]::Create(
$uacDisablerTrigger, $uacDisablerTrigger,
$uacDisablerName, $uacDisablerName,
@( @(
"{", ". `"$PSScriptRoot/UACDisabler.ps1`"",
" Invoke-Command { $("$(Get-UACDisablerScript)" -split "[`r`n]" -join " ") } -ArgumentList @(", " $uacDisablerTrigger",
" $uacDisablerTrigger,", " '$contextScript'",
" '$contextScript',", " '$autoLoginName'",
" '$autoLoginName',", " '$uacDisablerName'",
" '$uacDisablerName',", " '$autoLoginTriggerProperty'",
" '$autoLoginTriggerProperty',", " '$uacDisablerTriggerProperty'")));
" '$uacDisablerTriggerProperty'",
" )",
"}")));
foreach ($options in $optionCollection) { foreach ($options in $optionCollection) {
$action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument ( $action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument ((@("-c") + $options.Item3) -join " ");
[string]::Join(" ", @("-c") + ($options.Item3)));
schtasks /Create /SC ONEVENT /EC Application /MO "*[System[Provider[@Name='Application'] and EventID=$($options.Item1)]]" /TR cmd.exe /TN "$tempTask"; schtasks /Create /SC ONEVENT /EC Application /MO "*[System[Provider[@Name='Application'] and EventID=$($options.Item1)]]" /TR cmd.exe /TN "$tempTask";
$trigger = (Get-ScheduledTask $tempTask).Triggers; $trigger = (Get-ScheduledTask $tempTask).Triggers;

View file

@ -1,20 +1,13 @@
#!/bin/pwsh #!/bin/pwsh
. "$PSScriptRoot/../../Scripts/Context.ps1"; param(
function Get-AutoLoginScript() {
return {
param(
[int]$EventID, [int]$EventID,
[string]$ContextScriptPath,
[string]$PreparedUsernameProperty, [string]$PreparedUsernameProperty,
[string]$PreparedSecretProperty [string]$PreparedSecretProperty
); );
. "$ContextScriptPath"; . "$PSScriptRoot/../../Scripts/Context.ps1";
$context = [Context]::new(); $context = [Context]::new();
$userName = $context.Get($PreparedUsernameProperty); $userName = $context.Get($PreparedUsernameProperty);
$password = $context.Get($PreparedSecretProperty); $password = $context.Get($PreparedSecretProperty);
$context.SetAutologin($userName, $password); $context.SetAutologin($userName, $password);
Write-EventLog -LogName Application -Source "Application" -EventId $EventID -Message "This event was created by $env:Username"; Write-EventLog -LogName Application -Source "Application" -EventId $EventID -Message "This event was created by $env:Username";
};
}

View file

@ -1,26 +1,20 @@
#!/bin/pwsh #!/bin/pwsh
. "$PSScriptRoot/../../Scripts/Context.ps1"; param (
function Get-UACDisablerScript() {
return {
param (
[int]$EventID, [int]$EventID,
[string]$ContextScriptPath, [string]$ContextScriptPath,
[string]$AutoLoginTaskName, [string]$AutoLoginTaskName,
[string]$UACDisablerTaskName, [string]$UACDisablerTaskName,
[string]$AutoLoginTriggerProperty, [string]$AutoLoginTriggerProperty,
[string]$UACDisablerTriggerProperty [string]$UACDisablerTriggerProperty
); );
. "$ContextScriptPath"; . "$PSScriptRoot/../../Scripts/Context.ps1";
$context = [Context]::new(); $context = [Context]::new();
Unregister-ScheduledTask -Confirm:$false $AutoLoginTaskName; Unregister-ScheduledTask -Confirm:$false $AutoLoginTaskName;
Unregister-ScheduledTask -Confirm:$false $UACDisablerTaskName; Unregister-ScheduledTask -Confirm:$false $UACDisablerTaskName;
$context.SetUACState($false); $context.SetUACState($false);
$context.Remove($AutoLoginTriggerProperty); $context.Remove($AutoLoginTriggerProperty);
$context.Remove($UACDisablerTriggerProperty); $context.Remove($UACDisablerTriggerProperty);
$context.DeregisterNewUserReboot(); $context.DeregisterNewUserReboot();
Write-EventLog -LogName Application -Source "Application" -EventId $EventID -Message "This event was created by $env:Username"; Write-EventLog -LogName Application -Source "Application" -EventId $EventID -Message "This event was created by $env:Username";
Restart-Computer -Force; Restart-Computer -Force;
};
}