Skip to content

Commit 14ccdfc

Browse files
committed
Suprress task deletion errors and ensure only one error message is printed
1 parent 33f3f7c commit 14ccdfc

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

nxc/modules/schtask_as.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import os
23
from time import sleep
34
from datetime import datetime, timedelta
@@ -91,7 +92,8 @@ def on_admin_login(self, context, connection):
9192
except Exception as e:
9293
if "SCHED_S_TASK_HAS_NOT_RUN" in str(e):
9394
self.logger.fail("Task was not run, seems like the specified user has no active session on the target")
94-
exec_method.deleteartifact()
95+
with contextlib.suppress(Exception):
96+
exec_method.deleteartifact()
9597
else:
9698
self.logger.fail(f"Failed to execute command: {e}")
9799

@@ -255,20 +257,25 @@ def execute_handler(self, command, fileless=False):
255257
except Exception as e:
256258
if "ERROR_NONE_MAPPED" in str(e):
257259
self.logger.fail(f"User {self.user} is not connected on the target, cannot run the task")
258-
tsch.hSchRpcDelete(dce, f"\\{self.task}")
259-
if e.error_code and hex(e.error_code) == "0x80070005":
260-
self.logger.fail("Schtask_as: Create schedule task got blocked.")
261-
tsch.hSchRpcDelete(dce, f"\\{self.task}")
262-
if "ERROR_TRUSTED_DOMAIN_FAILURE" in str(e):
260+
with contextlib.suppress(Exception):
261+
tsch.hSchRpcDelete(dce, f"\\{self.task}")
262+
elif e.error_code and hex(e.error_code) == "0x80070005":
263+
self.logger.fail("Create schedule task got blocked.")
264+
with contextlib.suppress(Exception):
265+
tsch.hSchRpcDelete(dce, f"\\{self.task}")
266+
elif "ERROR_TRUSTED_DOMAIN_FAILURE" in str(e):
263267
self.logger.fail(f"User {self.user} does not exist in the domain.")
264-
tsch.hSchRpcDelete(dce, f"\\{self.task}")
265-
if "SCHED_S_TASK_HAS_NOT_RUN" in str(e):
266-
tsch.hSchRpcDelete(dce, f"\\{self.task}")
267-
if "ERROR_ALREADY_EXISTS" in str(e):
268-
self.logger.fail(f"Schtask_as: Create schedule task failed: {e}")
268+
with contextlib.suppress(Exception):
269+
tsch.hSchRpcDelete(dce, f"\\{self.task}")
270+
elif "SCHED_S_TASK_HAS_NOT_RUN" in str(e):
271+
with contextlib.suppress(Exception):
272+
tsch.hSchRpcDelete(dce, f"\\{self.task}")
273+
elif "ERROR_ALREADY_EXISTS" in str(e):
274+
self.logger.fail(f"Create schedule task failed: {e}")
269275
else:
270-
self.logger.fail(f"Schtask_as: Create schedule task failed: {e}")
271-
tsch.hSchRpcDelete(dce, f"\\{self.task}")
276+
self.logger.fail(f"Create schedule task failed: {e}")
277+
with contextlib.suppress(Exception):
278+
tsch.hSchRpcDelete(dce, f"\\{self.task}")
272279
return
273280

274281
self.logger.info(f"Running task \\{self.task}")

0 commit comments

Comments
 (0)