Last active
February 24, 2021 16:02
-
-
Save aev-mambro2/e5c4d0768a01858ed50b2e88fc685b65 to your computer and use it in GitHub Desktop.
disable-remote-scheduled-tasks-using-powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This only works if you have CIM, WMI, and task change privileges on the remote host servers. | |
# List the known hosts, and for each host their known scheduled task paths. | |
# We're only interested in production task paths - no need to specify test task paths. | |
# (You did separate those, didn't you?) | |
# | |
# The host name is just the NetBUI name: no slashes, ports, protocols, or paths. | |
# The scheduled task path starts with a backslash (which stands for Task Scheduler -> Task Scheduler Library) | |
# and then follows the hierarchy you see in the task scheduler. Use a wildcard * to include any task in a path. | |
$taskHostPaths = @{ | |
"server-name-1" = @("\FolderName\Production\*") | |
"server-name-2" = @("\FolderName\FolderName\Prod\*") | |
"server-name-3" = @( | |
"\FolderName\One\Production\*" | |
, "\OtherFolder\Two\Three\Prod\*" | |
, "\YetAnotherFolder\Foo\Bar\Baz\*" | |
) | |
"server-name-4" = @("\FolderName\Production\*") | |
} | |
# Disable every scheduled task found in every known path for every known remote host. | |
# Prints the host name, the path name, and the result of disabling each task. | |
foreach ($_host in $taskHostPaths.Keys) { | |
$_session = New-CimSession -ComputerName $_host; | |
foreach ($_path in $taskHostPaths[$_host]) { | |
Write-Output "Host: $_host"; | |
Write-Output "Task path: $_path"; | |
Get-ScheduledTask -CimSession $_session -TaskPath $_path | Disable-ScheduledTask; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The amount of time this saves for devops is staggering. It does require that the operator has certain privileges on the remote host: CIM, WMI, and task schedule changes.