Skip to content

Commit

Permalink
Added some optional parameter checks and features to SchTasks.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
mohinparamasivam committed Sep 3, 2022
1 parent 83eff89 commit 029bfbe
Show file tree
Hide file tree
Showing 139 changed files with 95,866 additions and 8 deletions.
Binary file added .vs/SharPersist/v16/.suo
Binary file not shown.
97 changes: 94 additions & 3 deletions SharPersist/SchTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ public void listPersistence(string persistMethod, string command, string command
string schtaskName = task.Name;
DateTime runTime = task.NextRunTime;
string theRunTime = runTime.ToString("G", CultureInfo.CurrentCulture);
DateTime lastrunTime = task.LastRunTime;
string theLastRunTime = lastrunTime.ToString("G", CultureInfo.CurrentCulture);

// once we find the schtask, display its details
if (schtaskName.ToLower().Equals(theName.ToLower()))
Expand Down Expand Up @@ -352,6 +354,9 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");
Expand Down Expand Up @@ -417,6 +422,8 @@ public void listPersistence(string persistMethod, string command, string command
string schtaskName = task.Name;
DateTime runTime = task.NextRunTime;
string theRunTime = runTime.ToString("G", CultureInfo.CurrentCulture);
DateTime lastrunTime = task.LastRunTime;
string theLastRunTime = lastrunTime.ToString("G", CultureInfo.CurrentCulture);
bool taskActive = task.IsActive;

// only proceed to list schtask info if it is active
Expand Down Expand Up @@ -446,8 +453,29 @@ public void listPersistence(string persistMethod, string command, string command
if (optionSpecified)
{

if (option.ToLower().Equals("hourly") && triggerType.ToLower().Equals("time"))

int lastrunday = lastrunTime.Day;
int nextrunday = runTime.Day;
int lastrunhour = lastrunTime.Hour;
int nextrunhour = runTime.Hour;
int lastrunminute = lastrunTime.Minute;
int nextrunminute = runTime.Minute;


int timediffday = nextrunday - lastrunday;
int timediffhour = nextrunhour - lastrunhour;
int timediffminute = nextrunminute - lastrunminute;


string[] optional_addon = option.Split(' ');
string triggerTime = optional_addon[0].ToLower();



if (triggerTime.ToLower().Equals("minute") && triggerType.ToLower().Equals("time") && timediffminute > 0 && timediffhour <= 0 && timediffday <= 0)
{


Console.WriteLine("[*] INFO: TASK NAME:");
Console.WriteLine(schtaskName);
Console.WriteLine("");
Expand All @@ -457,6 +485,60 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");

// get the frequency in which the schtask executes
TriggerCollection theTriggers = task.Definition.Triggers;
string theTriggerType = "";
foreach (Trigger trigger in theTriggers)
{
RepetitionPattern pattern = trigger.Repetition;

theTriggerType = trigger.TriggerType.ToString();
Console.WriteLine("[*] INFO: TASK TRIGGER:");
Console.WriteLine(theTriggerType);
Console.WriteLine("");
}



// get all actions and print
foreach (Microsoft.Win32.TaskScheduler.Action action in allActions)
{
Console.WriteLine("[*] INFO: TASK ACTION:");
Console.WriteLine(action.ToString());
Console.WriteLine("");

}

Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");

}


if (triggerTime.ToLower().Equals("hourly") && triggerType.ToLower().Equals("time") && timediffhour > 0 && timediffday <= 0)
{


Console.WriteLine("[*] INFO: TASK NAME:");
Console.WriteLine(schtaskName);
Console.WriteLine("");
Console.WriteLine("[*] INFO: TASK PATH:");
Console.WriteLine(schtaskFolder);
Console.WriteLine("");
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");
Expand Down Expand Up @@ -492,7 +574,7 @@ public void listPersistence(string persistMethod, string command, string command
}


else if (option.ToLower().Equals("daily") && triggerType.ToLower().Equals("daily"))
else if (triggerTime.ToLower().Equals("daily") && triggerType.ToLower().Equals("daily") && timediffday > 0)
{
Console.WriteLine("[*] INFO: TASK NAME:");
Console.WriteLine(schtaskName);
Expand All @@ -503,6 +585,9 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");
Expand Down Expand Up @@ -537,7 +622,7 @@ public void listPersistence(string persistMethod, string command, string command
}


else if ((option.ToLower().Equals("logon") && triggerType.ToLower().Equals("logon")) || (option.ToLower().Equals("boot") && triggerType.ToLower().Equals("boot")))
else if ((triggerTime.ToLower().Equals("logon") && triggerType.ToLower().Equals("logon")) || (option.ToLower().Equals("boot") && triggerType.ToLower().Equals("boot")))
{
Console.WriteLine("[*] INFO: TASK NAME:");
Console.WriteLine(schtaskName);
Expand All @@ -548,6 +633,9 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");
Expand Down Expand Up @@ -596,6 +684,9 @@ public void listPersistence(string persistMethod, string command, string command
Console.WriteLine("[*] INFO: TASK OWNER:");
Console.WriteLine(owner);
Console.WriteLine("");
Console.WriteLine("[*] INFO: LAST RUN TIME:");
Console.WriteLine(theLastRunTime);
Console.WriteLine("");
Console.WriteLine("[*] INFO: NEXT RUN TIME:");
Console.WriteLine(theRunTime);
Console.WriteLine("");
Expand Down
4 changes: 2 additions & 2 deletions SharPersist/SharPersist.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
<Reference Include="Microsoft.CSharp">
<HintPath>..\..\..\..\..\..\..\..\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.CSharp.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Win32.TaskScheduler, Version=2.8.11.0, Culture=neutral, PublicKeyToken=c416bc1b32d97233, processorArchitecture=MSIL">
<HintPath>..\packages\TaskScheduler.2.8.11\lib\net40\Microsoft.Win32.TaskScheduler.dll</HintPath>
<Reference Include="Microsoft.Win32.TaskScheduler, Version=2.10.1.0, Culture=neutral, PublicKeyToken=e25603a88b3aa7da, processorArchitecture=MSIL">
<HintPath>..\packages\TaskScheduler.2.10.1\lib\net40\Microsoft.Win32.TaskScheduler.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
Expand Down
Binary file not shown.
Loading

0 comments on commit 029bfbe

Please sign in to comment.