import subprocess
import csv


def run_command(cmd, logfile):
    with open(logfile, 'wb') as stream:
        p = subprocess.Popen(cmd, stdout=stream, stderr=stream)
        p.wait()


def task_scheduler():
    logfile = 'C:/projekte/python/logs/schtasks.csv'

    run_command('schtasks /query /v /fo CSV', logfile)

    with open(logfile, "r", encoding="cp850", errors="ignore", newline="") as source_file:
        csv_read = csv.reader(source_file, delimiter=",")
        header = next(csv_read)

        for row in csv_read:
            if row[0] != 'Hostname' and not row[1].startswith('\\Microsoft') and row[2] != 'Nicht zutreffend':
                print(";".join(row))


def task_manager():
    logfile = 'C:/projekte/python/logs/tasklist.csv'

    run_command('tasklist /fo CSV', logfile)

    with open(logfile, "r", encoding="cp850", errors="ignore", newline="") as source_file:
        csv_read = csv.reader(source_file, delimiter=",")
        header = next(csv_read)

        for row in csv_read:
            if row[0].lower() not in ['svchost.exe', 'teams.exe', 'chrome.exe', 'conhost.exe']:
                print(";".join(row))


if __name__ == '__main__':
    # Aufgabenplanung aktueller Stand
    # task_scheduler()
    # Laufende Prozesse
    task_manager()
    # aktuelle Freigabe-Sessions

    # Liste aller Dateien im GAPS-Verzeichnis
    # filename;size;cdate;mdate

    # Logdateien aus Tasks/logs und System/prot
