1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# -*- coding: utf-8 -*- ## Copyright 2019 Trevor van Hoof and Jan Pijpers. ## Licensed under the Apache License, Version 2.0 ## Downloaded from https://janpijpers.com or https://gumroad.com/janpijpers ## See the license file attached or on https://www.janpijpers.com/script-licenses/ ''' Name: getProcessId Description: script to get all running processes from tasklist alternatively you can use wmic for the commandline parameters i.e. wmic process where caption="bla.exe" get commandline ''' import json import subprocess def get_pdata(name = None): ''' {'Session Name': 'Console', 'Mem Usage': '7', 'PID': '15888', 'Image Name': 'conhost.exe', 'Session#': '1'} ''' pdata = [] ## create a startup info so we can hide the window popup. startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE ## run the system command to gte all task info in csv format task_list = subprocess.check_output(['tasklist', '/fo', 'csv'], startupinfo = startupinfo).replace('"','') taskLines = task_list.splitlines() dictHeaders =taskLines.pop(0).split(",") ## Itterate over all tasks for task_line in taskLines: taskDict = {taskData[0]:taskData[1] for taskData in zip(dictHeaders, task_line.split(','))} ## Filter on name if so desired if name: if name.lower() in taskDict["Image Name"].lower(): pdata.append(taskDict) else: pdata.append(taskDict) return pdata data = get_pdata() print json.dumps(data, indent =2, sort_keys=True) |