Skip to content

Commit

Permalink
Module: WindowsTaskScheduler (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfevia authored Jul 8, 2024
1 parent aef7bec commit 99ac9c8
Show file tree
Hide file tree
Showing 51 changed files with 7,339 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="NSwag.ApiDescription.Client" Version="14.0.8" />
<PackageVersion Include="NUnit" Version="4.1.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="TaskScheduler" Version="2.11.0" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions PowerAutomate.Desktop.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "OpenAPI", "OpenAPI", "{6709
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PetStore.Client", "samples\PetStore.Client\PetStore.Client.csproj", "{A9858CCE-DDB1-4942-84BE-08991E46A818}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modules.Windows.TaskScheduler.Actions", "modules\Modules.Windows.TaskScheduler.Actions\Modules.Windows.TaskScheduler.Actions.csproj", "{E7F31F2C-1091-4B82-A685-C7A74FC80A1D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -67,6 +69,10 @@ Global
{A9858CCE-DDB1-4942-84BE-08991E46A818}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A9858CCE-DDB1-4942-84BE-08991E46A818}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A9858CCE-DDB1-4942-84BE-08991E46A818}.Release|Any CPU.Build.0 = Release|Any CPU
{E7F31F2C-1091-4B82-A685-C7A74FC80A1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7F31F2C-1091-4B82-A685-C7A74FC80A1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7F31F2C-1091-4B82-A685-C7A74FC80A1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7F31F2C-1091-4B82-A685-C7A74FC80A1D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -84,6 +90,7 @@ Global
{6709150F-B5E8-4377-BAEA-5394C5340806} = {64A2FE14-920C-4A2F-8F4A-28B77CC84BA4}
{3D0BC3F2-AB03-44A3-8FAF-599FB80DB717} = {6709150F-B5E8-4377-BAEA-5394C5340806}
{A9858CCE-DDB1-4942-84BE-08991E46A818} = {6709150F-B5E8-4377-BAEA-5394C5340806}
{E7F31F2C-1091-4B82-A685-C7A74FC80A1D} = {3B78C634-DFD6-43DA-A30E-33AC42224BD4}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
Modules.Actions.Shared\Modules.Actions.Shared.projitems*{7e19e2f1-6de7-4a0e-aed8-7cea38b166af}*SharedItemsImports = 13
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// --------------------------------------------------------------
// Copyright (c) Jesus Fernandez. All Rights Reserved.
// --------------------------------------------------------------

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes;
using Microsoft.Win32.TaskScheduler;
using PowerAutomate.Desktop.Modules.Actions.Shared;

namespace PowerAutomate.Desktop.Modules.Windows.TaskScheduler.Actions.Actions;

[Action(Id = "CreateTask")]
[Group(Name = Groups.General, Order = 1)]
[Group(Name = Groups.Advanced, Order = 2, IsDefault = true)]
[Throws(ErrorCodes.Unknown)]
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global", Justification = "PowerAutomate.Desktop.Module.Action")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global", Justification = "PowerAutomate.Desktop.Module.Action")]
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global", Justification = "PowerAutomate.Desktop.Module.Action")]
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "PowerAutomate.Desktop.Module.Action")]
[SuppressMessage("ReSharper", "UnusedType.Global", Justification = "PowerAutomate.Desktop.Module.Action")]
public class CreateTaskAction : ActionBase
{
[InputArgument(Order = 4, Required = false)]
public string AccountDomain { get; set; } = null!;

[InputArgument(Order = 5, Required = false)]
public string Password { get; set; } = null!;

[InputArgument(Order = 2, Required = false)]
public string TargetServer { get; set; } = null!;

[InputArgument(Order = 1, Group = Groups.General)]
public string TaskName { get; set; } = null!;

[InputArgument(Order = 3, Required = false)]
public string UserName { get; set; } = null!;

public override void Execute(ActionContext context)
{
Debugger.Launch();

try
{
using var taskService = new TaskService(TargetServer, UserName, AccountDomain, Password);
using var task = taskService.NewTask();

taskService.RootFolder
.RegisterTaskDefinition(TaskName, task)
.Dispose();
}
catch (Exception ex)
{
throw new ActionException(ErrorCodes.Unknown, ex.Message, ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// --------------------------------------------------------------
// Copyright (c) Jesus Fernandez. All Rights Reserved.
// --------------------------------------------------------------

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK;
using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes;
using Microsoft.Win32.TaskScheduler;
using PowerAutomate.Desktop.Modules.Actions.Shared;
using PowerAutomate.Desktop.Modules.Windows.TaskScheduler.Actions.Exceptions;

namespace PowerAutomate.Desktop.Modules.Windows.TaskScheduler.Actions.Actions;

[Action(Id = "CreateTaskAction")]
[Group(Name = Groups.General, Order = 1)]
[Group(Name = Groups.Advanced, Order = 2, IsDefault = true)]
[Throws(ErrorCodes.TaskNotFound)]
[Throws(ErrorCodes.Unknown)]
[SuppressMessage("ReSharper", "AutoPropertyCanBeMadeGetOnly.Global", Justification = "PowerAutomate.Desktop.Module.Action")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global", Justification = "PowerAutomate.Desktop.Module.Action")]
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global", Justification = "PowerAutomate.Desktop.Module.Action")]
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "PowerAutomate.Desktop.Module.Action")]
[SuppressMessage("ReSharper", "UnusedType.Global", Justification = "PowerAutomate.Desktop.Module.Action")]
public class CreateTaskActionAction : ActionBase
{
[InputArgument(Order = 7, Required = false)]
public string AccountDomain { get; set; } = null!;

[InputArgument(Order = 3, Group = Groups.General, Required = false)]
public string Arguments { get; set; } = null!;

[InputArgument(Order = 8, Required = false)]
public string Password { get; set; } = null!;

[InputArgument(Order = 2, Group = Groups.General)]
public string Path { get; set; } = null!;

[InputArgument(Order = 5, Required = false)]
public string TargetServer { get; set; } = null!;

[InputArgument(Order = 1, Group = Groups.General)]
public string TaskName { get; set; } = null!;

[InputArgument(Order = 6, Required = false)]
public string UserName { get; set; } = null!;

[InputArgument(Order = 4, Group = Groups.General, Required = false)]
public string WorkingDirectory { get; set; } = null!;

public override void Execute(ActionContext context)
{
Debugger.Launch();

try
{
using var taskService = new TaskService(TargetServer, UserName, AccountDomain, Password);

using var task = taskService.FindTask(TaskName);
if (task is null)
{
throw new TaskNotFoundException(TaskName);
}

using var execAction = new ExecAction(Path, Arguments, WorkingDirectory);
task.Definition.Actions.Add(execAction);
}
catch (TaskNotFoundException ex)
{
throw new ActionException(ErrorCodes.TaskNotFound, ex.Message, ex);
}
catch (Exception ex)
{
throw new ActionException(ErrorCodes.Unknown, ex.Message, ex);
}
}
}
Loading

0 comments on commit 99ac9c8

Please sign in to comment.