Skip to content

Instantly share code, notes, and snippets.

@Diagg
Created November 15, 2021 00:05
Show Gist options
  • Select an option

  • Save Diagg/c98b2fda49d243fe75966761666faed5 to your computer and use it in GitHub Desktop.

Select an option

Save Diagg/c98b2fda49d243fe75966761666faed5 to your computer and use it in GitHub Desktop.
This function will help showing user interface when using client in system context like SCCM, Intune or Workspace One.
Function Invoke-ScheduledTask
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[String]$TaskName,
[Parameter(Mandatory = $true)]
[String]$command,
[Parameter(Mandatory = $false)]
[string]$Parameters,
[Parameter(Mandatory = $false)]
[ValidateSet("System","SYSTEM","system","User","USER","user","Admin","ADMIN","admin")]
[String]$Context = "System",
[Parameter(Mandatory = $false)]
[switch]$Interactive,
[Parameter(Mandatory = $false)]
[string]$Description = "Powershell Scheduled task for instant execution (Kinda..)"
)
#Created Scheduled Task to run as Logged on User
$TaskName = $TaskName.Replace(" ","_")
$ToastGUID = ([guid]::NewGuid()).ToString().ToUpper()
$Task_TimeToRun = (Get-Date).AddSeconds(10).ToString('s')
$Task_Expiry = (Get-Date).AddSeconds(120).ToString('s')
If([String]::IsNullOrWhiteSpace($Parameters))
{$Task_Action = New-ScheduledTaskAction -Execute $command}
Else
{$Task_Action = New-ScheduledTaskAction -Execute $command -Argument $Parameters}
$Task_Trigger = New-ScheduledTaskTrigger -Once -At $Task_TimeToRun
$Task_Trigger.EndBoundary = $Task_Expiry
If ($Context.ToUpper() -eq "SYSTEM")
{
if ($Interactive){$LogonType = "Interactive"} Else {$LogonType = "ServiceAccount"}
$Task_Principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType $LogonType -RunLevel Highest
}
elseif ($Context.ToUpper() -eq "ADMIN")
{$Task_Principal = New-ScheduledTaskPrincipal -GroupId "S-1-5-32-544" -RunLevel Highest}
else
{$Task_Principal = New-ScheduledTaskPrincipal -GroupId "S-1-5-32-545" -RunLevel Limited}
$Task_Settings = New-ScheduledTaskSettingsSet -Compatibility V1 -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 600) -AllowStartIfOnBatteries
$New_Task = New-ScheduledTask -Description $Description -Action $Task_Action -Principal $Task_Principal -Trigger $Task_Trigger -Settings $Task_Settings
Try
{
Register-ScheduledTask -TaskName $($TaskName + "_" + $ToastGuid) -InputObject $New_Task -ErrorAction SilentlyContinue
Write-host "Task $taskName scheduled sucessfully in $context context for a planed execution within the next 30 seconds !"
Write-host "Command to run is: $command $Parameters"
Return $true
}
Catch
{
Write-host "[Error], Unable to schedule task $taskName, Aborting !!!"
Return $false
}
}
#Task is planned within System or Admin context
#ServiceUI.exe is part of MDT
#Run as admin with GUI, with all privileges
#Invoke-ScheduledTask -TaskName PsCleaner -command 'C:\Windows\System32\cleanmgr.exe' -Parameters '/sagerun:1' -Context admin
#Run as user with GUI, with user only privileges
#Invoke-ScheduledTask -TaskName PsCleaner -command 'C:\Windows\System32\cleanmgr.exe' -Parameters '/sagerun:1' -Context user
##Run as system with GUI (ServiceUI is requiered), with all privileges
#Invoke-ScheduledTask -TaskName PsCleaner -command "C:\=Perso=\Auchan\Patching\ServiceUI.exe" -Parameters 'C:\Windows\System32\cleanmgr.exe /sagerun:1' -Context System -Interactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment