diff options
-rw-r--r-- | cmd/tsch.go | 13 | ||||
-rw-r--r-- | internal/exec/tsch/exec.go | 15 | ||||
-rw-r--r-- | internal/exec/tsch/module.go | 6 |
3 files changed, 24 insertions, 10 deletions
diff --git a/cmd/tsch.go b/cmd/tsch.go index 9abb3eb..f377d56 100644 --- a/cmd/tsch.go +++ b/cmd/tsch.go @@ -32,6 +32,7 @@ func tschDemandCmdInit() { tschDemandCmd.Flags().StringVarP(&executableArgs, "args", "a", "", "Arguments to pass to executable") tschDemandCmd.Flags().StringVarP(&tschTaskName, "name", "n", "", "Target task name") tschDemandCmd.Flags().BoolVar(&tschNoDelete, "no-delete", false, "Don't delete task after execution") + tschDemandCmd.Flags().Uint32Var(&tschSessionId, "session-id", 0, "Hijack existing session") if err := tschDemandCmd.MarkFlagRequired("executable"); err != nil { panic(err) } @@ -41,9 +42,9 @@ func tschRegisterCmdInit() { tschRegisterCmd.Flags().StringVarP(&executable, "executable", "e", "", "Remote Windows executable to invoke") tschRegisterCmd.Flags().StringVarP(&executableArgs, "args", "a", "", "Arguments to pass to executable") tschRegisterCmd.Flags().StringVarP(&tschTaskName, "name", "n", "", "Target task name") - tschRegisterCmd.Flags().DurationVar(&tschStopDelay, "delay-stop", time.Duration(5*time.Second), "Delay between task execution and termination. This will not stop the process spawned by the task") - tschRegisterCmd.Flags().DurationVarP(&tschDelay, "delay-start", "d", time.Duration(5*time.Second), "Delay between task registration and execution") - tschRegisterCmd.Flags().DurationVarP(&tschDeleteDelay, "delay-delete", "D", time.Duration(0*time.Second), "Delay between task termination and deletion") + tschRegisterCmd.Flags().DurationVar(&tschStopDelay, "delay-stop", 5*time.Second, "Delay between task execution and termination. This will not stop the process spawned by the task") + tschRegisterCmd.Flags().DurationVarP(&tschDelay, "delay-start", "d", 5*time.Second, "Delay between task registration and execution") + tschRegisterCmd.Flags().DurationVarP(&tschDeleteDelay, "delay-delete", "D", 0*time.Second, "Delay between task termination and deletion") tschRegisterCmd.Flags().BoolVar(&tschNoDelete, "no-delete", false, "Don't delete task after execution") tschRegisterCmd.Flags().BoolVar(&tschCallDelete, "call-delete", false, "Directly call SchRpcDelete to delete task") @@ -74,6 +75,7 @@ func tschArgs(principal string) func(cmd *cobra.Command, args []string) error { } var ( + tschSessionId uint32 tschNoDelete bool tschCallDelete bool tschDeleteDelay time.Duration @@ -175,8 +177,9 @@ References: ExecutionMethod: tschexec.MethodDemand, ExecutionMethodConfig: tschexec.MethodDemandConfig{ - NoDelete: tschNoDelete, - TaskPath: tschTaskPath, + NoDelete: tschNoDelete, + TaskPath: tschTaskPath, + SessionId: tschSessionId, }, } if err := module.Connect(log.WithContext(ctx), creds, target, connCfg); err != nil { diff --git a/internal/exec/tsch/exec.go b/internal/exec/tsch/exec.go index 1996f27..49a2dc2 100644 --- a/internal/exec/tsch/exec.go +++ b/internal/exec/tsch/exec.go @@ -145,10 +145,12 @@ func (mod *Module) Exec(ctx context.Context, ecfg *exec.ExecutionConfig) (err er } else { taskPath := cfg.TaskPath + if taskPath == "" { log.Debug().Msg("Task path not defined. Using random path") taskPath = `\` + util.RandomString() } + st := newSettings(true, true, false) tk := newTask(st, nil, triggers{}, ecfg.ExecutableName, ecfg.ExecutableArgs) @@ -157,12 +159,20 @@ func (mod *Module) Exec(ctx context.Context, ecfg *exec.ExecutionConfig) (err er if err != nil { return fmt.Errorf("call registerTask: %w", err) } + if !cfg.NoDelete { defer mod.deleteTask(ctx, taskPath) } + + var flags uint32 + + if cfg.SessionId != 0 { + flags |= 4 + } _, err := mod.tsch.Run(ctx, &itaskschedulerservice.RunRequest{ - Path: taskPath, - Flags: 0, // Maybe we want to use these? + Path: taskPath, + Flags: flags, + SessionID: cfg.SessionId, }) if err != nil { log.Error().Str("task", taskPath).Err(err).Msg("Failed to run task") @@ -170,6 +180,7 @@ func (mod *Module) Exec(ctx context.Context, ecfg *exec.ExecutionConfig) (err er } log.Info().Str("task", taskPath).Msg("Started task") } + } else { return fmt.Errorf("method '%s' not implemented", ecfg.ExecutionMethod) } diff --git a/internal/exec/tsch/module.go b/internal/exec/tsch/module.go index 6e379a7..33c0723 100644 --- a/internal/exec/tsch/module.go +++ b/internal/exec/tsch/module.go @@ -14,9 +14,8 @@ type Module struct { } type MethodRegisterConfig struct { - NoDelete bool - CallDelete bool - //TaskName string + NoDelete bool + CallDelete bool TaskPath string StartDelay time.Duration StopDelay time.Duration @@ -26,6 +25,7 @@ type MethodRegisterConfig struct { type MethodDemandConfig struct { NoDelete bool CallDelete bool + SessionId uint32 TaskName string TaskPath string StopDelay time.Duration |