def main():
# Fetch information for current processes:
proc_info_old = get_proc_info()
# Extract list of PIDs:
pids_old = get_all(proc_info_old, "ProcessId")
# Do something that starts one or more new processes:
os.system('start notepad.exe')
# Fetch information for current processes:
proc_info_new = get_proc_info()
# Extract list of PIDs:
pids_new = get_pids(proc_info_new, "ProcessId")
# Determine PIDs which are only in pids_new, not in pids_old:
new_pids = list(set(pids_new) - set(pids_old))
# Output new PIDs and the associated command line:
for pid in new_pids:
cmd = get_for(proc_info_new, "ProcessId", pid, "CommandLine")
test.log("PID: %s" % pid)
test.log("CMD: %s" % cmd)
test.log("")
os.system("taskkill /f /im notepad.exe")
def get_all(proc_info, get_name):
res = []
for pi in proc_info:
res.append(pi[get_name])
return res
def get_for(proc_info, look_for_name, look_for_value, get_name):
for pi in proc_info:
if pi[look_for_name] == look_for_value:
return pi[get_name]
return None
def get_proc_info(search_expression=None):
if search_expression is None:
search_expression = ""
else:
search_expression = " WHERE " + search_expression
# Execute with wmic and capture output:
s = 'pushd "' + os.getcwd() + '" && wmic PROCESS ' + search_expression + ' GET * /format:csv <nul'
d = subprocess.Popen(s, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0];
# Strip first empty line produced by wmic:
d = d[3:]
# Write to file (to later read via testData API):
fn = "temp.csv"
f = codecs.open(fn, "w", "utf8")
f.write(d)
f.close()
# Read via testData API:
dataset = testData.dataset(fn)
all_proc_info = []
for row in dataset:
proc_info = {}
field_names = testData.fieldNames(row)
for n in field_names:
v = testData.field(row, n)
proc_info[n] = v
all_proc_info.append(proc_info)
os.remove(fn)
return all_proc_info