- Access exclusive content
- Connect with peers
- Share your expertise
- Find support resources
04-21-2024 03:02 PM
Hello dear community,
here is a script for searching specific scheduled tasks by name in mass. The search is via LIKE and wildcards are used.
import subprocess
import sys
def ScheduledTask(scheduler_name):
# PowerShell-Befehl mit Where-Object und Filterung für den TaskScheduler
pscommand = f"""Get-ScheduledTask | Where-Object {{$_.TaskName -like '*{scheduler_name}*'}} | ForEach-Object {{ $_.TaskName + "|" + $_.State }}"""
try:
# PowerShell-Befehl ausführen und Ausgabe erfassen
result = subprocess.check_output(["powershell", "& {" + pscommand + "}"], text=True, encoding='utf-8', errors='ignore')
# Ergebnis anzeigen oder weiterverarbeiten
return result.strip()
except subprocess.CalledProcessError as e:
# Fehlerbehandlung, falls der Befehl nicht erfolgreich ist
sys.stderr.write(f"Fehler bei der Ausführung des Befehls: {e}")
return None
# Vorherige Festlegung des TaskScheduler-Namens
scheduler_name = "MeinTaskScheduler"
# Aufruf der Funktion mit dem festgelegten TaskScheduler-Namen
output = ScheduledTask(scheduler_name)
# Ausgabe des Ergebnisses ohne Header
print(output)
Have fun with it!
BR
Rob
04-23-2024 12:00 PM
Thanks for sharing!
12-10-2024 02:43 AM
Hi,
i took the liberty to use your code and to evolve it, by passing the scheduler_name as an argument of the script
import subprocess
import sys
def ScheduledTask(scheduler_name):
# PowerShell command using Where-Object and filtering for the TaskScheduler
pscommand = f"""Get-ScheduledTask | Where-Object {{$_.TaskName -like '*{scheduler_name}*'}} | ForEach-Object {{ $_.TaskName + "|" + $_.State }}"""
try:
# Execute PowerShell command and capture output
result = subprocess.check_output(["powershell", "& {" + pscommand + "}"], text=True, encoding='utf-8', errors='ignore')
# Display or further process the result
return result.strip()
except subprocess.CalledProcessError as e:
# Error handling if the command is not successful
sys.stderr.write(f"Error executing the command: {e}")
return None
if __name__ == "__main__":
print(main(sys.argv[1]))
Click Accept as Solution to acknowledge that the answer to your question has been provided.
The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!
These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!
The LIVEcommunity thanks you for your participation!