Program.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Microsoft.Win32.TaskScheduler;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace barryscheduler
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. if (args.Length == 0) return; //no command line arg.
  15. add_scheduled_task(args[0]);
  16. }
  17. public static void add_scheduled_task(string drive_name)
  18. {
  19. // Get the service on the local machine
  20. using (TaskService ts = new TaskService())
  21. {
  22. // Create a new task definition and assign properties
  23. TaskDefinition td = ts.NewTask();
  24. td.RegistrationInfo.Description = "Barry Autorun Script";
  25. td.Settings.Hidden = true;
  26. // Create a trigger that will fire at logon
  27. td.Triggers.Add(new LogonTrigger());
  28. // Create an action that will launch barry powershell script
  29. string target_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), String.Format("barry\\{0}.ps1", drive_name));
  30. td.Actions.Add(new ExecAction("powershell", String.Format(@"-WindowStyle Hidden -ExecutionPolicy Unrestricted -File ""{0}""", target_path), null));
  31. // Register the task in the root folder
  32. ts.RootFolder.RegisterTaskDefinition("BarryAutorun", td);
  33. }
  34. }
  35. }
  36. }