using System.IO; using System.Threading; using System.Threading.Tasks; namespace Artemis.Core { /// /// Represents a plugin prerequisite action that runs a PowerShell script /// Note: To run an inline script instead, use /// public class RunPowerShellAction : PluginPrerequisiteAction { /// /// Creates a new instance of a copy folder action /// /// The name of the action /// The full path of the script to run /// A boolean indicating whether the file should run with administrator privileges /// /// Optional arguments to pass to your script, you are responsible for proper quoting etc. /// Arguments are available in PowerShell as $args[0], $args[1] etc. /// public RunPowerShellAction(string name, string scriptPath, bool elevate = false, string? arguments = null) : base(name) { ScriptPath = scriptPath; Elevate = elevate; Arguments = arguments; ProgressIndeterminate = true; } /// /// Gets the inline full path of the script to run /// public string ScriptPath { get; } /// /// Gets a boolean indicating whether the file should run with administrator privileges /// public bool Elevate { get; } /// /// Gets optional arguments to pass to your script, you are responsible for proper quoting etc. /// Arguments are available in PowerShell as $args[0], $args[1] etc. /// public string? Arguments { get; } /// public override async Task Execute(CancellationToken cancellationToken) { if (!ScriptPath.EndsWith(".ps1")) throw new ArtemisPluginException($"Script at path {ScriptPath} must have the .ps1 extension or PowerShell will refuse to run it"); if (!File.Exists(ScriptPath)) throw new ArtemisCoreException($"Script not found at path {ScriptPath}"); Status = "Running PowerShell script and waiting for exit.."; ShowProgressBar = true; ProgressIndeterminate = true; int result = await ExecuteFileAction.RunProcessAsync("powershell.exe", $"-ExecutionPolicy Unrestricted -File {ScriptPath} {Arguments}", Elevate); Status = $"PowerShell exited with code {result}"; } } }