Jun 08 2023 03:40 AM
Hello to all, i seek your wisdom!
I am trying to automate the following steps performed manually:
- Disable a running scheduled task, but keeping it running.
- Do actions that stop the task process.
- Do additional actions.
- Enable scheduled task and run it.
I can do all the steps in PowerShell, but the issue i have is that when i use:
Disable-ScheduledTask -TaskName 'TaskName' -TaskPath '\TaskPath\'
while the task remains running, i am not able to get the Disabled state with:
Get-ScheduledTask -TaskName 'TaskName' -TaskPath '\TaskPath\'
In the taskschd.msc ui i can see the option to enable the task, so i see hat is is disabled
But i need to confirm the Disabled state from PowerShell.
I am not able to find anything in task cim properties either.
I hope one of you knows the solution to my dilema.
Jun 08 2023 04:04 AM
There's two points of interest within the scheduled task WMI class:
What you are checking for in your final stage is State when you should be checking the Triggers array.
Here is a simple example script that demonstrates the difference between State and Triggers, where each object within the Triggers array contains a property named Enabled, which indicates if the trigger is enabled or disabled, which is what your final question is alluding to.
Get-ScheduledTask -TaskName "SystemSoundsService" |
ForEach-Object {
$Task = $_;
for ($index = 0; $index -lt $Task.Triggers.Length; $index++) {
[PSCustomObject] @{
Task = "$($Task.TaskPath)$($Task.TaskName)";
State = $Task.State;
Trigger = $index;
TriggerEnabled = $Task.Triggers[$index].Enabled;
}
}
}
Cheers,
Lain
Jun 08 2023 04:55 AM - edited Jun 08 2023 04:57 AM
Hello @LainRobertson
thank you for your fast reply. I think i understand your State and Trigger explanation. However, the trigger comes back the same enabled : True, when the task is enabled or disabled.
Looking at your script i am a bit confused. You are running a foreach-object, but there seems to be only one state and one trigger property for a service, as far as i can see. For example:
$task = Get-ScheduledTask -TaskName "RtkAudUService64_BG" -TaskPath \
$task | Get-Member
TypeName: Microsoft.Management.Infrastructure.CimInstance#Root/Microsoft/Windows/TaskScheduler/MSFT_ScheduledTask
Name MemberType Definition
---- ---------- ----------
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
GetCimSessionComputerName Method string GetCimSessionComputerName()
GetCimSessionInstanceId Method guid GetCimSessionInstanceId()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Actions Property CimInstance#InstanceArray Actions {get;set;}
Author Property string Author {get;set;}
Date Property string Date {get;set;}
Description Property string Description {get;set;}
Documentation Property string Documentation {get;set;}
Principal Property CimInstance#Instance Principal {get;set;}
PSComputerName Property string PSComputerName {get;}
SecurityDescriptor Property string SecurityDescriptor {get;set;}
Settings Property CimInstance#Instance Settings {get;set;}
Source Property string Source {get;set;}
TaskName Property string TaskName {get;}
TaskPath Property string TaskPath {get;}
Triggers Property CimInstance#InstanceArray Triggers {get;set;}
URI Property string URI {get;}
Version Property string Version {get;set;}
State ScriptProperty System.Object State {get=[Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.StateEnum]($this.PSBase.CimInstanceProperti
$task.Triggers
Enabled : True
EndBoundary :
ExecutionTimeLimit :
Id :
Repetition : MSFT_TaskRepetitionPattern
StartBoundary :
Delay : PT30S
UserId :
PSComputerName :
Or do you mean that every object has a corresponding trigger in WMI MSFT_TaskLogonTrigger object?
Sorry for the confusion :(
Jun 08 2023 05:03 AM - edited Jun 08 2023 05:05 AM
i was poking around the system where could i see some additional info and i found this, when running schtasks /query:
PS C:\Windows\System32> schtasks /query /TN "\RtkAudUService64_BG" /XML
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2010-07-30T20:43:10</Date>
<Author>Realtek</Author>
<URI>\RtkAudUService64_BG</URI>
</RegistrationInfo>
<Principals>
<Principal id="Author">
<GroupId>S-1-5-32-545</GroupId>
</Principal>
</Principals>
<Settings>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<Enabled>false</Enabled>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<MultipleInstancesPolicy>Parallel</MultipleInstancesPolicy>
<IdleSettings>
<Duration>PT10M</Duration>
<WaitTimeout>PT1H</WaitTimeout>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
</Settings>
<Triggers>
<LogonTrigger>
<Delay>PT30S</Delay>
</LogonTrigger>
</Triggers>
<Actions Context="Author">
<Exec>
<Command>""C:\WINDOWS\System32\DriverStore\FileRepository\realtekservice.inf_amd64_ed3f04e1261e4822\RtkAudUService64.exe""</Command>
<Arguments>-background</Arguments>
</Exec>
</Actions>
</Task>
The <Enabled>false</Enabled> line in Settings, is visible only when the task is disabled.
I would rather however use the PowerShell commandlets, to get the values.
Jun 08 2023 05:25 AM - edited Jun 08 2023 05:26 AM
Solution
Hi,
Sorry, try this instead, which checks the Settings.Enabled property:
Get-ScheduledTask -TaskName "SystemSoundsService" |
ForEach-Object {
[PSCustomObject] @{
Task = "$($_.TaskPath)$($_.TaskName)";
State = $_.State;
Enabled = $_.Settings.Enabled;
}
}
The principle is the same as for Triggers, it's just that the Settings.Enabled describes the job as a whole rather than Triggers.Enabled that returns the status of each trigger in the Triggers array.
Cheers,
Lain
Jun 08 2023 06:40 AM
@LainRobertson
Thats it, thank you mate!
I am surprised and a bit ashamed, that i did not see it when i was going through the properties.
Cheers